GuanStudy

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(包名.类名.方法名)

分类:

技术点:

相关文章:

  • 2022-01-05
  • 2020-03-18
  • 2021-09-22
  • 2021-10-03
猜你喜欢
  • 2022-12-23
  • 2021-08-07
  • 2021-07-24
  • 2022-02-04
  • 2022-12-23
  • 2020-05-30
  • 2021-05-28
相关资源
相似解决方案