> 文档中心 > 【mybatis plus源码解析】(一)mybatis plus执行原理,mybatis plus是如何实现自动注入CRUD操作

【mybatis plus源码解析】(一)mybatis plus执行原理,mybatis plus是如何实现自动注入CRUD操作


系列文章目录

【mybatis plus源码解析】(一)mybatis plus执行原理,mybatis plus是如何实现自动注入CRUD操作
【mybatis plus源码解析】(二)详解SQL注入器底层原理,mybatis plus是如何实现自动注入CRUD操作

文章目录

  • 系列文章目录
  • 前言
  • 一、mybatis plus是什么?
  • 二、搭建测试环境,运行代码看流程
    • 三,来看看mybatisXMLConfigBuilder.parse()方法都做了啥
    • 关于mybatis动态注入相关类的uml图

前言

前面文章讲过mybatis相关源码理解,感兴趣的可以看看我之前的文章。这回顺道看看mybatis plus的源码

一、mybatis plus是什么?

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
支持的数据库
任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。

MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb

达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库

二、搭建测试环境,运行代码看流程

在这里插入图片描述
这里用经典的一套执行流程
打断点到这一步
在这里插入图片描述
发现配置类中mappedStatements对象已经放入执行相关方法sql映射语句。这里其实能猜出个大概,其实就是在初始化配置的时候mybatis plus已经为我们封装好了默认方法sql映射语句。这也就是为什么只要继承BaseMapper接口我们就能使用selectMaps,selectById等等相关的方法。

三,来看看mybatisXMLConfigBuilder.parse()方法都做了啥

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
到这一步停下来,发现调用了configuration.addMapper(boundType);方法。这个configuration是mybatis plus继承mybatis原有的Configuration类的新类MybatisConfiguration。所以我们进入mybatisConfigurationaddMapper(boundType)方法看看
【mybatis plus源码解析】(一)mybatis plus执行原理,mybatis plus是如何实现自动注入CRUD操作
又调用MybatisMapperRegistry的addMapper方法,继续进入看看

    @Override    public <T> void addMapper(Class<T> type) { if (type.isInterface()) {     if (hasMapper(type)) {  // TODO 如果之前注入 直接返回  return;  // TODO 这里就不抛异常了//  throw new BindingException("Type " + type + " is already known to the MapperRegistry.");     }     boolean loadCompleted = false;     try {  // TODO 这里也换成 MybatisMapperProxyFactory 而不是 MapperProxyFactory  knownMappers.put(type, new MybatisMapperProxyFactory<>(type));  // It's important that the type is added before the parser is run  // otherwise the binding may automatically be attempted by the  // mapper parser. If the type is already known, it won't try.  // TODO 这里也换成 MybatisMapperAnnotationBuilder 而不是 MapperAnnotationBuilder  MybatisMapperAnnotationBuilder parser = new MybatisMapperAnnotationBuilder(config, type);  parser.parse();  loadCompleted = true;     } finally {  if (!loadCompleted) {      knownMappers.remove(type);  }     } }    }

发现创建了MybatisMapperAnnotationBuilder建造者并解析
来看看MybatisMapperAnnotationBuilder的parse()方法

    @Override    public void parse() { String resource = type.toString(); if (!configuration.isResourceLoaded(resource)) {     loadXmlResource();     configuration.addLoadedResource(resource);     String mapperName = type.getName();     assistant.setCurrentNamespace(mapperName);     parseCache();     parseCacheRef();     InterceptorIgnoreHelper.InterceptorIgnoreCache cache = InterceptorIgnoreHelper.initSqlParserInfoCache(type);     for (Method method : type.getMethods()) {  if (!canHaveStatement(method)) {      continue;  }  if (getAnnotationWrapper(method, false, Select.class, SelectProvider.class).isPresent()      && method.getAnnotation(ResultMap.class) == null) {      parseResultMap(method);  }  try {      // TODO 加入 注解过滤缓存      InterceptorIgnoreHelper.initSqlParserInfoCache(cache, mapperName, method);      parseStatement(method);  } catch (IncompleteElementException e) {      // TODO 使用 MybatisMethodResolver 而不是 MethodResolver      configuration.addIncompleteMethod(new MybatisMethodResolver(this, method));  }     }     // TODO 注入 CURD 动态 SQL , 放在在最后, because 可能会有人会用注解重写sql     try {  // https://github.com/baomidou/mybatis-plus/issues/3038  if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) {      parserInjector();  }     } catch (IncompleteElementException e) {  configuration.addIncompleteMethod(new InjectorResolver(this));     } } parsePendingMethods();    }

在这里插入图片描述
这里就加入mybatisplus自己的动态sql
执行完这个语句后配置当中已经有相关动态语句。

关于mybatis动态注入相关类的uml图

在这里插入图片描述
ISqlInjector顶级接口
DefaultSqlInjectorSQL 默认注入器
在这里插入图片描述篇幅原因后续关于动态sql这块留到下篇文章讲解