Mybatis-Plus(簡稱MP)是一個 Mybatis 的增強工具,那麼它是怎麼增強的呢?其實就是它已經封裝好了一些crud方法,開發就不需要再寫xml了,直接調用這些方法就行,就類似于JPA。
那麼這篇文章就來閱讀以下MP的具體實作,看看是怎樣實作這些增強的。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SN1QGZyY2NiNWY3gDNmZmYxkTY0IWM5I2Y4UjNhVGO58CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.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 腳本方法。
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 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實作動态語句的實作過程,并且給出一個可能的更便捷方法。