程序员

MyBatis之插件(plugins)

2019-11-04  本文已影响0人  随遇而安_90d2

MyBatis 允许在已映射语句执行过程中的某一点进行拦截调用。
MyBatis 拦截器设计的一个初衷就是为了供用户在某些时候可以实现自己的逻辑而不必去动MyBatis 固有的逻辑。

默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

对于拦截器MyBatis 为我们提供了一个Interceptor接口,通过实现该接口就可以定义我们自己的拦截器。我们来看一下这个接口的定义:

public interface Interceptor {
  Object intercept(Invocation invocation) throws Throwable;
  Object plugin(Object target);
  void setProperties(Properties properties);
}

我们可以看到在该接口中一共有三个方法。


实现自己的拦截器
1.实现Interceptor接口

@Intercepts({@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor {
    /**
     * @param invocation{ 代理对象,被监控方法对象,当前被监控方法运行时需要实参 }
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("被拦截方法执行之前,做的辅助服务");
        Object obj = invocation.proceed();//执行被拦截方法
        System.out.println("被拦截方法执行之后,做的辅助服务");
        return obj;
    }

    /**
     * 如果被拦截对象所在的类有实现接口,就为当前拦截对象生成一个代理对象
     * 如果没有指定接口,这个对象之后行为就不会被代理操作
     *
     * @param target 表示被拦截的对象,针对当前,应该Executor接口实例对象
     * @return
     */
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

2.在MyBatis核心配置文件注册自定义拦截器

<plugins>
    <plugin interceptor="com.tamty.utils.MyInterceptor"></plugin>
</plugins>
上一篇下一篇

猜你喜欢

热点阅读