org.apache.ibatis.session.SqlSessionFactoryBuilder Java Examples

The following examples show how to use org.apache.ibatis.session.SqlSessionFactoryBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MybatisTest.java    From code with Apache License 2.0 7 votes vote down vote up
/**
 * 测试mybatis环境
 */
public static void main(String[] args) throws Exception {
    // 1、读取配置文件
    InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 2、创建SqlSessionFactory的建造者
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    // 3、使用建造者创建工厂对象SqlSessionFactory
    SqlSessionFactory factory = builder.build(in);
    // 4、使用SqlSessionFactory创建SqlSession对象
    SqlSession session = factory.openSession();
    // 5、使用SqlSession创建dao接口的代理对象
    UserMapper userMapper = session.getMapper(UserMapper.class);
    // 6、使用代理对象执行sql查询
    System.out.println("基于XML配置");
    userMapper.findAll().forEach(System.out::println);
    System.out.println("基于注解配置");
    userMapper.findUsers().forEach(System.out::println);
    // 7、释放资源
    session.close();
    in.close();
}
 
Example #2
Source File: ResulthandlerTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/result_handler/mybatis-config.xml");
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  reader.close();
  sqlSessionFactory.getConfiguration().addMapper(Mapper.class);

  // populate in-memory database
  SqlSession session = sqlSessionFactory.openSession();
  Connection conn = session.getConnection();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/result_handler/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #3
Source File: RoundingHandlersTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/rounding/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/rounding/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #4
Source File: DmlMapperReturnTypesTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader(XML);
  try {
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  } finally {
    reader.close();
  }

  // populate in-memory database
  SqlSession session = sqlSessionFactory.openSession();
  try {
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader(SQL);
    try {
      ScriptRunner runner = new ScriptRunner(conn);
      runner.setLogWriter(null);
      runner.runScript(reader);
    } finally {
      reader.close();
    }
  } finally {
    session.close();
  }
}
 
Example #5
Source File: CollectionParametersTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create an SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/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/collectionparameters/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #6
Source File: CallSettersOnNullsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources
      .getResourceAsReader("org/apache/ibatis/submitted/call_setters_on_nulls/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/call_setters_on_nulls/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #7
Source File: MissingIdPropertyTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #8
Source File: MultipleResultSetTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/mybatis-config.xml");
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  reader.close();
  
  // populate in-memory database
  // Could not get the table creation, procedure creation, and data population to work from the same script.
  // Once it was in three scripts, all seemed well.
  SqlSession session = sqlSessionFactory.openSession();
  Connection conn = session.getConnection();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB1.sql");
  runReaderScript(conn, session, reader);
  reader.close();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB2.sql");
  runReaderScript(conn, session, reader);
  reader.close();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB3.sql");
  runReaderScript(conn, session, reader);
  reader.close();
  session.close();
}
 
Example #9
Source File: NestedResultHandlerAssociationTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create an SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/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/nestedresulthandler_association/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #10
Source File: AutomaticLazyLoadingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
/**
 * Create a session with the specified configuration and initialized the database.
 */
private void openSession(String aConfig) throws Exception {
  final String resource = "org/apache/ibatis/submitted/automatic_lazy_loading/ibatis-automatic-lazy-load-" + 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/automatic_lazy_loading/create.sql");
  runner.runScript(createScriptReader);

  sqlSession = sqlSessionFactory.openSession();
}
 
Example #11
Source File: PermissionsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/permissions/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/permissions/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.setErrorLogWriter(null);
  runner.runScript(reader);
  conn.commit();
  conn.close();
  reader.close();
}
 
Example #12
Source File: InlineCollectionWithDotTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
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();
  }
 
Example #13
Source File: RefCursorTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testRefCursor2() throws IOException {
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/refcursor/MapperConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        Map<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("orderId", 1);
        mapper.getOrder2(parameter);
        
        assertNotNull(parameter.get("order"));
        List<Order> orders = (List<Order>) parameter.get("order");
        assertEquals(1, orders.size());
        Order order = orders.get(0);
        assertEquals(3, order.getDetailLines().size());
    } finally {
        sqlSession.close();
    }
}
 
