美文网首页mybatis
Mybatis拦截器

Mybatis拦截器

作者: 48892085f47c | 来源:发表于2016-10-19 15:07 被阅读2740次

    1.需求背景

    设定订单表order,要根据订单类型统计订单数据,大致sql如下:

    select order_type , count(1)  as order_num from order group by order_type;
    

    Mybatis无法将以上sql以指定key:order_type;value:order_num存入至map中。

    而Mybatis默认返回的List<Map<String, Object>>,是以每个字段name作为key,字段的值作为value,放入至Map<String,Object>的List数组。
    因此自定义一种可以指定key、value字段的Mybatis插件将非常有用。

    2.实现

    在实现这个插件之前,我们先了解下Mybatis拦截器。
    详见Mybatis拦截器介绍及分页插件

    拦截器:我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法。
    Mybatis拦截器:就是为了供用户在某些时候可以实现自己的逻辑而不必去动Mybatis固有的逻辑。

    2.1 Mybatis 提供的Interceptor接口

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

    Interceptor接口提供三个方法:
      plugin方法是拦截器用于封装目标对象的,通过该方法我们可以返回目标对象本身,也可以返回一个它的代理。当返回的是代理的时候我们可以对其中的方法进行拦截来调用intercept方法,当然也可以调用其他方法,这点将在后文讲解。setProperties方法是用于在Mybatis配置文件中指定一些属性的。
      定义自己的Interceptor最重要的是要实现plugin方法和intercept方法,在plugin方法中我们可以决定是否要进行拦截进而决定要返回一个什么样的目标对象。而intercept方法就是要进行拦截的时候要执行的方法。
      对于plugin方法而言,其实Mybatis已经为我们提供了一个实现。Mybatis中有一个叫做Plugin的类,里面有一个静态方法wrap(Object target,Interceptor interceptor),通过该方法可以决定要返回的对象是目标对象还是对应的代理。
    先看下Plugin类源码:

    public static Object wrap(Object target, Interceptor interceptor) {  
           Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);  
           Class<?> type = target.getClass();  
           Class<?>[] interfaces = getAllInterfaces(type, signatureMap);  
           if (interfaces.length > 0) {    
              return Proxy.newProxyInstance(type.getClassLoader(),interfaces,
                                   new Plugin(target, interceptor, signatureMap));  
       }     return target;
    }
    
    @Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
     try {    
          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); 
     } catch (Exception e) {    
            throw ExceptionUtil.unwrapThrowable(e); 
       }
    }
    

    我们先看一下Plugin的wrap方法,它根据当前的Interceptor上面的注解定义哪些接口需要拦截,然后判断当前目标对象是否有实现对应需要拦截的接口,如果没有则返回目标对象本身,如果有则返回一个代理对象。而这个代理对象的InvocationHandler正是一个Plugin。所以当目标对象在执行接口方法时,如果是通过代理对象执行的,则会调用对应InvocationHandler的invoke方法,也就是Plugin的invoke方法。所以接着我们来看一下该invoke方法的内容。这里invoke方法的逻辑是:如果当前执行的方法是定义好的需要拦截的方法,则把目标对象、要执行的方法以及方法参数封装成一个Invocation对象,再把封装好的Invocation作为参数传递给当前拦截器的intercept方法。如果不需要拦截,则直接调用当前的方法。Invocation中定义了定义了一个proceed方法,其逻辑就是调用当前方法,所以如果在intercept中需要继续调用当前方法的话可以调用invocation的procced方法。
      这就是Mybatis中实现Interceptor拦截的一个思想,如果用户觉得这个思想有问题或者不能完全满足你的要求的话可以通过实现自己的Plugin来决定什么时候需要代理什么时候需要拦截。以下讲解的内容都是基于Mybatis的默认实现即通过Plugin来管理Interceptor来讲解的。

    两个重要注解:@Intercepts和@Signature,@Intercepts用于表示当前对象是Interceptor拦截器,@Signature用于展示需要拦截的接口、方法和参数。
    eg:
    @Intercepts(@Signature(method="handleResultSets",type=ResultSetHandler.class,args={Statement.class}))

    2.2注册拦截器

    注册拦截器是通过在Mybatis配置文件中plugins元素下的plugin元素来进行的。一个plugin对应着一个拦截器,在plugin元素下面我们可以指定若干个property子元素。Mybatis在注册定义的拦截器时会先把对应拦截器下面的所有property通过Interceptor的setProperties方法注入给对应的拦截器。

    <property name="plugins">    
        <array>        
          <bean class="com.test.admin.common.dal.common.interceptor.MapInterceptor"/>    
        </array>
    </property>
    

    相关文章

      网友评论

        本文标题:Mybatis拦截器

        本文链接:https://www.haomeiwen.com/subject/rzdzyttx.html