码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis插件

时间:2017-06-20 11:36:02      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:class   ati   signature   rem   obj   null   ora   并且   工具   

1. Interceptor 就是个接口,供其它插件实现这个接口有三个方法

 

Object intercept(Invocation invocation)throws Throwable;

传入一个Invocation 对象,我猜应该是指定拦截具体某个target的某个方法

这里面应该写具体的拦截策略,befor after。调用invocation.proceed()执行具体的方法

 

Object plugin(Object target);

将目标对象与插件绑定

 

void setProperties(Properties properties);

设置属性

 

2. Invocation 是一个对象类

 

对象内部包括目标对象、待执行的方法、参数列表

 

public Object proceed() 包装了一下method.invoke(target,args)

 

3. Plugin这个类比较重要

这个类有两个比较重要的方法,一个

public staticObject wrap(Object target, Interceptor interceptor)

这是个静态方法,作为工具用

将target和interceptor两个对象包装在一起。返回代理类对象

实现了Interceptor接口的对象中plugin方法可以调用这个静态方法来实现将intercepter和target绑定到一起

这个方法调用了getSignatureMap方法来取写了antonation的方法,可能是通过注解来标注哪些类需要拦截吧

另一个

publicObject invoke(Object proxy, Method method, Object[] args)

这个方法实现了InvocationHandler接口必须要实现这个方法,并且这个方法在调用对象的方法时是一定要执行的。

这个方法里也比较简单,就是判断有没有拦截器,有执行拦截器,否则直接执行方法

Set<Method> methods = signatureMap.get(method.getDeclaringClass());

     if(methods != null&& methods.contains(method)) {

        return interceptor.intercept(new Invocation(target, method, args));

     }

      return method.invoke(target, args);

 

4. 有个问题就是这几个插件类具体怎么用

         1) 首先一定是要实现Interceptor,这个接口里应该有个成员变量来存target吧?通过plugin方法绑定。Plugin方法里应该是调用了Plugin. Wrap这个静态方法。将绑定后的类返回

         2) 用户使用时直接new 一个Plugin对象,将target、interceptor、signatureMap传进得去。这样再调用 

mybatis插件

标签:class   ati   signature   rem   obj   null   ora   并且   工具   

原文地址:http://www.cnblogs.com/liguangming/p/7053380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!