mybatis执行流程
三个文件:
Mapper接口
Mybatis核心配置文件(配置了数据源和映射文件)
映射文件(包含了具体的sql语句)
测试代码
public class testMybatis {
@Test
public void testmybatis() throws IOException {
//加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory SessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
//获取mybatis操作数据库的会话对象Sqlsession对象
SqlSession sqlSession = SessionFactory.openSession();
//获取mapper接口对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//do sql
int res = mapper.insertUser();
//提交事务
sqlSession.commit();
System.out.println("res:"+res);
}
}
执行顺序
sqlSession包含了数据源和映射文件(包含了mapper接口)
当mybatis拿到接口的class文件以后,mybatis会结合映射文件生成一个mapper接口的实现类对象
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/269710.html