網站首頁 編程語言 正文
經過前兩篇文章的分析,我們知道了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-08-15 element-ui中el-select低版本清除選項,el-select 要怎么清空已選的數據,顯
- 2022-04-12 Top-level statements must precede namespace and ty
- 2022-09-19 python正則表達式re.sub各個參數的超詳細講解_python
- 2022-04-23 python中的迭代器,生成器與裝飾器詳解_python
- 2022-06-14 GO語言結構體面向對象操作示例_Golang
- 2022-07-09 Android開發中Flutter組件實用技巧_Android
- 2022-02-26 Echarts - 更改圖表圖例(legend)自定義顏色(并與數據段顏色對應)
- 2022-10-03 Redis之SDS數據結構的使用_Redis
- 最近更新
-
- 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同步修改后的遠程分支