一、概述
Mybatis采用责任链模式,通过动态代理组织多个插件(拦截器),通过这些插件可以改变Mybatis的默认行为(诸如SQL重写之类的)
Mybatis是通过动态代理的方式实现拦截的
拦截器(Interceptor)在 Mybatis 中被当做插件(plugin)对待,官方文档提供了 Executor(拦截执行器的方法),ParameterHandler(拦截参数的处理),ResultSetHandler(拦截结果集的处理),StatementHandler(拦截Sql语法构建的处理) 共4种,并且提示“这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 发行包中的源代码”。
拦截器的使用场景主要是更新数据库的通用字段,分库分表,加解密等的处理。
MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦截器功能。
MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed):拦截执行器的方法
Mybatis的内部执行器,它负责调用StatementHandler操作数据库,并把结果集通过 ResultSetHandler进行自动映射,另外,他还处理了二级缓存的操作。从这里可以看出,我们也是可以通过插件来实现自定义的二级缓存的。
ParameterHandler (getParameterObject, setParameters):拦截参数的处理
是Mybatis实现Sql入参设置的对象。插件可以改变我们Sql的参数默认设置。
ResultSetHandler (handleResultSets, handleOutputParameters):拦截结果集的处理
是Mybatis把ResultSet集合映射成POJO的接口对象。我们可以定义插件对Mybatis的结果集自动映射进行修改。
StatementHandler (prepare, parameterize, batch, update, query):拦截Sql语法构建的处理
是Mybatis直接和数据库执行sql脚本的对象。另外它也实现了Mybatis的一级缓存。这里,我们可以使用插件来实现对一级缓存的操作(禁用等等)。
Mybatis插件能够对这四大对象进行拦截,可以说包含到了Mybatis一次SQL执行的所有操作
二、 原理
Mybatis的拦截器实现机制,使用的是JDK的InvocationHandler.
当我们调用ParameterHandler,ResultSetHandler,StatementHandler,Executor的对象的时候,实际上使用的是Plugin这个代理类的对象,这个类实现了InvocationHandler接口
接下来我们就知道了,在调用上述被代理类的方法的时候,就会执行Plugin的invoke方法.
Plugin在invoke方法中根据@Intercepts的配置信息(方法名,参数等)动态判断是否需要拦截该方法.
再然后使用需要拦截的方法Method封装成Invocation,并调用Interceptor的proceed方法.
这样我们就达到了拦截目标方法的结果.
例如Executor的执行大概是这样的流程:
拦截器代理类对象->拦截器->目标方法
Executor->Plugin->Interceptor->Invocation
Executor.Method->Plugin.invoke->Interceptor.intercept->Invocation.proceed->method.invoke
2.1、拦截器接口
拦截器均需要实现该 org.apache.ibatis.plugin.Interceptor 接口
public interfaceInterceptor {
Object intercept(Invocation invocation)throwsThrowable;
Object plugin(Object target);voidsetProperties(Properties properties);
}
有3个方法。 MyBatis默认没有一个拦截器接口的实现类
setProperties方法是在Mybatis进行配置插件的时候可以配置自定义相关属性,即:接口实现对象的参数配置。
plugin方法是插件用于封装目标对象的,通过该方法我们可以返回目标对象本身,也可以返回一个它的代理,可以决定是否要进行拦截进而决定要返回一个什么样的目标对象,官方提供了示例:return Plugin.wrap(target, this)。
理解这个接口的定义,先要知道java动态代理机制。plugin接口即返回参数target对象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理对象。在调用对应对象的接口的时候,可以进行拦截并处理。
intercept方法就是要进行拦截的时候要执行的方法。
2.2、拦截器四大接口实例
上述的Executor、ParameterHandler、ResultSetHandler、StatementHandler具体实现
2.2.1、实例创建
publicExecutor 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(