Search in sources :

Example 16 with SqlSessionFactory

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

the class ConfigurationTest method applyPropertyValueOnXmlConfiguration.

@Test
public void applyPropertyValueOnXmlConfiguration() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    props.setProperty("settings.jdbcTypeForNull", JdbcType.CHAR.name());
    props.setProperty("db.name", "global_variables_defaults_custom");
    props.setProperty("productName.hsql", "Hsql");
    props.setProperty("objectFactory.name", "custom");
    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();
    Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.CHAR));
    Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), Is.is("jdbc:hsqldb:mem:global_variables_defaults_custom"));
    Assert.assertThat(configuration.getDatabaseId(), IsNull.nullValue());
    Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), Is.is("custom"));
}
Also used : Configuration(org.apache.ibatis.session.Configuration) 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 17 with SqlSessionFactory

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

the class InsertTest method testInsertMappedBatch.

// Not supported yet in PostgreSQL
@Ignore
@Test
public void testInsertMappedBatch() throws Exception {
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    try {
        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
        Name name = new Name();
        name.setFirstName("Fred");
        name.setLastName("Flintstone");
        mapper.insertNameMapped(name);
        Name name2 = new Name();
        name2.setFirstName("Wilma");
        name2.setLastName("Flintstone");
        mapper.insertNameMapped(name2);
        List<BatchResult> batchResults = sqlSession.flushStatements();
        assertNotNull(name.getId());
        assertNotNull(name2.getId());
        assertEquals(1, batchResults.size());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) BatchResult(org.apache.ibatis.executor.BatchResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with SqlSessionFactory

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

the class InlineCollectionWithDotTest method openSession.

public void openSession(String aConfig) throws Exception {
    final String resource = "org/apache/ibatis/submitted/inline_association_with_dot/ibatis-" + aConfig + ".xml";
    Reader batisConfigReader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sqlSessionFactory;
    try {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(batisConfigReader);
    } catch (Exception anException) {
        throw new RuntimeException("Mapper configuration failed, expected this to work: " + anException.getMessage(), anException);
    }
    SqlSession session = sqlSessionFactory.openSession();
    Connection conn = session.getConnection();
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    Reader createScriptReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/inline_association_with_dot/create.sql");
    runner.runScript(createScriptReader);
    sqlSession = sqlSessionFactory.openSession();
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner)

Example 19 with SqlSessionFactory

use of org.apache.ibatis.session.SqlSessionFactory 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 20 with SqlSessionFactory

use of org.apache.ibatis.session.SqlSessionFactory 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)

Aggregations

SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)52 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)42 Reader (java.io.Reader)39 Test (org.junit.Test)30 SqlSession (org.apache.ibatis.session.SqlSession)26 Connection (java.sql.Connection)18 Configuration (org.apache.ibatis.session.Configuration)13 Properties (java.util.Properties)8 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)4 HashMap (java.util.HashMap)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)3 InputStream (java.io.InputStream)2 List (java.util.List)2 DbSqlSessionFactory (org.activiti.engine.impl.db.DbSqlSessionFactory)2 Before (org.junit.Before)2 MovieMapper (com.mapper.MovieMapper)1 Movie (com.po.Movie)1 StringReader (java.io.StringReader)1 Statement (java.sql.Statement)1