MyBatis执行器
作者:互联网
Mybatis的执行器
下面先展示一张图,用来说明一下Mybatis执行器的整体架构
SimpleExecutor
首先SimpleExecutor是我们最常使用的一个执行器,无论我们执行什么方法默认调用的都是SimpleExecutor
下面是基本使用,这里可能会比较懵了,哪里来的configuration,doQuery,RowBounds,ResultHandler,BoundSql
在这里我来一一解释
SimpleExecutor simpleExecutor = new SimpleExecutor(configuration, transaction);
MappedStatement ms = configuration.getMappedStatement("com.guan.ibatis.mapper.UserMapper.queryUsersInfo");
BoundSql boundSql = ms.getBoundSql(null);
List<User> users = simpleExecutor.doQuery(ms, null,
RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER, boundSql);
users.forEach(System.out::println);
- configuration我们读取配置文件使用SqlSessionFactoryBuilder来构建,而配置文件(Mybatis-config.xml)解析后就会将解析完的所有数据放到一个名为Configuration的类里面,我们的一些操作,比如设置Setting,设置数据源,设置映射文件,都可以通过
new Configuration()
来进行配置,而获取Configuration的实力,只需要我们SqlSessionFactoryBuilder.build()所创建的SqlSessionFactory就可以获取Configuration了--->SqlSessionFactory.getConfiguration()
- doQuery是BaseMapper的一个抽象方法,分别由三个子类进行实现,是最基本的查询方法,无论调用什么查询方法都会调用doQuery这个方法
- RowBounds分页条件,我们可以new RowBounds()来自定义分页条件,而RowBounds.DEFAULT就是new一个0-Integer.MAX_VALUE的RowBounds
- ResultHandler结果处理器
- BoundSql我们编写的sql语句,获取方法:
ms.getBoundSql()
没有参数就可以传null - ms就是MappedStatement获取我们对应方法的属性,参数为statementid(包名.类名.方法名)
标签:执行器,SimpleExecutor,ms,MyBatis,new,Configuration,RowBounds 来源: https://www.cnblogs.com/GuanStudy/p/14839035.html