Example usage for org.apache.ibatis.session Configuration Configuration

List of usage examples for org.apache.ibatis.session Configuration Configuration

Introduction

In this page you can find the example usage for org.apache.ibatis.session Configuration Configuration.

Prototype

public Configuration(Environment environment) 

Source Link

Usage

From source file:com.baifendian.swordfish.dao.datasource.ConnectionFactory.java

License:Apache License

/**
 *  sql session factory/*ww  w  .j  a v  a  2 s .  c  o m*/
 */
public static SqlSessionFactory getSqlSessionFactory() {
    if (sqlSessionFactory == null) {
        synchronized (ConnectionFactory.class) {
            if (sqlSessionFactory == null) {
                DataSource dataSource = getDataSource();
                TransactionFactory transactionFactory = new JdbcTransactionFactory();

                Environment environment = new Environment("development", transactionFactory, dataSource);

                Configuration configuration = new Configuration(environment);
                configuration.setLazyLoadingEnabled(true);
                configuration.addMappers("com.baifendian.swordfish.dao.mapper");

                SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
                sqlSessionFactory = builder.build(configuration);
            }
        }
    }

    return sqlSessionFactory;
}

From source file:com.dvdprime.server.mobile.config.MyBatisConfig.java

License:Apache License

/**
 * MyBatis ?  Configuration? .//from   w w  w  . j ava2 s . c o m
 * 
 * @return
 */
public Configuration getConfig() {
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("master", transactionFactory, getTomcatDataSource());

    logger.info("MyBatis Configuration Initialization.");
    Configuration configuration = new Configuration(environment);
    configuration.setCacheEnabled(true);
    configuration.setLazyLoadingEnabled(false);
    configuration.setAggressiveLazyLoading(false);
    configuration.setUseColumnLabel(true);
    configuration.setUseGeneratedKeys(false);
    configuration.setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
    configuration.setDefaultExecutorType(ExecutorType.REUSE);
    configuration.setDefaultStatementTimeout(25000);
    configuration.setSafeRowBoundsEnabled(true);

    // Alias Type
    Iterator<String> it = TypeAliasProp.getProperties().keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        logger.info("typeAliasRegistry: [{}] -> [{}]", key, TypeAliasProp.getProperties().get(key));
        configuration.getTypeAliasRegistry().registerAlias(key,
                (String) TypeAliasProp.getProperties().get(key));
    }

    // Mapper
    it = MapperProp.getProperties().keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        logger.info("mapper loaded: [{}]", MapperProp.getProperties().get(key));
        try {
            InputStream inputStream = Resources
                    .getResourceAsStream((String) MapperProp.getProperties().get(key));
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration,
                    (String) MapperProp.getProperties().get(key), configuration.getSqlFragments());
            mapperParser.parse();
        } catch (IOException e) {
            logger.error("mapper parsing   ?.");
        }
    }

    return configuration;
}

From source file:com.ehensin.paypal.infra.db.DBRepository.java

License:Apache License

private SqlSessionFactory getSqlSessionFactory(Map<String, String> properties) {
    DataSourceConfig config = new DataSourceConfig();
    config.setDbUrl(properties.get("url"));
    config.setDriver(properties.get("driver"));
    config.setPartition(Integer.valueOf(properties.get("partions")));
    config.setUserName(properties.get("username"));
    config.setPassword(properties.get("password"));
    config.setMinConnections(Integer.valueOf(properties.get("minconnection")));
    config.setMaxConnections(Integer.valueOf(properties.get("maxconnection")));

    DataSource dataSource = DataSourceFactory.getDataSource(config);
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("account-service", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    for (String mapper : mappers) {
        configuration.addMappers(mapper);
    }//w  ww .  j  a  v a  2s .  co m
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return sqlSessionFactory;
}

From source file:com.gf.components.mybatis.AbstractTest.java

License:Apache License

@Before
public void init() throws Exception {

    DataSourceManagerImpl manager = new DataSourceManagerImpl();
    manager.setUser("sa");
    manager.setPassword("");
    manager.setDriverClass("org.h2.Driver");
    manager.setUrl("jdbc:h2:" + tmpDirPath + "/db-" + sessionId);
    manager.setPoolSize(10);/*from   w  w  w.java2 s .  co m*/
    manager.init();

    dataSource = manager.getDataSource();

    TransactionFactory transactionFactory = new ManagedTransactionFactory();
    Environment environment = new Environment("default-env", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMappers("com.gf", AbstractMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

}

From source file:com.taobao.tdhs.jdbc.test.mybatis.TestBase.java

License:Open Source License

private SqlSession createSession(DataSource dataSource) {
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(PersonMapper.class);
    configuration.addMapper(TestMapper.class);
    configuration.addMapper(OrderMapper.class);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return sqlSessionFactory.openSession();
}

From source file:com.tianjunwei.dynamic.SimpleTableAnnotatedMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*from w w  w. jav a2  s  . c o  m*/
    InputStream is = getClass().getResourceAsStream("/examples/simple/CreateSimpleDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(SimpleTableAnnotatedMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.animal.data.AnimalDataTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//from   w w w  .  j  a  v a 2 s  . c o m
    InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(AnimalDataMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.column.comparison.ColumnComparisonTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//from  w  w w  .j a  va 2  s . co  m
    InputStream is = getClass().getResourceAsStream("/examples/column/comparison/CreateDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(ColumnComparisonMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.generated.always.mybatis.GeneratedAlwaysAnnotatedMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*from  www  . j  a  va  2s. c  om*/
    InputStream is = getClass().getResourceAsStream("/examples/generated/always/CreateGeneratedAlwaysDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(GeneratedAlwaysAnnotatedMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.groupby.GroupByTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//from  w w  w .j ava  2 s  . c om
    InputStream is = getClass().getResourceAsStream("/examples/groupby/CreateGroupByDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(GroupByMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}