Search in sources :

Example 36 with SqlSessionFactoryBuilder

use of org.apache.ibatis.session.SqlSessionFactoryBuilder in project mybatis-3 by mybatis.

the class CustomizationTest method applyDefaultValueWhenCustomizeDefaultValueSeparator.

@Test
public void applyDefaultValueWhenCustomizeDefaultValueSeparator() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
    Configuration configuration = factory.getConfiguration();
    configuration.addMapper(CustomDefaultValueSeparatorMapper.class);
    SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));
    Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.NULL));
    Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), Is.is("jdbc:hsqldb:mem:global_variables_defaults"));
    Assert.assertThat(configuration.getDatabaseId(), Is.is("hsql"));
    Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), Is.is("default"));
    Assert.assertThat(cache.getName(), Is.is("default"));
    SqlSession sqlSession = factory.openSession();
    try {
        CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
        Assert.assertThat(mapper.selectValue(null), Is.is("default"));
    } finally {
        sqlSession.close();
    }
}
Also used : Configuration(org.apache.ibatis.session.Configuration) SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) Properties(java.util.Properties) Test(org.junit.Test)

Example 37 with SqlSessionFactoryBuilder

use of org.apache.ibatis.session.SqlSessionFactoryBuilder in project mybatis-3 by mybatis.

the class XmlMapperTest method applyPropertyValueOnXmlMapper.

@Test
public void applyPropertyValueOnXmlMapper() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    props.setProperty("cache.name", "custom");
    props.setProperty("select.columns", "'5555'");
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
    Configuration configuration = factory.getConfiguration();
    configuration.addMapper(XmlMapper.class);
    SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
    Assert.assertThat(cache.getName(), Is.is("custom"));
    SqlSession sqlSession = factory.openSession();
    try {
        XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
        Assert.assertThat(mapper.ping(), Is.is("Hi"));
        Assert.assertThat(mapper.selectOne(), Is.is("1"));
        Assert.assertThat(mapper.selectFromVariable(), Is.is("5555"));
    } finally {
        sqlSession.close();
    }
}
Also used : Configuration(org.apache.ibatis.session.Configuration) SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) Properties(java.util.Properties) Test(org.junit.Test)

Example 38 with SqlSessionFactoryBuilder

use of org.apache.ibatis.session.SqlSessionFactoryBuilder in project mybatis-3 by mybatis.

the class HeavyInitialLoadTest method initSqlSessionFactory.

@BeforeClass
public static void initSqlSessionFactory() throws Exception {
    Connection conn = null;
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        conn = DriverManager.getConnection("jdbc:hsqldb:mem:heavy_initial_load", "sa", "");
        Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/heavy_initial_load/ibatisConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        reader.close();
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) BeforeClass(org.junit.BeforeClass)

Example 39 with SqlSessionFactoryBuilder

use of org.apache.ibatis.session.SqlSessionFactoryBuilder in project mybatis-3 by mybatis.

the class MapTypeHandlerTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    // create an SqlSessionFactory
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/maptypehandler/mybatis-config.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();
    // populate in-memory database
    SqlSession session = sqlSessionFactory.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/maptypehandler/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Example 40 with SqlSessionFactoryBuilder

use of org.apache.ibatis.session.SqlSessionFactoryBuilder in project mybatis-3 by mybatis.

the class MissingIdPropertyTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    // create a SqlSessionFactory
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/missing_id_property/MapperConfig.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();
    // populate in-memory database
    SqlSession session = sqlSessionFactory.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/missing_id_property/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Aggregations

SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)175 Reader (java.io.Reader)159 Connection (java.sql.Connection)134 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)109 BeforeClass (org.junit.BeforeClass)98 SqlSession (org.apache.ibatis.session.SqlSession)85 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)42 Configuration (org.apache.ibatis.session.Configuration)30 Test (org.junit.Test)30 Before (org.junit.Before)18 Environment (org.apache.ibatis.mapping.Environment)14 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)14 UnpooledDataSource (org.apache.ibatis.datasource.unpooled.UnpooledDataSource)10 Properties (java.util.Properties)9 PrintWriter (java.io.PrintWriter)6 InputStream (java.io.InputStream)3 DataSource (javax.sql.DataSource)3 BaseDataTest (org.apache.ibatis.BaseDataTest)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)3