Mybatis中攔截器的簡單實(shí)現(xiàn)方法
前言
需求驅(qū)動(dòng)學(xué)習(xí),最近一周組長讓我在業(yè)務(wù)模塊里加日志,經(jīng)過與導(dǎo)師以及組長討論決定用攔截器記錄日志。周五下班前已經(jīng)發(fā)了提測郵件。
雖然我知道 MyBatis 有這東西,但是沒在實(shí)際情況中用過,心里有點(diǎn)虛2333……所以才有了此文的理解。
前世今生
它的本質(zhì)就是 JDK 的動(dòng)態(tài)代理。首先先來復(fù)習(xí)一下動(dòng)態(tài)代理我貼了一段最常見的 JDK 動(dòng)態(tài)代理的代碼
//服務(wù)員的接口 public interface Waiter { void serve(); } //服務(wù)員的實(shí)現(xiàn) public class WaiterImpl implements Waiter { @Override public void serve() { System.out.println("服務(wù)中..."); } } //需要代理的對象處理器 class WaitInvocationHandler implements InvocationHandler { private Waiter waiter; public WaitInvocationHandler(Waiter waiter1) { waiter = waiter1; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("你好"); Object invoke = method.invoke(waiter, args); System.out.println("再見"); return invoke; } } public class App { //普通的實(shí)現(xiàn) @Test public void fun() { Waiter waiter = new WaiterImpl(); waiter.serve(); } @Test public void fun1() { Waiter a = new WaiterImpl(); ClassLoader classLoader = getClass().getClassLoader(); Class[] classes = {Waiter.class}; //生成代理對象 Waiter waiterproxy = (Waiter) Proxy.newProxyInstance(classLoader, classes, new WaitInvocationHandler(a)); waiterproxy.serve(); } }
攔截器
V1
我現(xiàn)在要在函數(shù)執(zhí)行前后記錄日志操作,考慮需要在代理方法那里定義個(gè)攔截器的接口,如下代碼所示
//攔截器 V1 版本 public interface MyInterceptorV1 { void interceptor(); } //代理對象變成這個(gè) public class TargetProxyV1 implements InvocationHandler { private Target target; private MyInterceptorV1 myInterceptor; public TargetProxyV1(Target target, MyInterceptorV1 myInterceptor) { this.target = target; this.myInterceptor = myInterceptor; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { myInterceptor.interceptor(); return method.invoke(target, args); } }
這是最簡單的版本,但是我們很多時(shí)候需要攔截參數(shù)等根據(jù)參數(shù)判斷攔不攔截等,代碼更新如下
public interface MyInterceptorV1 { void interceptor(Method method, Object[] args); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { myInterceptor.interceptor(method, args); return method.invoke(target, args); }
V2
似乎上面的方案很完美
廢話肯定不完美,不然怎么會有這段
沒錯(cuò),第二段代碼并不是很優(yōu)雅,有方法參數(shù)重復(fù),可以考慮將三者抽出來,直接在攔截器的實(shí)現(xiàn)里寫上 method.invoke(target, args); ,如下代碼所示
@Getter @Setter @AllArgsConstructor public class MyInvocation { private Object target; private Method method; private Object[] args; public Object proceed() throws InvocationTargetException, IllegalAccessException { return method.invoke(target, args); } } //沒錯(cuò)這就是 V2 版本 public interface MyInterceptorV2 { Object interceptor(MyInvocation invocation) throws Throwable; }
總結(jié)
Mybatis 的攔截器就是像我上面這么寫的,
名字也跟我取得一樣, 只是它更加復(fù)雜,能夠通過注解區(qū)分?jǐn)r截 update 操作和 query
等操作。
既完成了任務(wù)又鞏固了原來的知識,這種感覺很棒,最關(guān)鍵的是還有錢拿……
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
- mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能
- Mybatis自定義攔截器和插件開發(fā)詳解
- mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor示例
- mybatis 通過攔截器打印完整的sql語句以及執(zhí)行結(jié)果操作
- Mybatis Plugin攔截器開發(fā)過程詳解
- 簡單了解mybatis攔截器實(shí)現(xiàn)原理及實(shí)例
- mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的方法
- mybatis攔截器與分頁插件實(shí)例教程
- Mybatis Interceptor 攔截器的實(shí)現(xiàn)
- MyBatis攔截器實(shí)現(xiàn)分頁功能的實(shí)現(xiàn)方法
- MyBatis攔截器的原理與使用
相關(guān)文章
ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04SpringBoot Tomcat啟動(dòng)實(shí)例代碼詳解
這篇文章主要介紹了SpringBoot Tomcat啟動(dòng)實(shí)例代碼詳解,需要的朋友可以參考下2017-09-09spring boot+ redis 接口訪問頻率限制的實(shí)現(xiàn)
這篇文章主要介紹了spring boot+ redis 接口訪問頻率限制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01