Java Code Examples for org.apache.ibatis.session.SqlSessionFactoryBuilder#build()

The following examples show how to use org.apache.ibatis.session.SqlSessionFactoryBuilder#build() . 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: MybatisUtil.java    From code with Apache License 2.0 6 votes vote down vote up
public static void template(Callback callback) {
    SqlSession session = null;
    //1.读取配置文件
    try (InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");) {
        //2.创建构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.创建 SqlSession 工厂对象
        factory = builder.build(in);
        //4.创建 SqlSession 对象
        session = factory.openSession();
        //5.执行操作
        callback.execute(session);
        //6.提交或回滚事务
        session.commit();
    } catch (IOException e) {
        if (session != null) {
            session.rollback();
        }
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
 
Example 3
Source File: IncludeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncludes() throws Exception {
  String resource = "org/apache/ibatis/submitted/includes/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlMapper = builder.build(reader);
  assertNotNull(sqlMapper);

  final SqlSession sqlSession = sqlMapper.openSession();
  try {
    final Integer result = sqlSession.selectOne("org.apache.ibatis.submitted.includes.mapper.selectWithProperty");
    Assert.assertEquals(Integer.valueOf(1), result);
  } finally {
    sqlSession.close();
  }
}
 
Example 4
Source File: MybatisCRUDTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    SqlSessionFactory factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 5
Source File: ExternalRefidResolutionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalRefAfterSelectKey() throws Exception {
  String resource = "org/apache/ibatis/submitted/refid_resolution/ExternalMapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlSessionFactory = builder.build(reader);
  reader.close();
  sqlSessionFactory.getConfiguration().getMappedStatementNames();
}
 
Example 6
Source File: IntegerEnumTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseMapWithIntegerJdbcType() throws Exception {
  String resource = "org/apache/ibatis/submitted/integer_enum/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  builder.build(reader);
}
 
Example 7
Source File: DuplicateResourceTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example 8
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectKey() throws Exception {
  // this test checks to make sure that we can have select keys with the same
  // insert id in different namespaces
  String resource = "org/apache/ibatis/submitted/selectkey/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlMapper = builder.build(reader);
  assertNotNull(sqlMapper);
}
 
Example 9
Source File: RefidResolutionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = PersistenceException.class)
public void testIncludes() throws Exception {
  String resource = "org/apache/ibatis/submitted/refid_resolution/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlSessionFactory = builder.build(reader);
  sqlSessionFactory.getConfiguration().getMappedStatementNames();
}
 
Example 10
Source File: ExternalRefidResolutionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalRefAfterSelectKey() throws Exception {
  String resource = "org/apache/ibatis/submitted/refid_resolution/ExternalMapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlSessionFactory = builder.build(reader);
  reader.close();
  sqlSessionFactory.getConfiguration().getMappedStatementNames();
}
 
Example 11
Source File: MybatisManager.java    From grain with MIT License 5 votes vote down vote up
/**
 * 初始化链接mariadb
 * 
 * @param configDir
 *            地址
 * @param configName
 *            配置文件名
 * @param log
 *            日志可为null
 * @throws Exception
 */
public static void init(String configDir, String configName, ILog log) throws Exception {
	if (configDir == null || configDir.equals("")) {
		throw new Exception("配置文件信息为空");
	}
	MybatisManager.log = log;
	if (!configDir.endsWith("/") && !configDir.endsWith("\\")) {
		configDir += "/";
	}
	String xmlPath = configDir + configName;
	InputStream inputStream = new FileInputStream(xmlPath);
	SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
	sqlSessionFactory = builder.build(inputStream);
}
 
Example 12
Source File: MybatisSelectTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    SqlSessionFactory factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 13
Source File: RefidResolutionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = PersistenceException.class)
public void testIncludes() throws Exception {
  String resource = "org/apache/ibatis/submitted/refid_resolution/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlSessionFactory = builder.build(reader);
  sqlSessionFactory.getConfiguration().getMappedStatementNames();
}
 
Example 14
Source File: AccountTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    accountMapper = session.getMapper(AccountMapper.class);
}
 
Example 15
Source File: MybatisCacheTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 16
Source File: UserTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 17
Source File: DuplicateResourceTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example 18
Source File: SelectKeyTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectKey() throws Exception {
  // this test checks to make sure that we can have select keys with the same
  // insert id in different namespaces
  String resource = "org/apache/ibatis/submitted/selectkey/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  SqlSessionFactory sqlMapper = builder.build(reader);
  assertNotNull(sqlMapper);
}
 
Example 19
Source File: MybatisDynamicSqlTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    SqlSessionFactory factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 20
Source File: IntegerEnumTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseMapWithIntegerJdbcType() throws Exception {
  String resource = "org/apache/ibatis/submitted/integer_enum/MapperConfig.xml";
  Reader reader = Resources.getResourceAsReader(resource);
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  builder.build(reader);
}