網站首頁 編程語言 正文
經過前兩篇文章的分析,我們知道了mybatis對配置文件mybatis-config.xml和mapper.xml 的一個加載原理,以及配置信息Configuraction和執行器Excutor 信息封裝入DefaultSqlSession中。
現在針對mapper相關源碼進行解析,分析Mybaits是如果沒有實體類的情況下,可以執行接口方法?
示例:
?進入getmapper方法,實現類DefaultSqlSession:
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
// 初始化
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
?繼續:
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
MapperProxy類,該類實現了InvocationHandler接口,實現了invoke方法。
?回過頭進入newInstance方法:
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
這就很清楚了創建了一個jdk代理對象。
?
總結 : getmapper方法創建了mapper接口的JDK代理對接,并返回。因為mapper接口核心目的是提供sql方法的全限定名,使用其找到對應的存儲在mappedstatements中的xml配置sql內容,執行sql,所以這就是為什么不需要實現類就可以進行方法調用的根本原因:通過代理實現調用invoke方法實現mappedstatement配置的匹配。
原文鏈接:https://blog.csdn.net/qq_31142237/article/details/120410946
相關推薦
- 2022-05-06 C#迭代器方法介紹_C#教程
- 2022-09-25 統一管理 Activity 便于隨時退出程序
- 2023-12-18 YAMLException異常產生原因及解決方案
- 2022-06-13 Python+Matplotlib繪制雙y軸圖像的示例代碼_python
- 2022-03-07 深入淺析C/C++語言結構體指針的使用注意事項_C 語言
- 2022-08-05 C語言示例講解switch分支語句的用法_C 語言
- 2022-10-15 C語言循環結構深入刨析_C 語言
- 2022-05-15 Python?中的集合和字典_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支