mybatis-plugin插件執(zhí)行原理解析
mybatis-plugin插件執(zhí)行原理
今天主要是在看mybatis的主流程源碼,其中比較感興趣的是mybatis的plugin功能,這里主要記錄下mybatis-plugin的插件功能原理。
plugin集合列表:在構(gòu)建SqlSessionFactory時,通過解析配置或者plugin-bean的注入,會將所有的mybatis-plugin都收集到Configuration
對象的interceptorChain屬性中。InterceptorChain類定義如下:
public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<>();
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
public List<Interceptor> getInterceptors() {
return Collections.unmodifiableList(interceptors);
}
}plugin作用對象:Executor,ParameterHandler,ResultSetHandler,StatementHandler,這4個對象在mybatis執(zhí)行sql的過程中
有不同的作用。
Executor:sql執(zhí)行的具體操作對象。
ParameterHandler:sql執(zhí)行前的參數(shù)處理對象。
ResultSetHandler:sql執(zhí)行后的結(jié)果集處理對象。
StatementHandler:具體送到數(shù)據(jù)庫執(zhí)行的sql操作對象。
plugin作用原理:類似AOP,使用JDK動態(tài)代理,只不過mybatis的增強對象不是所有對象,而是上面陳列的4個對象而已。
在4個對象創(chuàng)建時,都會對各個對象進行判斷,是否需要進行插件化。比如下面的插件:
@Intercepts({@Signature( type= Executor.class, method = "query", args ={
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class
})})
public class ExamplePlugin implements Interceptor {
// 分頁 讀寫分離 Select 增刪改
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("代理");
Object[] args = invocation.getArgs();
MappedStatement ms= (MappedStatement) args[0];
// 執(zhí)行下一個攔截器、直到盡頭
return invocation.proceed();
}
}該插件將會在Executor該對象創(chuàng)建時,使用該插件進行增強。在新開一個sqlSession時,將會創(chuàng)建Executor對象。跟蹤到具體方法:
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
/**
* 判斷執(zhí)行器的類型
* 批量的執(zhí)行器
*/
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
//可重復使用的執(zhí)行器
executor = new ReuseExecutor(this, transaction);
} else {
//簡單的sql執(zhí)行器對象
executor = new SimpleExecutor(this, transaction);
}
//判斷mybatis的全局配置文件是否開啟緩存
if (cacheEnabled) {
//把當前的簡單的執(zhí)行器包裝成一個CachingExecutor
executor = new CachingExecutor(executor);
}
/**
* TODO:調(diào)用所有的攔截器對象plugin方法
* 插件: 責任鏈+ 裝飾器模式(動態(tài)代理)
*/
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}我們找到interceptorChain.pluginAll方法:
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}發(fā)現(xiàn)會通過已加載的所有plugin列表中,逐個遍歷去篩選出符合Executor類型的插件,再通過具體插件的interceptor.plugin方法去創(chuàng)建
Executor的代理對象。
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
default Object plugin(Object target) {
return Plugin.wrap(target, this);
}
default void setProperties(Properties properties) {
// NOP
}
}再看到具體的Plugin.wrap(target, this)方法:
public static Object wrap(Object target, Interceptor interceptor) {
// 獲得interceptor配置的@Signature的type
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
// 當前代理類型
Class<?> type = target.getClass();
// 根據(jù)當前代理類型 和 @signature指定的type進行配對, 配對成功則可以代理
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}這里我們就很清楚了,通過@Signature注解上的type、method、args屬性去匹配,如果找到符合的,就會為對象創(chuàng)建代理對象,并返回代理對象。
責任鏈設(shè)計模式:因為一個增強對象可能會有多個plugin的增強邏輯,所以在執(zhí)行的時候使用的是責任鏈設(shè)計模式。

因為Plugin.wrap()方法新建的代理對象中使用的InvocationHandler對象是Plugin本身,所以在執(zhí)行方法的時候首先要調(diào)用它的invoke方法,
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}當我們執(zhí)行Executor的query方法時,符合if (methods != null && methods.contains(method)) {條件,這時就會去執(zhí)行具體插件的增強方法,interceptor.intercept,
然后再通過傳遞new Invocation(target, method, args)對象,在插件執(zhí)行完之后,再調(diào)用invocation.proceed()去執(zhí)行下一個插件邏輯。
如下是對Executor的query方法添加了2個插件的場景:

總結(jié):如果我們的業(yè)務需要我們?nèi)ゾ帉憇ql插件,那我們就需要來研究下Executor,ParameterHandler,ResultSetHandler,StatementHandler這4個對象的具體跟sql相關(guān)的方法,
然后再進行修改,就可以直接起到aop的作用。
到此這篇關(guān)于mybatis-plugin插件執(zhí)行原理的文章就介紹到這了,更多相關(guān)mybatis-plugin插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用觀察者模式實現(xiàn)氣象局高溫預警功能示例
這篇文章主要介紹了Java使用觀察者模式實現(xiàn)氣象局高溫預警功能,結(jié)合完整實例形式分析了java觀察者模式實現(xiàn)氣象局高溫預警的相關(guān)接口定義、使用、功能操作技巧,并總結(jié)了其設(shè)計原則與適用場合,具有一定參考借鑒價值,需要的朋友可以參考下2018-04-04
在Java的Hibernate框架中對數(shù)據(jù)庫數(shù)據(jù)進行查詢操作
這篇文章主要介紹了Java的Hibernate框架中對數(shù)據(jù)庫數(shù)據(jù)進行查詢操作的方法,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
Java中字符串和byte數(shù)組之間的簡單轉(zhuǎn)換方法
這篇文章主要給大家介紹了關(guān)于Java中字符串和byte數(shù)組之間的簡單轉(zhuǎn)換方法,Java中將String類型轉(zhuǎn)換為byte[]類型,可以使用String的getBytes()方法,還有很多其他的辦法,需要的朋友可以參考下2023-08-08
Maven項目在new module后,pom文件顯示為Ignored pom.xml問題
在Maven項目中,若創(chuàng)建過同名module后刪除,再次創(chuàng)建時可能導致pom.xml文件被IDEA忽略,原因是IDEA保留了之前module的痕跡,導致重建時將其視為已刪除的module,解決方法是進入IDEA設(shè)置,找到Maven的Ignored Files設(shè)置2024-09-09
java基礎(chǔ)篇之Date類型最常用的時間計算(相當全面)
這篇文章主要給大家介紹了關(guān)于java基礎(chǔ)篇之Date類型最常用的時間計算的相關(guān)資料,Java中的Date類是用來表示日期和時間的類,它提供了一些常用的方法來處理日期和時間的操作,需要的朋友可以參考下2023-12-12
Java使用Scanner類進行控制臺輸入實現(xiàn)方法
這篇文章主要介紹了Java使用Scanner類進行控制臺輸入實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12

