Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?

本文解析了 MyBatis Plus (MP) 的核心实现原理,特别是其如何通过自定义动态 SQL 语句增强原有 MyBatis 框架。通过分析 MP 的关键类如 MybatisSqlSessionFactoryBuilder 和 MybatisMapperAnnotationBuilder,了解 MP 如何注册和构建动态 SQL 方法。

点击上方“芋道源码”,选择“设为星标

管她前浪,还是后浪?

能浪的浪,才是好浪!

每天 10:33 更新文章,每天掉亿点点头发...

源码精品专栏

 

来源:juejin.cn/post/
6883081187103866894


Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,开发就不需要再写xml了,直接调用这些方法就行,就类似于JPA。那么这篇文章就来阅读以下MP的具体实现,看看是怎样实现这些增强的。

9a664e9a18337b7ea6d91fa7a054d0cc.png

入口类:MybatisSqlSessionFactoryBuilder

通过在入口类 MybatisSqlSessionFactoryBuilder#build方法中, 在应用启动时, 将mybatis plus(简称MP)自定义的动态配置xml文件注入到Mybatis中。

public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
    public SqlSessionFactory build(Configuration configuration) {
            // ... 省略若干行 
            if (globalConfig.isEnableSqlRunner()) {
                new SqlRunnerInjector().inject(configuration);
            }
            // ... 省略若干行 
            return sqlSessionFactory;
        }
}

这里涉及到2个MP2个功能类

  • 扩展继承自Mybatis的MybatisConfiguration类: MP动态脚本构建,注册,及其它逻辑判断。

  • SqlRunnerInjector: MP默认插入一些动态方法的xml 脚本方法。

推荐下自己做的 Spring Boot 的实战项目:

https://github.com/YunaiV/ruoyi-vue-pro

MybatisConfiguration类

这里我们重点剖析MybatisConfiguration类,在MybatisConfiguration中,MP初始化了其自身的MybatisMapperRegistry,而MybatisMapperRegistry是MP加载自定义的SQL方法的注册器。

MybatisConfiguration中很多方法是使用MybatisMapperRegistry进行重写实现

其中有3个重载方法addMapper实现了注册MP动态脚本的功能。

public class MybatisConfiguration extends Configuration {
    /**
     * Mapper 注册
     */
    protected final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);
    // ....

    /**
     * 初始化调用
     */
    public MybatisConfiguration() {
        super();
        this.mapUnderscoreToCamelCase = true;
        languageRegistry.setDefaultDriverClass(MybatisXMLLanguageDriver.class);
    }

    /**
     * MybatisPlus 加载 SQL 顺序:
     * <p> 1、加载 XML中的 SQL </p>
     * <p> 2、加载 SqlProvider 中的 SQL </p>
     * <p> 3、XmlSql 与 SqlProvider不能包含相同的 SQL </p>
     * <p>调整后的 SQL优先级:XmlSql > sqlProvider > CurdSql </p>
     */
    @Override
    public void addMappedStatement(MappedStatement ms) {
        // ...
    }
    
    // ... 省略若干行 
    /**
     * 使用自己的 MybatisMapperRegistry
     */
    @Override
    public <T> void addMapper(Class<T> type) {
        mybatisMapperRegistry.addMapper(type);
    }
    // .... 省略若干行 
}

在MybatisMapperRegistry中,MP将mybatis的MapperAnnotationBuilder替换为MP自己的MybatisMapperAnnotationBuilder

public class MybatisMapperRegistry extends MapperRegistry {
    @Override
    public <T> void addMapper(Class<T> type) {
        // ... 省略若干行 
        MybatisMapperAnnotationBuilder parser = new MybatisMapperAnnotationBuilder(config, type);
        parser.parse();
        // ... 省略若干行 
    }
}

在MybatisMapperRegistry类的addMapper方法中,真正进入到MP的核心类MybatisMapperAnnotationBuilder,MybatisMapperAnnotationBuilder这个类是MP实现动态脚本的关键类。

推荐下自己做的 Spring Cloud 的实战项目:

