Search in sources :

Example 21 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class MultipleIncludeTest method getSqlSessionFactoryJavaConfig.

private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:xmlextref", "sa", "");
    initDb(c);
    Configuration configuration = new Configuration();
    Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
    configuration.setEnvironment(environment);
    configuration.addMapper(MultipleIncludePersonMapper.class);
    return new SqlSessionFactoryBuilder().build(configuration);
}
Also used : UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) Configuration(org.apache.ibatis.session.Configuration) Connection(java.sql.Connection) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)

Example 22 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class SubstitutionInAnnotsTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:annots", "sa", "");
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(c);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    runner.runScript(reader);
    c.commit();
    reader.close();
    Configuration configuration = new Configuration();
    Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
    configuration.setEnvironment(environment);
    configuration.addMapper(SubstitutionInAnnotsMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
Also used : UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) Configuration(org.apache.ibatis.session.Configuration) Connection(java.sql.Connection) Reader(java.io.Reader) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) BeforeClass(org.junit.BeforeClass)

Example 23 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class ResultMapReferenceTest method getSqlSessionFactoryJavaConfig.

private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:xmlextref", "sa", "");
    initDb(c);
    Configuration configuration = new Configuration();
    Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
    configuration.setEnvironment(environment);
    configuration.addMapper(ResultMapReferencePersonMapper.class);
    configuration.addMapper(ResultMapReferencePetMapper.class);
    return new SqlSessionFactoryBuilder().build(configuration);
}
Also used : UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) Configuration(org.apache.ibatis.session.Configuration) Connection(java.sql.Connection) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)

Example 24 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class ResultLoader method newExecutor.

private Executor newExecutor() {
    final Environment environment = configuration.getEnvironment();
    if (environment == null) {
        throw new ExecutorException("ResultLoader could not load lazily.  Environment was not configured.");
    }
    final DataSource ds = environment.getDataSource();
    if (ds == null) {
        throw new ExecutorException("ResultLoader could not load lazily.  DataSource was not configured.");
    }
    final TransactionFactory transactionFactory = environment.getTransactionFactory();
    final Transaction tx = transactionFactory.newTransaction(ds, null, false);
    return configuration.newExecutor(tx, ExecutorType.SIMPLE);
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) Transaction(org.apache.ibatis.transaction.Transaction) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) Environment(org.apache.ibatis.mapping.Environment) DataSource(javax.sql.DataSource)

Example 25 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class XMLConfigBuilder method environmentsElement.

private void environmentsElement(XNode context) throws Exception {
    if (context != null) {
        if (environment == null) {
            environment = context.getStringAttribute("default");
        }
        for (XNode child : context.getChildren()) {
            String id = child.getStringAttribute("id");
            if (isSpecifiedEnvironment(id)) {
                TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                DataSource dataSource = dsFactory.getDataSource();
                Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory).dataSource(dataSource);
                configuration.setEnvironment(environmentBuilder.build());
            }
        }
    }
}
Also used : DataSourceFactory(org.apache.ibatis.datasource.DataSourceFactory) XNode(org.apache.ibatis.parsing.XNode) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) BaseBuilder(org.apache.ibatis.builder.BaseBuilder) Environment(org.apache.ibatis.mapping.Environment) DataSource(javax.sql.DataSource)

Aggregations

Environment (org.apache.ibatis.mapping.Environment)26 Configuration (org.apache.ibatis.session.Configuration)19 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)18 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)14 Connection (java.sql.Connection)10 UnpooledDataSource (org.apache.ibatis.datasource.unpooled.UnpooledDataSource)10 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)10 DataSource (javax.sql.DataSource)8 BeforeClass (org.junit.BeforeClass)6 Properties (java.util.Properties)5 SQLException (java.sql.SQLException)3 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)3 Transaction (org.apache.ibatis.transaction.Transaction)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 UnpooledDataSourceFactory (org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory)2 Executor (org.apache.ibatis.executor.Executor)2 ManagedTransactionFactory (org.apache.ibatis.transaction.managed.ManagedTransactionFactory)2 InputStreamReader (java.io.InputStreamReader)1