mybatis 實現 SQL 查詢攔截修改詳解
前言
截器的一個作用就是我們可以攔截某些方法的呼叫,我們可以選擇在這些被攔截的方法執行前後加上某些邏輯,也可以在執行這些被攔截的方法時執行自己的邏輯而不再執行被攔截的方法。
Mybatis攔截器設計的一個初衷就是為了供使用者在某些時候可以實現自己的邏輯而不必去動Mybatis固有的邏輯。比如我想針對所有的SQL執行某個固定的操作,針對SQL查詢執行安全檢查,或者記錄相關SQL查詢日誌等等。
Mybatis為我們提供了一個Interceptor介面,可以實現自定義的攔截器。
public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; Object plugin(Object target); void setProperties(Properties properties); }
介面中包含了三個方法定義
intercept方法為具體的攔截物件的處理方法,傳入的Invocation包含了攔截目標類的實力,攔截的方法和方法的入引數組。使用Invocation的procced執行原函式。
plugin 中執行判斷是否要進行攔截進,如果不需要攔截,直接返回target,如果需要攔截則呼叫Plugin類中的wrap靜態方法,如果當前攔截器實現了任意介面,則返回一個代理物件,否則直接返回(回憶代理模式的設計)。代理物件實際是一個Plugin類例項,它實現了InvocationHandler介面 ,InvocationHandler介面僅包含invoke方法用於回撥方法。
當執行代理物件的介面方法時,會呼叫Plugin的invoke方法,它會把要執行的物件,方法和引數打包成Invocation物件傳給攔截器的intercept方法。Invocation定義了一個procced方法,用於執行被攔截的原方法。
Plugin類定義
public class Plugin implements InvocationHandler { private Object target; private Interceptor interceptor; private Map,Set> signatureMap; private Plugin(Object target,Interceptor interceptor,Map,Set> signatureMap) { this.target = target; this.interceptor = interceptor; this.signatureMap = signatureMap; } public static Object wrap(Object target,Interceptor interceptor) { Map,Set> 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; } public Object invoke(Object proxy,Method method,Object[] args) throws Throwable { try { Set 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); } } private static Map,Set> 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,Set> signatureMap = new HashMap,Set>(); for (Signature sig : sigs) { Set methods = signatureMap.get(sig.type()); if (methods == null) { methods = new HashSet(); 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,Set> signatureMap) { Set> interfaces = new HashSet>(); 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()]); } }
setProperties 方法顧名思義,用於設定屬性的。bean的屬性初始化方法有很多,這是其中的一種。
mybatis提供了@Intercepts註解用於聲明當前類是攔截器,其值為@Signature陣列,表明要攔截的介面、方法以及對應的引數型別
@Intercepts({@Signature(method = "prepare",type = StatementHandler.class,args = {Connection.class}),@Signature(method = "query",args = {java.sql.Statement.class,ResultHandler.class})}) public class TenantInterceptor implements Interceptor { .....
例如上面的類宣告,第一個Signature標註攔截了StatementHandler類下的入參是一個Connection的名為prepare的方法。
第二個Signature標註攔截StatementHandler類中包含2個入參(分別為Statement和ResultHandler型別)的名為query的方法。
最後,宣告的Interceptor需要註冊到mybatis的plug中才能生效。
<!-- 配置mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!-- mapper掃描 --> <property name="mapperLocations" value="classpath:mybatis/*/*.xml"/> <property name="plugins"> <array> <!-- 註冊自己的攔截器 --> <bean id="paginationInterceptor" class="xxx.xxx.TenantInterceptor"> </bean> </array> </property> </bean>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。