测试类
@Test
public void testMyBatis() throws IOException {
//加载核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//获取sqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//获取mapper接口对象,帮助创建这个UserMapper的实现类,返回实现类对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//测试功能
int result = mapper.insertUser();
//提交事务
//sqlSession.commit();
System.out.println("result:"+result);
}
MyBatis框架在底层基于mapper接口动态创建实现类
//获取mapper接口对象,帮助创建这个UserMapper的实现类,返回实现类对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);