詳解Mybatis內(nèi)的mapper方法為何不能重載
動(dòng)態(tài)代理的功能:通過(guò)攔截器方法回調(diào),對(duì)目標(biāo)target方法進(jìn)行增強(qiáng)。
言外之意就是為了增強(qiáng)目標(biāo)target方法。上面這句話沒(méi)錯(cuò),但也不要認(rèn)為它就是真理,殊不知,動(dòng)態(tài)代理還有投鞭斷流的霸權(quán),連目標(biāo)target都不要的科幻模式。
注:本文默認(rèn)認(rèn)為,讀者對(duì)動(dòng)態(tài)代理的原理是理解的,如果不明白target的含義,難以看懂本篇文章,建議先理解動(dòng)態(tài)代理。
1. 自定義JDK動(dòng)態(tài)代理之投鞭斷流實(shí)現(xiàn)自動(dòng)映射器Mapper
首先定義一個(gè)pojo。
public class User {
private Integer id;
private String name;
private int age;
public User(Integer id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// getter setter
}
再定義一個(gè)接口UserMapper.java。
public interface UserMapper {
public User getUserById(Integer id);
}
接下來(lái)我們看看如何使用動(dòng)態(tài)代理之投鞭斷流,實(shí)現(xiàn)實(shí)例化接口并調(diào)用接口方法返回?cái)?shù)據(jù)的。
自定義一個(gè)InvocationHandler。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MapperProxy implements InvocationHandler {
@SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> clz) {
return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] { clz }, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
// 諸如hashCode()、toString()、equals()等方法,將target指向當(dāng)前對(duì)象this
return method.invoke(this, args);
} catch (Throwable t) {
}
}
// 投鞭斷流
return new User((Integer) args[0], "zhangsan", 18);
}
}
上面代碼中的target,在執(zhí)行Object.java內(nèi)的方法時(shí),target被指向了this,target已經(jīng)變成了傀儡、象征、占位符。在投鞭斷流式的攔截時(shí),已經(jīng)沒(méi)有了target。
寫(xiě)一個(gè)測(cè)試代碼:
public static void main(String[] args) {
MapperProxy proxy = new MapperProxy();
UserMapper mapper = proxy.newInstance(UserMapper.class);
User user = mapper.getUserById(1001);
System.out.println("ID:" + user.getId());
System.out.println("Name:" + user.getName());
System.out.println("Age:" + user.getAge());
System.out.println(mapper.toString());
}
output:
ID:1001
Name:zhangsan
Age:18 page
x.y.MapperProxy@6bc7c054
這便是Mybatis自動(dòng)映射器Mapper的底層實(shí)現(xiàn)原理。
可能有讀者不禁要問(wèn):你怎么把代碼寫(xiě)的像初學(xué)者寫(xiě)的一樣?沒(méi)有結(jié)構(gòu),且缺乏美感。
必須聲明,作為一名經(jīng)驗(yàn)老道的高手,能把程序?qū)懙南癯鯇W(xué)者寫(xiě)的一樣,那必定是高手中的高手。這樣可以讓初學(xué)者感覺(jué)到親切,舒服,符合自己的Style,讓他們或她們,感覺(jué)到大牛寫(xiě)的代碼也不過(guò)如此,自己甚至寫(xiě)的比這些大牛寫(xiě)的還要好,從此自信滿滿,熱情高漲,認(rèn)為與大牛之間的差距,僅剩下三分鐘。
2. Mybatis自動(dòng)映射器Mapper的源碼分析
首先編寫(xiě)一個(gè)測(cè)試類(lèi):
public static void main(String[] args) {
SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
try {
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
} finally {
sqlSession.close();
}
}
Mapper長(zhǎng)這個(gè)樣子:
public interface StudentMapper {
List<Student> findAllStudents();
Student findStudentById(Integer id);
void insertStudent(Student student);
}
org.apache.ibatis.binding.MapperProxy.java部分源碼。
public class MapperProxy<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;
private final Class<T> mapperInterface;
private final Map<Method, MapperMethod> methodCache;
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
// 投鞭斷流
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
// ...
org.apache.ibatis.binding.MapperProxyFactory.java部分源碼。
public class MapperProxyFactory<T> {
private final Class<T> mapperInterface;
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
這便是Mybatis使用動(dòng)態(tài)代理之投鞭斷流。
3. 接口Mapper內(nèi)的方法能重載(overLoad)嗎?(重要)
類(lèi)似下面:
public User getUserById(Integer id); public User getUserById(Integer id, String name);
Answer:不能。
原因:在投鞭斷流時(shí),Mybatis使用package+Mapper+method全限名作為key,去xml內(nèi)尋找唯一sql來(lái)執(zhí)行的。類(lèi)似:key=x.y.UserMapper.getUserById,那么,重載方法時(shí)將導(dǎo)致矛盾。對(duì)于Mapper接口,Mybatis禁止方法重載(overLoad)。
注:學(xué)習(xí)時(shí),是先研究的源碼,看懂了原理。寫(xiě)博文時(shí),則先闡釋原理,再閱讀的源碼。順序剛好相反,希望讀者不要因此疑惑,以為我強(qiáng)大到未卜先知。
到此這篇關(guān)于詳解Mybatis內(nèi)的mapper方法為何不能重載的文章就介紹到這了,更多相關(guān)Mybatis mapper重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文帶你看懂Java8中的lambda表達(dá)式和方法引用
Lambda 表達(dá)式是 Java 8 引入的一項(xiàng)重要特性,它提供了一種簡(jiǎn)潔、清晰且靈活的語(yǔ)法來(lái)表示可傳遞的匿名函數(shù),下面就跟隨小編一起學(xué)習(xí)一下Java8中的lambda表達(dá)式和方法引用的相關(guān)知識(shí)吧2023-12-12
單一職責(zé)原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了單一職責(zé)原則的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
mybatis如何實(shí)現(xiàn)的數(shù)據(jù)庫(kù)排序
這篇文章主要介紹了mybatis如何實(shí)現(xiàn)的數(shù)據(jù)庫(kù)排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實(shí)現(xiàn)在線語(yǔ)音識(shí)別
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)在線語(yǔ)音識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
kafka 重新分配partition和調(diào)整replica的數(shù)量實(shí)現(xiàn)
當(dāng)需要提升Kafka集群的性能和負(fù)載均衡時(shí),可通過(guò)kafka-reassign-partitions.sh命令手動(dòng)重新分配Partition,增加節(jié)點(diǎn)后,可以將Topic的Partition的Leader節(jié)點(diǎn)均勻分布,以提高寫(xiě)入和消費(fèi)速度,感興趣的可以了解一下2022-03-03
Spring?Cloud?Sleuth?和?Zipkin?進(jìn)行分布式跟蹤使用小結(jié)
分布式跟蹤是一種機(jī)制,我們可以使用它跟蹤整個(gè)分布式系統(tǒng)中的特定請(qǐng)求,分布式跟蹤允許您跟蹤分布式系統(tǒng)中的請(qǐng)求,本文給大家介紹Spring?Cloud?Sleuth?和?Zipkin?進(jìn)行分布式跟蹤使用小結(jié),感興趣的朋友一起看看吧2022-03-03
Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用
這篇文章主要介紹了Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
spring boot補(bǔ)習(xí)系列之幾種scope詳解
這篇文章主要給大家介紹了關(guān)于spring boot補(bǔ)習(xí)系列之幾種scope的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

