Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎
動態(tài)代理的功能:通過攔截器方法回調(diào),對目標(biāo)target方法進行增強。
言外之意就是為了增強目標(biāo)target方法。上面這句話沒錯,但也不要認(rèn)為它就是真理,殊不知,動態(tài)代理還有投鞭斷流的霸權(quán),連目標(biāo)target都不要的科幻模式。
注:本文默認(rèn)認(rèn)為,讀者對動態(tài)代理的原理是理解的,如果不明白target的含義,難以看懂本篇文章,建議先理解動態(tài)代理。
1. 自定義JDK動態(tài)代理之投鞭斷流實現(xiàn)自動映射器Mapper
首先定義一個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 }
再定義一個接口UserMapper.java。
public interface UserMapper { public User getUserById(Integer id); }
接下來我們看看如何使用動態(tài)代理之投鞭斷流,實現(xiàn)實例化接口并調(diào)用接口方法返回數(shù)據(jù)的。
自定義一個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)前對象this return method.invoke(this, args); } catch (Throwable t) { } } // 投鞭斷流 return new User((Integer) args[0], "zhangsan", 18); } }
上面代碼中的target,在執(zhí)行Object.java內(nèi)的方法時,target被指向了this,target已經(jīng)變成了傀儡、象征、占位符。在投鞭斷流式的攔截時,已經(jīng)沒有了target。
寫一個測試代碼:
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
x.y.MapperProxy@6bc7c0541234
這便是Mybatis自動映射器Mapper的底層實現(xiàn)原理。
可能有讀者不禁要問:你怎么把代碼寫的像初學(xué)者寫的一樣?沒有結(jié)構(gòu),且缺乏美感。
必須聲明,作為一名經(jīng)驗老道的高手,能把程序?qū)懙南癯鯇W(xué)者寫的一樣,那必定是高手中的高手。這樣可以讓初學(xué)者感覺到親切,舒服,符合自己的Style,讓他們或她們,感覺到大牛寫的代碼也不過如此,自己甚至寫的比這些大牛寫的還要好,從此自信滿滿,熱情高漲,認(rèn)為與大牛之間的差距,僅剩下三分鐘。
2. Mybatis自動映射器Mapper的源碼分析
首先編寫一個測試類:
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長這個樣子:
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使用動態(tài)代理之投鞭斷流。
3. 接口Mapper內(nèi)的方法能重載(overLoad)嗎?(重要)
類似下面:
public User getUserById(Integer id); public User getUserById(Integer id, String name);
Answer:不能。
原因:在投鞭斷流時,Mybatis使用package+Mapper+method全限名作為key,去xml內(nèi)尋找唯一sql來執(zhí)行的。類似:key=x.y.UserMapper.getUserById,那么,重載方法時將導(dǎo)致矛盾。對于Mapper接口,Mybatis禁止方法重載(overLoad)。
最后
到此這篇關(guān)于Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎的文章就介紹到這了,更多相關(guān)Mybatis接口Mapper重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項目啟動過程動態(tài)修改接口請求路徑的解決方案
在SpringBoot服務(wù)整合過程中,遇到了多個服務(wù)中相同RequestMapping路徑導(dǎo)致的啟動問題,解決方案是通過修改RequestMappingHandlerMapping類的getMappingForMethod方法,本文給大家介紹SpringBoot修改接口請求路徑的解決方案,感興趣的朋友一起看看吧2024-09-09Idea中如何調(diào)出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03淺談SpringBoot中properties、yml、yaml的優(yōu)先級
優(yōu)先級低的配置會被先加載,所以優(yōu)先級高的配置會覆蓋優(yōu)先級低的配置,本文就來介紹一下SpringBoot中properties、yml、yaml的優(yōu)先級,感興趣的可以了解一下2023-08-08SpringBoot實現(xiàn)文件上傳下載實時進度條功能(附源碼)
這篇文章主要為大家詳細介紹了SpringBoot如何實現(xiàn)文件上傳下載實時進度條功能,文中的示例代碼講解詳細,感興趣的小伙伴可以學(xué)習(xí)一下2022-10-10mybatis中方法返回泛型與resultType不一致的解決
這篇文章主要介紹了mybatis中方法返回泛型與resultType不一致的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?jav
這篇文章主要介紹了解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?javax.servlet.ServletContext問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01詳解使用Spring Boot開發(fā)Restful程序
本篇文章主要介紹了詳解使用Spring Boot開發(fā)Restful程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05