Search in sources :

Example 41 with SqlSessionFactory

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

the class InsertTest method testInsertMapped.

@Test
public void testInsertMapped() 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.insertNameMapped(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)

Example 42 with SqlSessionFactory

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

the class MultipleIncludeTest method getSqlSessionFactoryXmlConfig.

private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
    Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleIncludeMapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
    configReader.close();
    Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
    initDb(conn);
    return sqlSessionFactory;
}
Also used : SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder)

Example 43 with SqlSessionFactory

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

the class MultipleReverseIncludeTest method getSqlSessionFactoryXmlConfig.

private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
    Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleReverseIncludeMapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
    configReader.close();
    Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
    initDb(conn);
    return sqlSessionFactory;
}
Also used : SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder)

Example 44 with SqlSessionFactory

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

the class NonFullyQualifiedNamespaceTest method testCrossReferenceXmlConfig.

@Test
public void testCrossReferenceXmlConfig() throws Exception {
    Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespaceConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
    configReader.close();
    Configuration configuration = sqlSessionFactory.getConfiguration();
    MappedStatement selectPerson = configuration.getMappedStatement("person namespace.select");
    assertEquals("org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespacePersonMapper.xml", selectPerson.getResource());
    Connection conn = configuration.getEnvironment().getDataSource().getConnection();
    initDb(conn);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        Person person = (Person) sqlSession.selectOne("person namespace.select", 1);
        assertEquals((Integer) 1, person.getId());
        assertEquals(2, person.getPets().size());
        assertEquals((Integer) 2, person.getPets().get(1).getId());
        Pet pet = (Pet) sqlSession.selectOne("person namespace.selectPet", 1);
        assertEquals(Integer.valueOf(1), pet.getId());
        Pet pet2 = (Pet) sqlSession.selectOne("pet namespace.select", 3);
        assertEquals((Integer) 3, pet2.getId());
        assertEquals((Integer) 2, pet2.getOwner().getId());
    } finally {
        sqlSession.close();
    }
}
Also used : Configuration(org.apache.ibatis.session.Configuration) 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) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Test(org.junit.Test)

Example 45 with SqlSessionFactory

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

the class ParameterMapReferenceTest method getSqlSessionFactoryXmlConfig.

private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
    Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/ParameterMapReferenceMapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
    configReader.close();
    Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
    initDb(conn);
    return sqlSessionFactory;
}
Also used : SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder)

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