mybatis plus 批量保存_mybatis源码分析
原理圖:
Configuration解析:
Configuration表示配置,該對象中維護(hù)了很多mybatis的配置參數(shù);
大致可分為四部分:1.環(huán)境變量Environment 2.配置參數(shù);3.緩存集合;4.插件及其他
1.1環(huán)境變量Environment
Environment.java源碼
通過Environment源碼可以看出,Environment僅維護(hù)了一個id 唯一標(biāo)識,TransactionFactory(事物工廠,負(fù)責(zé)事物管理,Mybatis提供了兩套事物管理
1.org.apache.ibatis.transaction.jdbc 基于jdbc實現(xiàn)事物管理;
2.org.apache.ibatis.transaction.managed 用于擴展第三方事物管理);
和DataSource(數(shù)據(jù)源)。
1.2配置參數(shù):
Configuration提供了一些默認(rèn)配置參數(shù),并可通過get/set方法修改參數(shù)值;
1.3緩存集合:
Configuration維護(hù)了很多緩存容器,如caches,resulrtMaps等’
Configuration初始化
1.api構(gòu)建;2.xml初始化
Configuration初始化
MappendStatement解析:
該對象是mapper.xml在對象中的體現(xiàn),是整個mybatis框架中最為核心的對象,我們也可以不必通過xml文件來構(gòu)建該對象,可以直接通過編碼方式構(gòu)建,像最常用的簡單的增刪改查操作完全可以手動構(gòu)建mappedStatement對象并加入到mybatis容器中,這樣我們就不需要在xml文件中手寫CRUD操作了,mybatis-plus框架設(shè)計的思想就是鑒于此。
public final class MappedStatement { private String resource; private Configuration configuration; //sql的ID private String id; //嘗試影響驅(qū)動程序每次批量返回的結(jié)果行數(shù)和這個設(shè)置值相等 private Integer fetchSize; //SQL超時時間 private Integer timeout; //Statement的類型,STATEMENT/PREPARE/CALLABLE private StatementType statementType; //結(jié)果集類型,FORWARD_ONLY/SCROLL_SENSITIVE/SCROLL_INSENSITIVE private ResultSetType resultSetType; //表示解析出來的SQL private SqlSource sqlSource; //緩存 private Cache cache; //已廢棄 private ParameterMap parameterMap; //對應(yīng)的ResultMap private List resultMaps; private boolean flushCacheRequired; private boolean useCache; private boolean resultOrdered; //SQL類型,INSERT/SELECT/DELETE private SqlCommandType sqlCommandType; //和SELECTKEY標(biāo)簽有關(guān) private KeyGenerator keyGenerator; private String[] keyProperties; private String[] keyColumns; private boolean hasNestedResultMaps; //數(shù)據(jù)庫ID,用來區(qū)分不同環(huán)境 private String databaseId; private Log statementLog; private LanguageDriver lang; //多結(jié)果集時 private String[] resultSets; ...}源碼分析:
1.SqlSessionFactory 裝配Configuration對象;
2.將配置文件中的mapers注冊到configuration的mapperRegistry中
Mybatis代理模式原理分析:
buildSqlSessionFactory方法:XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());xmlMapperBuilder.parse();SqlSession提供select/insert/update/delete方法,在舊版本中使用使用SqlSession接口的這些方法,但是新版的Mybatis中就會建議使用Mapper接口的方法。
射器其實就是一個動態(tài)代理對象,進(jìn)入到MapperMethod的execute方法就能簡單找到SqlSession的刪除、更新、查詢、選擇方法,從底層實現(xiàn)來說:通過動態(tài)代理技術(shù),讓接口跑起來,之后采用命令模式,最后還是采用了SqlSession的接口方法(getMapper()方法等到Mapper)執(zhí)行SQL查詢(也就是說Mapper接口方法的實現(xiàn)底層還是采用SqlSession接口方法實現(xiàn)的)。
解析XMLStatementBuilder.java:
將xml文件轉(zhuǎn)換成MappedStatement對象
public void parseStatementNode() {
String id = context.getStringAttribute("id");
String databaseId = context.getStringAttribute("databaseId");
if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
return;
}
Integer fetchSize = context.getIntAttribute("fetchSize");
Integer timeout = context.getIntAttribute("timeout");
String parameterMap = context.getStringAttribute("parameterMap");
String parameterType = context.getStringAttribute("parameterType");
Class> parameterTypeClass = resolveClass(parameterType);
String resultMap = context.getStringAttribute("resultMap");
String resultType = context.getStringAttribute("resultType");
String lang = context.getStringAttribute("lang");
LanguageDriver langDriver = getLanguageDriver(lang);
Class> resultTypeClass = resolveClass(resultType);
String resultSetType = context.getStringAttribute("resultSetType");
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的mybatis plus 批量保存_mybatis源码分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OPPO Reno11正式开售 单反级人
- 下一篇: 错误代码1500什么意思_啊早安打工人是