Mybatis採用責任鏈模式,通過動態代理組織多個插件(攔截器),通過這些插件可以改變Mybatis的默認行爲(諸如SQL重寫之類的),由於插件會深入到Mybatis的核心,因此在編寫自己的插件前最好了解下它的原理,以便寫出安全高效的插件。
概述
Mybatis插件又稱攔截器,本篇文章中出現的攔截器都表示插件。
Mybatis採用責任鏈模式,通過動態代理組織多個插件(攔截器),通過這些插件可以改變Mybatis的默認行爲(諸如SQL重寫之類的),由於插件會深入到Mybatis的核心,因此在編寫自己的插件前最好了解下它的原理,以便寫出安全高效的插件。
MyBatis 允許你在已映射語句執行過程中的某一點進行攔截調用。默認情況下,MyBatis 允許使用插件來攔截的方法調用包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
總體概括爲:
- 攔截執行器的方法
- 攔截參數的處理
- 攔截結果集的處理
- 攔截Sql語法構建的處理
Mybatis是通過動態代理的方式實現攔截的,閱讀此篇文章需要先對Java的動態代理機制有所瞭解。
Mybatis四大接口
既然Mybatis是對四大接口進行攔截的,那我們先要知道Mybatis的四大接口是哪些: Executor, StatementHandler, ResultSetHandler, ParameterHandler。
上圖Mybatis框架的整個執行過程。Mybatis插件能夠對這四大對象進行攔截,可以說包含到了Mybatis一次SQL執行的所有操作。可見Mybatis的的插件很強大。
- Executor是 Mybatis的內部執行器,它負責調用StatementHandler操作數據庫,並把結果集通過 ResultSetHandler進行自動映射,另外,他還處理了二級緩存的操作。從這裏可以看出,我們也是可以通過插件來實現自定義的二級緩存的。
- StatementHandler是Mybatis直接和數據庫執行sql腳本的對象。另外它也實現了Mybatis的一級緩存。這裏,我們可以使用插件來實現對一級緩存的操作(禁用等等)。
- ParameterHandler是Mybatis實現Sql入參設置的對象。插件可以改變我們Sql的參數默認設置。
- ResultSetHandler是Mybatis把ResultSet集合映射成POJO的接口對象。我們可以定義插件對Mybatis的結果集自動映射進行修改。
插件Interceptor
Mybatis的插件實現要實現Interceptor接口,我們看下這個接口定義的方法。
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
這個接口只聲明瞭三個方法:
- setProperties方法是在Mybatis進行配置插件的時候可以配置自定義相關屬性,即:接口實現對象的參數配置。
- plugin方法是插件用於封裝目標對象的,通過該方法我們可以返回目標對象本身,也可以返回一個它的代理,可以決定是否要進行攔截進而決定要返回一個什麼樣的目標對象,官方提供了示例:return Plugin.wrap(target, this)。
- intercept方法就是要進行攔截的時候要執行的方法。
理解這個接口的定義,先要知道java動態代理機制。plugin接口即返回參數target對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。在調用對應對象的接口的時候,可以進行攔截並處理。
Mybatis四大接口對象創建方法
Mybatis的插件是採用對四大接口的對象生成動態代理對象的方法來實現的。那麼現在我們看下Mybatis是怎麼創建這四大接口對象的。
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
//確保ExecutorType不爲空(defaultExecutorType有可能爲空)
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor; if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
} if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}
查看源碼可以發現, Mybatis框架在創建好這四大接口對象的實例後,都會調用InterceptorChain.pluginAll()方法。InterceptorChain對象是插件執行鏈對象,看源碼就知道里面維護了Mybatis配置的所有插件(Interceptor)對象。
// target --> Executor/ParameterHandler/ResultSetHander/StatementHandler
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
其實就是按順序執行我們插件的plugin方法,一層一層返回我們原對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。當我們調用四大接口的方法的時候,實際上是調用代理對象的相應方法,代理對象又會調用四大接口的實例。
Plugin對象
我們知道,官方推薦插件實現plugin方法爲:Plugin.wrap(target, this);
public static Object wrap(Object target, Interceptor interceptor) {
// 獲取插件的Intercepts註解
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap));
}
return target;
}
這個方法其實是Mybatis簡化我們插件實現的工具方法。其實就是根據當前攔截的對象創建了一個動態代理對象。代理對象的InvocationHandler處理器爲新建的Plugin對象。
插件配置註解@Intercepts
Mybatis的插件都要有Intercepts註解來指定要攔截哪個對象的哪個方法。我們知道,Plugin.warp方法會返回四大接口對象的代理對象(通過new Plugin()創建的IvocationHandler處理器),會攔截所有的執行方法。在代理對象執行對應方法的時候,會調用InvocationHandler處理器的invoke方法。Mybatis中利用了註解的方式配置指定攔截哪些方法。具體如下:
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);
}
}
可以看到,只有通過Intercepts註解指定的方法纔會執行我們自定義插件的intercept方法。未通過Intercepts註解指定的將不會執行我們的intercept方法。
官方插件開發方式
@Intercepts({@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class TestInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget(); //被代理對象
Method method = invocation.getMethod(); //代理方法
Object[] args = invocation.getArgs(); //方法參數
// do something ...... 方法攔截前執行代碼塊
Object result = invocation.proceed();
// do something .......方法攔截後執行代碼塊
return result;
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
}
以上就是Mybatis官方推薦的插件實現的方法,通過Plugin對象創建被代理對象的動態代理對象。可以發現,Mybatis的插件開發還是很簡單的。
自定義開發方式
Mybatis的插件開發通過內部提供的Plugin對象可以很簡單的開發。只有理解了插件實現原理,對應不採用Plugin對象我們一樣可以自己實現插件的開發。下面是我個人理解之後的自己實現的一種方式。
public class TestInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget(); //被代理對象
Method method = invocation.getMethod(); //代理方法
Object[] args = invocation.getArgs(); //方法參數
// do something ...... 方法攔截前執行代碼塊
Object result = invocation.proceed();
// do something .......方法攔截後執行代碼塊
return result;
}
public Object plugin(final Object target) {
return Proxy.newProxyInstance(Interceptor.class.getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return intercept(new Invocation(target, method, args));
}
});
}
public void setProperties(Properties properties) {
}
}
當然,Mybatis插件的那這個時候Intercepts的註解起不到作用了。
小結
我們在MyBatis配置了一個插件,在運行發生了什麼
- 所有可能被攔截的處理類都會生成一個代理
- 處理類代理在執行對應方法時,判斷要不要執行插件中的攔截方法
- 執行插接中的攔截方法後,推進目標的執行
如果有N個插件,就有N個代理,每個代理都要執行上面的邏輯。這裏面的層層代理要多次生成動態代理,是比較影響性能的。雖然能指定插件攔截的位置,但這個是在執行方法時動態判斷,初始化的時候就是簡單的把插件包裝到了所有可以攔截的地方。
因此,在編寫插件時需注意以下幾個原則:
- 不編寫不必要的插件;
- 實現plugin方法時判斷一下目標類型,是本插件要攔截的對象才執行Plugin.wrap方法,否者直接返回目標本身,這樣可以減少目標被代理的次數。
參考: