Mybatis定義了四種攔截器:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
這四個類中的每一個方法都可以被攔截。
總體概括為:
- 攔截執行器的方法
- 攔截參數的處理
- 攔截結果集的處理
- 攔截Sql語法構建的處理
示例:
@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>
?