用過MyBatis3的人可能會(huì)覺得為什么MyBatis的Mapper接口沒有實(shí)現(xiàn)類,但是可以直接用?
那是因?yàn)镸yBatis使用Java動(dòng)態(tài)代理實(shí)現(xiàn)的接口。
這里僅僅舉個(gè)簡(jiǎn)單例子來(lái)說(shuō)明原理,不是完全針對(duì)MyBatis的,這種思想我們也可以應(yīng)用在其他地方。
定義一個(gè)接口
1
|
public interface MethodInterface { String helloWorld(); } |
實(shí)現(xiàn)動(dòng)態(tài)代理接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MethodProxy<T> implements InvocationHandler { private Class<T> methodInterface; public MethodProxy(Class<T> methodInterface) { this .methodInterface = methodInterface; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println( "=========================" ); System.out.println( "方法名:" + method.getName()); //針對(duì)不同的方法進(jìn)行不同的操作 return null ; } } |
這里針對(duì)invoke方法簡(jiǎn)單說(shuō)說(shuō)MyBatis的實(shí)現(xiàn)原理,在該方法中,我們通過Method能夠獲取接口和方法名,接口的全名相當(dāng)于MyBatis XML中的namespace,方法名相當(dāng)于具體一個(gè)方法中的id。也就是說(shuō)通過動(dòng)態(tài)代理后,可以通過SqlSession來(lái)通過namespace.id方式來(lái)調(diào)用相應(yīng)的方法。使用接口更方便,但是是一種間接的方式。
動(dòng)態(tài)代理工廠類
1
2
3
4
5
6
7
8
9
|
public class MethodProxyFactory { public static <T> T newInstance(Class<T> methodInterface) { final MethodProxy<T> methodProxy = new MethodProxy<T>(methodInterface); return (T) Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class[]{methodInterface}, methodProxy); } } |
通過該工廠類可以生成任意接口的動(dòng)態(tài)代理類。
測(cè)試
1
2
|
MethodInterface method = MethodProxyFactory.newInstance(MethodInterface. class ); method.helloWorld(); |
總結(jié)
一般談到動(dòng)態(tài)代理我們通常的用法都是處理事務(wù)、日志或者記錄方法執(zhí)行效率等方面的應(yīng)用。都是對(duì)實(shí)現(xiàn)類方法的前置或者后置的特殊處理。
通過本文,其實(shí)可以看到另一種應(yīng)用方向,那就是不需要實(shí)現(xiàn)類,直接通過動(dòng)態(tài)代理執(zhí)行接口方法,MyBatis使用這種方式方便了我們調(diào)用方法,利用這種思路我們也許可以在別的方面做出一些更好的設(shè)計(jì)。
以上所述是小編給大家介紹的MyBatis接口的簡(jiǎn)單實(shí)現(xiàn)原理分析,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
原文鏈接:http://www.cnblogs.com/zdd-java/archive/2017/07/09/7141270.html