溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java中mybatis攔截器的案例分析

發(fā)布時間:2020-11-09 12:00:56 來源:億速云 閱讀:186 作者:小新 欄目:編程語言

這篇文章主要介紹java中mybatis攔截器的案例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

攔截器的一個作用就是我們可以攔截某些方法的調(diào)用,我們可以選擇在這些被攔截的方法執(zhí)行前后加上某些邏輯,或者丟棄這些被攔截的方法而執(zhí)行自己的邏輯。

如對于mybatis的Executor,有幾種實現(xiàn):BatchExecutor,ReuseExecutor、SimpleExecutor和CachingExecutor,當這幾種Executor接口的query方法無法滿足我們的要求的時候,我們就可以建立一個攔截器來實現(xiàn)自己的query方法;攔截器一般采用aop動態(tài)實現(xiàn)。

攔截器原理

對于mybatis,我們可以通過interceptor接口定義自己的攔截器。interceptor接口定義:

package org.apache.ibatis.plugin;
import java.util.Properties; 
public interface Interceptor { 
    Object intercept(Invocation invocation) throws Throwable; 
    Object plugin(Object target);
    void setProperties(Properties properties);
}

plugin方法主要是用于封裝目標對象,通過該方法我們可以決定是否要進行攔截進而決定返回什么樣的目標對象。

intercept方法就是要進行攔截的時候執(zhí)行的方法。setProperties主要用于在配置文件中指定屬性,這個方法在Configuration初始化當前的Interceptor時就會執(zhí)行.在mybatis中有一個plugin類,該類包括靜態(tài)方法wrap,通過該方法可以決定需要返回的對象是目標對象還是代理。

package org.apache.ibatis.plugin;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.reflection.ExceptionUtil;
 
public class Plugin implements InvocationHandler {
 
    private Object target;
    private Interceptor interceptor;
    private Map<Class<?>, Set<Method>> signatureMap;
 
    private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
        this.target = target;
        this.interceptor = interceptor;
        this.signatureMap = signatureMap;
    }
 
    public static Object wrap(Object target, Interceptor interceptor) {
        //解析獲取需要攔截的類以及方法{*}
        Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
        Class<?> type = target.getClass();
        //解析type是否存在需要攔截的接口{*}
        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
        //決定返回的對象是否為代理{*}
        if (interfaces.length > 0) {
            return Proxy.newProxyInstance(
                type.getClassLoader(),
                interfaces,
                new Plugin(target, interceptor, signatureMap));
        }
        //返回原目標對象
        return target;
    }
 
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            Set<Method> methods = signatureMap.get(method.getDeclaringClass());
            //如果當前執(zhí)行的方法是定義的需要攔截的方法,則把目標對象,要攔截的方法以及參數(shù)封裝為一個Invocation對象傳遞給攔截器方法intercept;
            //Invocation中定義了定義了一個proceed方法,其邏輯就是調(diào)用當前方法,所以如果在intercept中需要繼續(xù)調(diào)用當前方法的話可以調(diào)用invocation的procced方法;
            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);
        }
    }
 
    //根據(jù)注解解析需要攔截的方法
    //兩個重要的注解:@Intercepts以及其值其值@Signature(一個數(shù)組)
    //@Intercepts用于表明當前的對象是一個Interceptor
    //@Signature則表明要攔截的接口、方法以及對應的參數(shù)類型。
    private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
        Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
        if (interceptsAnnotation == null) { // issue #251
            throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
        }
        Signature[] sigs = interceptsAnnotation.value();
        Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();
        for (Signature sig : sigs) {
            Set<Method> methods = signatureMap.get(sig.type());
            if (methods == null) {
                methods = new HashSet<Method>();
                signatureMap.put(sig.type(), methods);
            }
            try {
                Method method = sig.type().getMethod(sig.method(), sig.args());
                methods.add(method);
            } catch (NoSuchMethodException e) {
                throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
            }
        }
        return signatureMap;
    }
 
    private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>>  signatureMap) {
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        while (type != null) {
            for (Class<?> c : type.getInterfaces()) {
                if (signatureMap.containsKey(c)) {
                    interfaces.add(c);
                }
            }
            type = type.getSuperclass();
        }
        return interfaces.toArray(new Class<?>[interfaces.size()]);
    }
}

攔截器實例

package com.mybatis.interceptor;
 
import java.sql.Connection;
import java.util.Properties;
 
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
 
@Intercepts( {
@Signature(method = "query", type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class })}) 
public class TestInterceptor implements Interceptor {
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        return result;
    }
 
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
 
    public void setProperties(Properties properties) {
        String p = properties.getProperty("property");
    }
}

首先用@Intercepts標記了這是一個Interceptor,通過@Signatrue設計攔截點:攔截Executor接口中參數(shù)類型為MappedStatement、Object、RowBounds和ResultHandler的query方法;intercept方法調(diào)用invocation的proceed方法,使當前方法正常調(diào)用。

攔截器的注冊

注冊攔截器是通過在Mybatis配置文件中plugins元素下的plugin元素來進行的,Mybatis在注冊定義的攔截器時會先把對應攔截器下面的所有property通過Interceptor的setProperties方法注入。如:

<plugins>
    <plugin interceptor="com.mybatis.interceptor.TestInterceptor">
        <property name="property" value="攔截器配置"/>
    </plugin>
</plugins>

以上是java中mybatis攔截器的案例分析的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI