日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis源码学习笔记

發(fā)布時(shí)間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis源码学习笔记 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Mybatis核心概念:

?

Configuration : 管理 mysql-config.xml 全局配置關(guān)系類

SqlSessionFactory: Session 管理工廠接口

Session:? SqlSession 是一個(gè)面向用戶(程序員)的接口。SqlSession 中提供了很多操作數(shù)據(jù)庫的方法

Executor :? 執(zhí)行器是一個(gè)接口(基本執(zhí)行器、緩存執(zhí)行器)。 作用:SqlSession 內(nèi)部通過執(zhí)行器操作數(shù)據(jù)庫

MappedStatement: 底層封裝對(duì)象 。作用:對(duì)操作數(shù)據(jù)庫存儲(chǔ)封裝,包括 sql 語句、輸入輸出參數(shù)

StatementHandler :具體操作數(shù)據(jù)庫相關(guān)的 handler 接口

ResultSetHandler :具體操作數(shù)據(jù)庫返回結(jié)果的 handler 接口

?

Debug代碼執(zhí)行順序:

入口代碼:

String resource = "mybatis-config.xml";//獲取源配置文件InputStream inputStream = Resources.getResourceAsStream(resource);//獲取sqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//獲取sessionSqlSession sqlSession = sqlSessionFactory.openSession();//操作數(shù)據(jù)庫User user = sqlSession.selectOne("com.jiagouedu.mybatis.UserMapper.selectUser", 1);

?1. 解析xml文件變成Configuration 返回SqlSessionFactory

org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream)>org.apache.ibatis.builder.xml.XMLConfigBuilder 構(gòu)造函數(shù)>org.apache.ibatis.builder.xml.XMLConfigBuilder.parse>org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration mybatis-config.xml內(nèi)容>org.apache.ibatis.parsing.XPathParser.evaluate>org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement>org.apache.ibatis.session.SqlSessionFactoryBuilder.build(org.apache.ibatis.session.Configuration)>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.DefaultSqlSessionFactory

2. 執(zhí)行器進(jìn)行初始化 SimpleExecutor拿到SqlSession

org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession()>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource>org.apache.ibatis.transaction.TransactionFactory.newTransaction(javax.sql.DataSource, org.apache.ibatis.session.TransactionIsolationLevel, boolean)>org.apache.ibatis.session.Configuration.newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)>org.apache.ibatis.executor.SimpleExecutor>org.apache.ibatis.executor.CachingExecutor 一級(jí)緩存 自動(dòng)>org.apache.ibatis.plugin.InterceptorChain.pluginAll 責(zé)任鏈模式攔截器

3.操作數(shù)據(jù)庫

org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(java.lang.String, java.lang.Object)>org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(java.lang.String, java.lang.Object)>org.apache.ibatis.session.Configuration.getMappedStatement(java.lang.String)>org.apache.ibatis.executor.CachingExecutor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)>org.apache.ibatis.executor.CachingExecutor.createCacheKey 緩存的key>org.apache.ibatis.executor.CachingExecutor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)>org.apache.ibatis.executor.BaseExecutor.queryFromDatabase>org.apache.ibatis.executor.BaseExecutor.doQuery>org.apache.ibatis.executor.statement.PreparedStatementHandler.query>org.apache.ibatis.executor.resultset.ResultSetHandler.handleResultSets>org.apache.ibatis.executor.resultset.DefaultResultSetHandler

?

?以下是代碼執(zhí)行流程圖:

?seesion的獲取:

Mapper對(duì)象的獲取:

?

執(zhí)行器Executor:

BaseExecutor用到的是規(guī)范的理念,使實(shí)現(xiàn)這個(gè)接口的類都按照這個(gè)規(guī)范去做對(duì)應(yīng)的事情.

?

Mybatis中用到的設(shè)計(jì)模式:

1.sqlSessionFactory 工廠

2. build 建造者

3. getInstance ,Cache 單例

4. 委派 裝飾

5.InterceptorChain 責(zé)任鏈

6 Proxy 代理

7. Execute Command 命令

8.doQuery 模板

?

總結(jié)

以上是生活随笔為你收集整理的Mybatis源码学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。