Example #14
Source File: MybatisSessionInstance.java    From SSO with Apache License 2.0 6 votes vote down vote up
public static SqlSession getInstance() {
	if (sf == null) {
		synchronized (SqlSessionFactory.class) {
			if (sf == null) {
				try {
					Reader reader = Resources.getResourceAsReader(resource);
					sf = new SqlSessionFactoryBuilder().build(reader);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
	}
	return sf.openSession();
}
 
Example #15
Source File: CallSettersOnNullsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources
      .getResourceAsReader("org/apache/ibatis/submitted/call_setters_on_nulls/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/call_setters_on_nulls/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #16
Source File: CacheOrderTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/cacheorder/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/cacheorder/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #17
Source File: MybatisTest.java    From springdream with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // 获取SqlSessionFactory
    String resource = "MyBatis-Configuration.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    // 获取SqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUser(1);
        System.out.println("\nprint user info --> " + user.toString());
    } finally {
        sqlSession.close();
    }
}
 
Example #18
Source File: OgnlStaticTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/ognlstatic/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/ognlstatic/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #19
Source File: InsertTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example #20
Source File: CamelCaseMappingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  Connection conn = null;

  try {
    Class.forName("org.hsqldb.jdbcDriver");
    conn = DriverManager.getConnection("jdbc:hsqldb:mem:gname", "sa", "");
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/camelcase/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    runner.runScript(reader);
    conn.commit();
    reader.close();

    reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/camelcase/MapperConfig.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();

  } finally {
    if (conn != null) {
      conn.close();
    }
  }
}
 
Example #21
Source File: ComplexTypeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/complex_type/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/complex_type/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #22
Source File: BaseTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create an SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/basetest/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/basetest/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #23
Source File: PropertiesInMappersTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  
  // this property value should be replaced on all mapper files
  Properties p = new Properties();
  p.put("property", "id");
  
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/mybatis-config.xml");
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, p);
  reader.close();

  // populate in-memory database
  SqlSession session = sqlSessionFactory.openSession();
  Connection conn = session.getConnection();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #24
Source File: CmsCmProcessorTest.java    From oneops with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    System.out.println("Starting up CmsCmProcessor tests");
 String resource = "mybatis-config.xml";
 Reader reader = Resources.getResourceAsReader( resource );
 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
    session = sf.openSession();
 clazzMapper = session.getMapper(ClazzMapper.class);
    ciMapper = session.getMapper(CIMapper.class);
    mdValidator = new CmsMdValidator();
    cmValidator = new CmsCmValidator();
 mdProcessor = new CmsMdProcessor();
 mdProcessor.setClazzMapper( clazzMapper );
 mdProcessor.setMdValidator(mdValidator);
    cmProcessor = new CmsCmProcessor();
    cmProcessor.setCiMapper(ciMapper);
    cmProcessor.setMdProcessor(mdProcessor);
    cmProcessor.setCmValidator(cmValidator);
    RelationMapper relationMapper=session.getMapper(RelationMapper.class);
    mdProcessor.setRelationMapper(relationMapper);
    
}
 
Example #25
Source File: SubstitutionInAnnotsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:annots", "sa", "");
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(c);
  runner.setLogWriter(null);
  runner.setErrorLogWriter(null);
  runner.runScript(reader);
  c.commit();
  reader.close();

  Configuration configuration = new Configuration();
  Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
  configuration.setEnvironment(environment);
  
  configuration.addMapper(SubstitutionInAnnotsMapper.class);
  
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #26
Source File: CeshiCustomer.java    From cg-blog with MIT License 6 votes vote down vote up
@Test
public void testFind() throws IOException {
    /**
     *  1、获得 SqlSessionFactory
     *  2、获得 SqlSession
     *  3、调用在 mapper 文件中配置的 SQL 语句
     */
    String resource = "sqlMapConfig.xml";           // 定位核心配置文件
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    // 创建 SqlSessionFactory

    SqlSession sqlSession = sqlSessionFactory.openSession();    // 获取到 SqlSession

    Map map = new HashMap();
    map.put("customerName", "charies");

    // 调用 mapper 中的方法:命名空间 + id
    List<Customer> customerList = sqlSession.selectList("yeepay.payplus.mapper.CustomerMapper.find", map);

    for (Customer c : customerList) {
        System.out.println(c);
    }
}
 
Example #27
Source File: ColumnPrefixTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  Connection conn = null;

  try {
    Class.forName("org.hsqldb.jdbcDriver");
    conn = DriverManager.getConnection("jdbc:hsqldb:mem:clmpfx", "sa", "");
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/column_prefix/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    runner.runScript(reader);
    conn.commit();
    reader.close();

    reader = Resources.getResourceAsReader(getConfigPath());
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();

  } finally {
    if (conn != null) {
      conn.close();
    }
  }
}
 
Example #28
Source File: ForEachMapTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach_map/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/foreach_map/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example #29
Source File: DmlMapperReturnTypesTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create a SqlSessionFactory
  Reader reader = Resources.getResourceAsReader(XML);
  try {
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  } finally {
    reader.close();
  }

  // populate in-memory database
  SqlSession session = sqlSessionFactory.openSession();
  try {
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader(SQL);
    try {
      ScriptRunner runner = new ScriptRunner(conn);
      runner.setLogWriter(null);
      runner.runScript(reader);
    } finally {
      reader.close();
    }
  } finally {
    session.close();
  }
}
 
Example #30
Source File: BatchKeysTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  Connection conn = null;

  try {
    Class.forName("org.hsqldb.jdbcDriver");
    conn = DriverManager.getConnection("jdbc:hsqldb:mem:batch_keys", "sa", "");

    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/batch_keys/CreateDB.sql");

    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(new PrintWriter(System.err));
    runner.runScript(reader);
    conn.commit();
    reader.close();

    reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/batch_keys/Config.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();
  } finally {
    if (conn != null) {
      conn.close();
    }
  }
}