MyBatis执行器

Posted GuanStudy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
  1. configuration我们读取配置文件使用SqlSessionFactoryBuilder来构建,而配置文件(Mybatis-config.xml)解析后就会将解析完的所有数据放到一个名为Configuration的类里面,我们的一些操作,比如设置Setting,设置数据源,设置映射文件,都可以通过new Configuration()来进行配置,而获取Configuration的实力,只需要我们SqlSessionFactoryBuilder.build()所创建的SqlSessionFactory就可以获取Configuration了--->SqlSessionFactory.getConfiguration()
  2. doQuery是BaseMapper的一个抽象方法,分别由三个子类进行实现,是最基本的查询方法,无论调用什么查询方法都会调用doQuery这个方法
  3. RowBounds分页条件,我们可以new RowBounds()来自定义分页条件,而RowBounds.DEFAULT就是new一个0-Integer.MAX_VALUE的RowBounds
  4. ResultHandler结果处理器
  5. BoundSql我们编写的sql语句,获取方法:ms.getBoundSql()没有参数就可以传null
  6. ms就是MappedStatement获取我们对应方法的属性,参数为statementid(包名.类名.方法名)

以上是关于MyBatis执行器的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis怎么防止SQL注入

mybatis学习(39):动态sql片段

mybatis以及预编译如何防止SQL注入

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

mybatis动态sql片段与分页,排序,传参的使用

MyBatis动态SQL标签用法