mybatis-plus查询源码详解

 更新时间:2022年03月02日 11:54:07   作者:这个世界太疯狂了  
这篇文章主要介绍了mybatis-plus查询源码解读,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

配置详情

pom.xml

dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.4.1</version>
 </dependency>

mapper

public interface GenTableMapper extends BaseMapper<GenTable> {
}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GendemoApplication.class)
public class BlockqueueTestDemo {
    @Autowired
    GenTableMapper genTableMapper;
    @Test
    public void test(){
        List<GenTable> genTables = 
        genTableMapper.selectList(new QueryWrapper<>());
    }
}

debug流程

1.发现 genTableMapper 是一个代理对象类型。

在这里插入图片描述

2.进入代理对象MybatisMapperProxy , 调用其invoke 方法,方法的Class类型为BaseMapper.selectList()

在这里插入图片描述

3.其中cachedInvoker()方法会返回一个PlainMethodInvoker ,它重写了MapperMethodInvoker 接口的invoke()方法

在这里插入图片描述

4.最终会调用MybatisMapperMethodexecute()方法

在这里插入图片描述

public class MybatisMapperMethod {
    public Object execute(SqlSession sqlSession, Object[] args) {
        Object result;
        switch (command.getType()) {
            case INSERT: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.insert(command.getName(), param));
                break;
            }
            case UPDATE: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.update(command.getName(), param));
                break;
            }
            case DELETE: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.delete(command.getName(), param));
                break;
            }
            case SELECT:
                if (method.returnsVoid() && method.hasResultHandler()) {
                    executeWithResultHandler(sqlSession, args);
                    result = null;
                } else if (method.returnsMany()) {
                    result = executeForMany(sqlSession, args);
                } else if (method.returnsMap()) {
                    result = executeForMap(sqlSession, args);
                } else if (method.returnsCursor()) {
                    result = executeForCursor(sqlSession, args);
                } else {
                    Object param = method.convertArgsToSqlCommandParam(args);
                    // TODO 这里下面改了
                    if (IPage.class.isAssignableFrom(method.getReturnType())) {
                        result = executeForIPage(sqlSession, args);
                        // TODO 这里上面改了
                    } else {
                        result = sqlSession.selectOne(command.getName(), param);
                        if (method.returnsOptional()
                            && (result == null || !method.getReturnType().equals(result.getClass()))) {
                            result = Optional.ofNullable(result);
                        }
                    }
                }
                break;
            case FLUSH:
                result = sqlSession.flushStatements();
                break;
            default:
                throw new BindingException("Unknown execution method for: " + command.getName());
        }
        if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
            throw new BindingException("Mapper method '" + command.getName()
                + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
        }
        return result;
    }
}

5.这是经过判断会进入executeForMany(sqlSession, args)方法,此时方法和参数都显示出来了。sqlSession的类型是SqlSessionTemplate , 为什么要注意这个 sqlSession的类型?因为SqlSession是一个接口,有很多实现类,有时候我们并不知道到底调用了哪个实现类的selectList()方法,这个时候我们看类型就知道了,就可以进入SqlSessionTemplate类,找到selectList()打上断点,debug就过来了。

在这里插入图片描述

6.利用同样的方法,又调用了DefaultSqlSessionselectList()方法。

在这里插入图片描述


7.来到DefaultSqlSessionselectList() 方法中,此时已经进入到mybatis的源码范围了。executor的类型是MybatisCachingExecutor

在这里插入图片描述

8.此时要注意MybatisCachingExecutor 代理类的handler是一个Plugin

在这里插入图片描述

在这里插入图片描述

9.因为我使用到了分页插件,所以会来到com.github.pagehelperPageInterceptor

在这里插入图片描述

10.由MybatisCachingExecutor来执行查询

在这里插入图片描述

11.MybatisCachingExecutor 委派 BaseExecutor 执行查询

在这里插入图片描述

在这里插入图片描述

12.最终委派到PreparedStatementHandler来处理

在这里插入图片描述

13.最后由DefaultResultSetHandler来封装结果集

@Override
  public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

    final List<Object> multipleResults = new ArrayList<>();
    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
      ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }
    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {
      while (rsw != null && resultSetCount < resultSets.length) {
        ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
        if (parentMapping != null) {
          String nestedResultMapId = parentMapping.getNestedResultMapId();
          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
          handleResultSet(rsw, resultMap, null, parentMapping);
        }
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
      }
    return collapseSingleResultList(multipleResults);
  }

总结

为什么要一层一层的封装?一层一层的委派?
这里面和缓存有关,有兴趣的可以自己了解一下。

下一篇打算记录一下 mybatis-plus 的自动配置过程,有兴趣的可以持续关注一下。
文中有错误的地方不吝赐教谢谢。

到此这篇关于mybatis-plus查询源码走读的文章就介绍到这了,更多相关mybatis-plus查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot+redis实现微博热搜排行榜的示例代码

    springboot+redis实现微博热搜排行榜的示例代码

    本文主要介绍了springboot+redis实现微博热搜排行榜的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Java进阶知识之反射的概念与获取方法

    Java进阶知识之反射的概念与获取方法

    这篇文章主要给大家介绍了关于Java进阶知识之反射的概念与获取方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • MyBatis配置文件解析与MyBatis实例演示

    MyBatis配置文件解析与MyBatis实例演示

    这篇文章主要介绍了MyBatis配置文件解析与MyBatis实例演示以及怎样编译安装MyBatis,需要的朋友可以参考下
    2022-04-04
  • Springboot日志开启SLF4J过程解析

    Springboot日志开启SLF4J过程解析

    这篇文章主要介绍了Springboot日志开启SLF4J过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • JAVA 根据Url把多文件打包成ZIP下载实例

    JAVA 根据Url把多文件打包成ZIP下载实例

    这篇文章主要介绍了JAVA 根据Url把多文件打包成ZIP下载的相关资料,需要的朋友可以参考下
    2017-08-08
  • Spring.Net控制反转IoC入门使用

    Spring.Net控制反转IoC入门使用

    这篇文章主要为大家详细介绍了Spring.Net控制反转IoC入门使用的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Spring boot 配置多个redis的方法示例

    Spring boot 配置多个redis的方法示例

    这篇文章主要介绍了Spring boot 配置多个redis的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Springboot中yml文件不生效原因分析及解决

    Springboot中yml文件不生效原因分析及解决

    这篇文章给大家介绍了Springboot中yml文件不生效原因分析及解决方法,通过图文给大家讲解的非常详细,对大家解决问题有一定的帮助,需要的朋友可以参考下
    2024-02-02
  • Mybatis实现自定义类型转换器TypeHandler的方法

    Mybatis实现自定义类型转换器TypeHandler的方法

    Mybatis实现自定义的转换器非常的简单,只需要三步就可以实现自定义类型转换器TypeHandler,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-07-07
  • SpringCloud之熔断监控Hystrix Dashboard的实现

    SpringCloud之熔断监控Hystrix Dashboard的实现

    这篇文章主要介绍了SpringCloud之熔断监控Hystrix Dashboard的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09

最新评论