Mybatis-Plus(简称MP)是一个 Mybatis 的加强工具,那么它是怎么加强的呢?其实就是它曾经封装好了一些crud办法,开发就不须要再写xml了,间接调用这些办法就行,就相似于JPA。那么这篇文章就来浏览以下MP的具体实现,看看是怎么实现这些加强的。

入口类: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 脚本办法。

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实现动静脚本的要害类。

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的十多个类,次要如下图所示:

总体上来说,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实现动静语句的实现过程,并且给出一个可能的更便捷办法。