Search in sources :

Example 36 with SqlSessionFactory

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

the class CustomizationTest method applyPropertyValueWhenCustomizeDefaultValueSeparator.

@Test
public void applyPropertyValueWhenCustomizeDefaultValueSeparator() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
    props.setProperty("settings:jdbcTypeForNull", JdbcType.CHAR.name());
    props.setProperty("db:name", "global_variables_defaults_custom");
    props.setProperty("productName:hsql", "Hsql");
    props.setProperty("objectFactory:name", "customObjectFactory");
    props.setProperty("cache:name", "customCache");
    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.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("customObjectFactory"));
    Assert.assertThat(cache.getName(), Is.is("customCache"));
    SqlSession sqlSession = factory.openSession();
    try {
        CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
        Assert.assertThat(mapper.selectValue("3333"), Is.is("3333"));
    } 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 SqlSessionFactory

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

the class XmlMapperTest method applyDefaultValueOnXmlMapper.

@Test
public void applyDefaultValueOnXmlMapper() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    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("default"));
    SqlSession sqlSession = factory.openSession();
    try {
        XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
        Assert.assertThat(mapper.ping(), Is.is("Hello"));
        Assert.assertThat(mapper.selectOne(), Is.is("1"));
        Assert.assertThat(mapper.selectFromVariable(), Is.is("9999"));
    } 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 SqlSessionFactory

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

the class ManyAnnoTest method testGetMessageForEmptyDatabase.

@Test
public void testGetMessageForEmptyDatabase() throws Exception {
    final Environment environment = new Environment("test", new JdbcTransactionFactory(), BaseDataTest.createBlogDataSource());
    final Configuration config = new Configuration(environment);
    config.addMapper(PostMapper.class);
    final SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
    final SqlSession session = factory.openSession();
    PostMapper mapper = session.getMapper(PostMapper.class);
    List<AnnoPost> posts = mapper.getPosts(101);
    assertEquals(3, posts.size());
    assertEquals(3, posts.get(0).getTags().size());
    assertEquals(1, posts.get(1).getTags().size());
    assertEquals(0, posts.get(2).getTags().size());
    session.close();
}
Also used : Configuration(org.apache.ibatis.session.Configuration) SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 39 with SqlSessionFactory

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

the class AnnotationMapperTest method applyDefaultValueOnAnnotationMapper.

@Test
public void applyDefaultValueOnAnnotationMapper() throws IOException {
    Properties props = new Properties();
    props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
    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(AnnotationMapper.class);
    SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
    Assert.assertThat(cache.getName(), Is.is("default"));
    SqlSession sqlSession = factory.openSession();
    try {
        AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
        Assert.assertThat(mapper.ping(), Is.is("Hello"));
    } 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 40 with SqlSessionFactory

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

the class InsertTest method testInsertAnnotated.

@Test
public void testInsertAnnotated() throws Exception {
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
        Name name = new Name();
        name.setFirstName("Fred");
        name.setLastName("Flintstone");
        int rows = mapper.insertNameAnnotated(name);
        assertNotNull(name.getId());
        assertEquals(1, rows);
    } 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) Test(org.junit.Test)

Aggregations

SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)53 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