更新時間:2021-07-16 15:56:40 來源:動力節點 瀏覽1115次
當我們使用Mybatis的時候,總會使用到各種插件,如PageHelper(分頁插件)等,當我們需要自定義插件來改變,就必須了解插件的實現原理。
Mybatis插件又稱攔截器,Mybatis采用責任鏈模式,通過動態代理組織多個插件(攔截器),通過這些插件可以改變Mybatis的默認行為。MyBatis允許你在已映射語句執行過程中的某一點進行攔截調用。默認情況下,MyBatis允許使用插件來攔截的方法調用包括:
上圖Mybatis框架的整個執行過程。Mybatis插件能夠對則四大對象進行攔截,可以包含到了Mybatis一次會議的所有操作。可見Mybatis的的插件很強大。
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;
}
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 Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
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, autoCommit);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
這4個方法實例化了對應的對象之后,都會調用interceptorChain的pluginAll方法,那么下面我們在來看pluginAll干了什么
public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
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);
}
}
原來這個pluginAll方法就是遍歷所有的攔截器,然后順序執行我們插件的plugin方法,一層一層返回我們原對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。當我們調用四大接口對象的方法時候,實際上是調用代理對象的響應方法,代理對象又會調用四大接口對象的實例。
攔截器Interceptor
Mybatis的插件實現要實現Interceptor接口,我們看下這個接口定義的方法。
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
這個接口只聲明了三個方法。
官方推薦插件開發方式
@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插件
注:MyBatis默認沒有一個攔截器接口的實現類,開發者可以實現符合自己需求的攔截器。
下面的MyBatis官網的一個攔截器實例:
@Intercepts({@Signature(type= Executor.class, method = "update", args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
全局xml配置:
<plugins>
<plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>
這個攔截器攔截Executor接口的update方法(其實也就是SqlSession的新增,刪除,修改操作),所有執行executor的update方法都會被該攔截器攔截到。
以上就是動力節點小編介紹的"Mybatis插件原理",希望對大家有幫助,想了解更多可查看Mybatis視頻教程。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的學習和了解Java編程,感興趣的同學可以關注一下。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習