Java Code Examples for org.apache.ibatis.session.SqlSessionFactoryBuilder

The following examples show how to use org.apache.ibatis.session.SqlSessionFactoryBuilder. These examples are extracted from open source projects. 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 Project: mybatis   Source File: NestedResultHandlerTest.java    License: 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/nestedresulthandler/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/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example 2
Source Project: code   Source File: MybatisTest.java    License: Apache License 2.0 6 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 3
Source Project: mybaties   Source File: MissingIdPropertyTest.java    License: 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 4
Source Project: mybatis   Source File: CollectionParametersTest.java    License: 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 5
Source Project: mybatis   Source File: MultipleResultSetTest.java    License: 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 6
Source Project: Project   Source File: FlowerTest.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
      // 加载 xml 文件流
      InputStream is = Resources.getResourceAsStream("MyBatis.xml");
      //使用工厂设计模式
      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
      //生产SqlSession
      SqlSession session=factory.openSession();

      List<Flower> list = session.selectList("a.b.selAll");
      for (Flower flower : list) {
          System.out.println(flower.toString());
      }

      int count = session.selectOne("a.b.selById");
System.out.println(count);

      //把数据库中哪个列的值当作map的key
      Map<Object, Object> map = session.selectMap("a.b.selectMap", "name");
      System.out.println(map);

      session.close();
  }
 
Example 7
Source Project: mybatis   Source File: CallSettersOnNullsTest.java    License: 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 8
Source Project: mybatis   Source File: RefCursorTest.java    License: 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 9
Source Project: SSO   Source File: MybatisSessionInstance.java    License: 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 10
Source Project: mybaties   Source File: CallSettersOnNullsTest.java    License: 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 11
Source Project: mybatis   Source File: CacheOrderTest.java    License: 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 12
Source Project: mybaties   Source File: OgnlStaticTest.java    License: 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 13
Source Project: mybatis   Source File: InsertTest.java    License: 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 14
Source Project: mybatis   Source File: BaseTest.java    License: 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 15
Source Project: mybatis   Source File: PropertiesInMappersTest.java    License: 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 16
Source Project: oneops   Source File: CmsCmProcessorTest.java    License: 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 17
Source Project: cg-blog   Source File: CeshiCustomer.java    License: 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 18
Source Project: mybaties   Source File: ColumnPrefixTest.java    License: 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 19
Source Project: mybatis   Source File: ForEachMapTest.java    License: 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 20
Source Project: mybaties   Source File: BatchKeysTest.java    License: 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();
    }
  }
}
 
Example 21
Source Project: mybaties   Source File: DmlMapperReturnTypesTest.java    License: 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 22
Source Project: mybaties   Source File: SubstitutionInAnnotsTest.java    License: 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 23
Source Project: mybatis   Source File: ComplexTypeTest.java    License: 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 24
Source Project: mybatis   Source File: CamelCaseMappingTest.java    License: 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 25
Source Project: mybatis   Source File: ResulthandlerTest.java    License: 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 26
Source Project: mybaties   Source File: InlineCollectionWithDotTest.java    License: 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 27
Source Project: mybatis   Source File: PermissionsTest.java    License: 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 28
Source Project: mybatis   Source File: AutomaticLazyLoadingTest.java    License: 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 29
Source Project: mybaties   Source File: ImmutablePOJOTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupClass() throws Exception {
  Connection conn = null;

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

    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/immutable_constructor/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/immutable_constructor/ibatisConfig.xml");
    factory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();
  } finally {
    if (conn != null) {
      conn.close();
    }
  }
}
 
Example 30
Source Project: mybatis   Source File: BaseTest.java    License: 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/global_variables/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/global_variables/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}