Example usage for org.apache.ibatis.transaction TransactionFactory setProperties

List of usage examples for org.apache.ibatis.transaction TransactionFactory setProperties

Introduction

In this page you can find the example usage for org.apache.ibatis.transaction TransactionFactory setProperties.

Prototype

default void setProperties(Properties props) 

Source Link

Document

Sets transaction factory custom properties.

Usage

From source file:cc.oit.dao.impl.mybatis.session.XMLConfigBuilder.java

License:Apache License

private TransactionFactory transactionManagerElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        Properties props = context.getChildrenAsProperties();
        TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();
        factory.setProperties(props);
        return factory;
    }//from  w  ww  .j av  a  2s .  c o  m
    throw new BuilderException("Environment declaration requires a TransactionFactory.");
}

From source file:com.stone.base.dao.SqlSessionFactoryBean.java

License:Apache License

/**
 * Build a SqlSessionFactory instance.// w ww.  j  ava  2 s.co m
 *
 * The default implementation uses the standard MyBatis
 * {@link XMLConfigBuilder} API to build a SqlSessionFactory instance based
 * on an Reader.
 *
 * @see org.apache.ibatis.builder.xml.XMLConfigBuilder#parse()
 *
 * @return SqlSessionFactory
 *
 * @throws IOException
 *             if loading the config file failed
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
protected SqlSessionFactory buildSqlSessionFactory()
        throws IOException, IllegalAccessException, InstantiationException {

    XMLConfigBuilder xmlConfigBuilder;
    Configuration configuration;

    if (this.configLocation != null) {
        Reader reader = null;
        try {
            reader = new InputStreamReader(this.configLocation.getInputStream());
            // Null environment causes the configuration to use the default.
            // This will be overwritten below regardless.
            xmlConfigBuilder = new XMLConfigBuilder(reader, null, this.configurationProperties);
            configuration = xmlConfigBuilder.parse();
        } catch (IOException ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) {
                    // close quietly
                }
            }
        }

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Parsed configuration file: '" + this.configLocation + "'");
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
    }

    TransactionFactory transactionFactory = this.transactionFactoryClass.newInstance(); // expose IllegalAccessException,
    // InstantiationException

    transactionFactory.setProperties(this.transactionFactoryProperties);
    Environment environment = new Environment(this.environment, transactionFactory, this.dataSource);

    configuration.setEnvironment(environment);
    boolean mapper = false;
    if (!ObjectUtils.isEmpty(this.mapperLocations)) {
        Map<String, XNode> sqlFragments = new HashMap<String, XNode>();

        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            // MyBatis holds a Map using "resource" name as a key.
            // If a mapper file is loaded, it searches for a mapper
            // interface type.
            // If the type is found then it tries to load the mapper file
            // again looking for this:
            //
            // String xmlResource = type.getName().replace('.', '/') +
            // ".xml";
            //
            // So if a mapper interface exists, resource cannot be an
            // absolute path.
            // Otherwise MyBatis will throw an exception because
            // it will load both a mapper interface and the mapper xml file,
            // and throw an exception telling that a mapperStatement cannot
            // be loaded twice.
            String path;
            if (mapperLocation instanceof ClassPathResource) {
                path = ((ClassPathResource) mapperLocation).getPath();
            } else {
                // this won't work if there is also a mapper interface in
                // classpath
                path = mapperLocation.toString();
            }

            Reader reader = null;
            try {
                reader = new InputStreamReader(mapperLocation.getInputStream());
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(reader, configuration, path,
                        sqlFragments);
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ignored) {
                    }
                }
            }

            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
        mapper = true;
    }
    // if(this.mapperPackageName!=null&&!"".equals(this.mapperPackageName)){
    // configuration.addMappers(this.mapperPackageName);
    // mapper = true;
    // }
    for (String p : this.mapperPackageNames) {
        if (p != null && !"".equals(p)) {
            configuration.addMappers(p);
            mapper = true;
        }
    }

    if (!mapper) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug(
                    "Property 'mapperLocations' or 'mapperPackageName' was not specified, only MyBatis mapper files specified in the config xml were loaded");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}