新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)mybatis如何實(shí)現(xiàn)SQL查詢攔截修改,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站長(zhǎng)期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為北鎮(zhèn)企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、成都做網(wǎng)站,北鎮(zhèn)網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
前言
截器的一個(gè)作用就是我們可以攔截某些方法的調(diào)用,我們可以選擇在這些被攔截的方法執(zhí)行前后加上某些邏輯,也可以在執(zhí)行這些被攔截的方法時(shí)執(zhí)行自己的邏輯而不再執(zhí)行被攔截的方法。
Mybatis攔截器設(shè)計(jì)的一個(gè)初衷就是為了供用戶在某些時(shí)候可以實(shí)現(xiàn)自己的邏輯而不必去動(dòng)Mybatis固有的邏輯。比如我想針對(duì)所有的SQL執(zhí)行某個(gè)固定的操作,針對(duì)SQL查詢執(zhí)行安全檢查,或者記錄相關(guān)SQL查詢?nèi)罩镜鹊取?/p>
Mybatis為我們提供了一個(gè)Interceptor接口,可以實(shí)現(xiàn)自定義的攔截器。
public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; Object plugin(Object target); void setProperties(Properties properties); }
接口中包含了三個(gè)方法定義
intercept方法為具體的攔截對(duì)象的處理方法,傳入的Invocation包含了攔截目標(biāo)類的實(shí)力,攔截的方法和方法的入?yún)?shù)組。使用Invocation的procced執(zhí)行原函數(shù)。
plugin 中執(zhí)行判斷是否要進(jìn)行攔截進(jìn),如果不需要攔截,直接返回target,如果需要攔截則調(diào)用Plugin類中的wrap靜態(tài)方法,如果當(dāng)前攔截器實(shí)現(xiàn)了任意接口,則返回一個(gè)代理對(duì)象,否則直接返回(回憶代理模式的設(shè)計(jì))。代理對(duì)象實(shí)際是一個(gè)Plugin類實(shí)例,它實(shí)現(xiàn)了InvocationHandler接口 ,InvocationHandler接口僅包含invoke方法用于回調(diào)方法。
當(dāng)執(zhí)行代理對(duì)象的接口方法時(shí),會(huì)調(diào)用Plugin的invoke方法,它會(huì)把要執(zhí)行的對(duì)象,方法和參數(shù)打包成Invocation對(duì)象傳給攔截器的intercept方法。Invocation定義了一個(gè)procced方法,用于執(zhí)行被攔截的原方法。
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, Map, 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 方法顧名思義,用于設(shè)置屬性的。bean的屬性初始化方法有很多,這是其中的一種。
mybatis提供了@Intercepts注解用于聲明當(dāng)前類是攔截器,其值為@Signature數(shù)組,表明要攔截的接口、方法以及對(duì)應(yīng)的參數(shù)類型
@Intercepts({@Signature(method = "prepare", type = StatementHandler.class, args = {Connection.class}), @Signature(method = "query", type = StatementHandler.class, args = {java.sql.Statement.class, ResultHandler.class})}) public class TenantInterceptor implements Interceptor { .....
例如上面的類聲明,第一個(gè)Signature標(biāo)注攔截了StatementHandler類下的入?yún)⑹且粋€(gè)Connection的名為prepare的方法。
第二個(gè)Signature標(biāo)注攔截StatementHandler類中包含2個(gè)入?yún)ⅲǚ謩e為Statement和ResultHandler類型)的名為query的方法。
最后,聲明的Interceptor需要注冊(cè)到mybatis的plug中才能生效。
關(guān)于“mybatis如何實(shí)現(xiàn)SQL查詢攔截修改”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
分享標(biāo)題:mybatis如何實(shí)現(xiàn)SQL查詢攔截修改
鏈接URL:http://ef60e0e.cn/article/ppocho.html