https://github.com/YunaiV/onemall

MybatisMapperAnnotationBuilder动态构造

在MP的核心类MybatisMapperAnnotationBuilder的parser方法中,MP逐一遍历要加载的Mapper类,加载的方法包括下面几个

public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
    @Override
    public void parse() {
        //... 省略若干行 
        for (Method method : type.getMethods()) {
            /** for循环代码, MP判断method方法是否是@Select @Insert等mybatis注解方法**/
            parseStatement(method);
            InterceptorIgnoreHelper.initSqlParserInfoCache(cache, mapperName, method);
            SqlParserHelper.initSqlParserInfoCache(mapperName, method);
        }
        /** 这2行代码, MP注入默认的方法列表**/
        if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) {
            GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type);
        }
        //... 省略若干行 
    }

    @Override
    public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
        Class<?> modelClass = extractModelClass(mapperClass);
        //... 省略若干行 
        List<AbstractMethod> methodList = this.getMethodList(mapperClass);
        TableInfo tableInfo = TableInfoHelper.initTableInfo(builderAssistant, modelClass);
        // 循环注入自定义方法
        methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo));
        mapperRegistryCache.add(className);
    }
}
public class DefaultSqlInjector extends AbstractSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        return Stream.of(
            new Insert(),
            //... 省略若干行 
            new SelectPage()
        ).collect(toList());
    }
}

在MybatisMapperAnnotationBuilder中,MP真正将框架自定义的动态SQL语句注册到Mybatis引擎中。而AbstractMethod则履行了具体方法的SQL语句构造。

具体的AbstractMethod实例类,构造具体的方法SQL语句

以 SelectById 这个类为例说明下

/**
 * 根据ID 查询一条数据
 */
public class SelectById extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        /** 定义 mybatis xml method id, 对应 <id="xyz"> **/
        SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID;
        /** 构造id对应的具体xml片段 **/
        SqlSource sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(),
            sqlSelectColumns(tableInfo, false),
            tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
            tableInfo.getLogicDeleteSql(true, true)), Object.class);
        /** 将xml method方法添加到mybatis的MappedStatement中 **/
        return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
    }
}

至此,MP完成了在启动时加载自定义的方法xml配置的过程,后面的就是mybatis ${变量} #{变量}的动态替换和预编译,已经进入mybatis自有功能。

总结一下

MP总共改写和替换了mybatis的十多个类,主要如下图所示:

6dd64d1e1263d4551bad24f828dee05a.png

总体上来说,MP实现mybatis的增强,手段略显繁琐和不够直观,其实根据MybatisMapperAnnotationBuilder构造出自定义方法的xml文件,将其转换为mybatis的Resource资源,可以只继承重写一个Mybatis类:SqlSessionFactoryBean 比如如下:

public class YourSqlSessionFactoryBean extends SqlSessionFactoryBean implements ApplicationContextAware {

    private Resource[] mapperLocations;

    @Override
    public void setMapperLocations(Resource... mapperLocations) {
        super.setMapperLocations(mapperLocations);
        /** 暂存使用mybatis原生定义的mapper xml文件路径**/
        this.mapperLocations = mapperLocations;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        /** 只需要通过将自定义的方法构造成xml resource和原生定义的Resource一起注入到mybatis中即可, 这样就可以实现MP的自定义动态SQL和原生SQL的共生关系**/
        this.setMapperLocations(InjectMapper.getMapperResource(this.dbType, beanFactory, this.mapperLocations));
        super.afterPropertiesSet();
    }
}

在这边文章中,简单介绍了MP实现动态语句的实现过程,并且给出一个可能的更便捷方法。

- END -

欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢

50b816cfd2de78fe3b77a46c2bf6ba1f.png

已在知识星球更新源码解析如下:

fa887dbef4015bf302969bc18fdd85fb.png

b4fe0029a862671ff35b247a257f21e2.png

ffd9309c6bcb7bc8f738d017b8e3bb4d.png

e80f57a64e7a0eeb5d41136500651ee7.png

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值