mybatis的插件开发:主要就是实现拦截器,mybatis在创建四大神器的时候会将我们的拦截器逻辑通过代理的方式植入进去。
mybatis在创建Executor,StatmentHandler,ParameterHandler,ResultHandler这四个对象的时候都会有这个处理逻辑: interceptorChain.pluginAll(resultSetHandler)
下面看其中两个:
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 Object pluginAll(Object target) {
//遍历拦截器,然后执行拦截器的plugin方法,最后返回target对象
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
下面我们就实现一个拦截器:
1.实现Interceptor接口
@Intercepts(value= {@Signature( //拦截Statmenthandler的parameterize方法,这个方法需要参数类型Statment
type=StatementHandler.class,
method="parameterize",
args= {Statement.class}
)})
public class CustomerInterceptor implements Interceptor{
//拦截到的处理逻辑 相当于invoke方法
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("CustomerInterceptor拦截到,前置处理。。。");
Object resultObj = invocation.proceed();
System.out.println("CustomerInterceptor拦截到,后置处理。。。");
return resultObj;
}
//为target对象生成代理对象 相当于Proxy.newInstanceProxy(classLoader,interfaces,代理实现类)
public Object plugin(Object target) {
//Plugin 获取被@Intercepts注解的类,然后调用Proxy.newInstanceProxy生成代理
Object proxyObj = Plugin.wrap(target, this);
return proxyObj;
}
//为拦截器对象属性进行赋值
public void setProperties(Properties properties) {
String test = properties.getProperty("test");
System.out.println("CurtomerInterceptor 参数值:"+test);
}
}
2.在mybatis配置文件中配置拦截器
<plugins>
<plugin interceptor="stu.wf.interceptor.CustomerInterceptor">
<property name="test" value="stu.wf"/>
</plugin>
</plugins>
大体的步骤就是这个样子,在我们工作中用到的话,处理逻辑就按需求来咯。
来源:CSDN
作者:fly丶
链接:https://blog.csdn.net/qq_32314335/article/details/103460912