Search in sources :

Example 21 with SqlSessionFactory

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

the class DuplicateResourceTest method shouldDemonstrateDuplicateResourceIssue.

@Test
public void shouldDemonstrateDuplicateResourceIssue() throws Exception {
    final String resource = "org/apache/ibatis/submitted/duplicate_resource_loaded/Config.xml";
    final Reader reader = Resources.getResourceAsReader(resource);
    final SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    final SqlSessionFactory factory = builder.build(reader);
    final SqlSession sqlSession = factory.openSession();
    try {
        final Mapper mapper = sqlSession.getMapper(Mapper.class);
        final List<Map<String, Object>> list = mapper.selectAllBlogs();
        Assert.assertEquals(2, list.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) Map(java.util.Map) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 22 with SqlSessionFactory

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

the class CustomCollectionHandlingTest method testSelectListWithNestedSelect.

/**
     * Custom collections with nested select.
     *
     * @throws Exception
     */
@Test
public void testSelectListWithNestedSelect() throws Exception {
    String xmlConfig = "org/apache/ibatis/submitted/custom_collection_handling/MapperConfig.xml";
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        List<Person> list = sqlSession.selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithSelect");
        assertEquals(2, list.size());
        assertEquals(2, list.get(0).getContacts().size());
        assertEquals(1, list.get(1).getContacts().size());
        assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Test(org.junit.Test)

Example 23 with SqlSessionFactory

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

the class CustomCollectionHandlingTest method testSelectListWithNestedResultMap.

/**
     * Custom collections with nested resultMap.
     *
     * @throws Exception
     */
@Test
public void testSelectListWithNestedResultMap() throws Exception {
    String xmlConfig = "org/apache/ibatis/submitted/custom_collection_handling/MapperConfig.xml";
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        List<Person> list = sqlSession.selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithResultMap");
        assertEquals(2, list.size());
        assertEquals(2, list.get(0).getContacts().size());
        assertEquals(1, list.get(1).getContacts().size());
        assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Test(org.junit.Test)

Example 24 with SqlSessionFactory

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

the class NpeExtendsTest method testSelectNoName.

@Test
public void testSelectNoName() {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        StudentConstructorMapper studentConstructorMapper = sqlSession.getMapper(StudentConstructorMapper.class);
        StudentConstructor testStudent = studentConstructorMapper.selectNoNameById(1);
        assertEquals(1, testStudent.getConstructors().size());
        assertTrue(testStudent.getConstructors().contains(StudentConstructor.Constructor.ID));
        assertNull(testStudent.getName());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Test(org.junit.Test)

Example 25 with SqlSessionFactory

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

the class NpeExtendsTest method testSelectWithTeacher.

@Test
public void testSelectWithTeacher() {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        StudentConstructorMapper studentConstructorMapper = sqlSession.getMapper(StudentConstructorMapper.class);
        StudentConstructor testStudent = studentConstructorMapper.selectWithTeacherById(1);
        assertEquals(1, testStudent.getConstructors().size());
        assertTrue(testStudent.getConstructors().contains(StudentConstructor.Constructor.ID_NAME));
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) 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