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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis运行原理(二)SqlSession对象创建过程分析

發布時間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis运行原理(二)SqlSession对象创建过程分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PS:這篇博文承接上一篇:
MyBatis運行原理(一)SqlSessionFactory對象創建過程分析

在上一篇博文中分析了SqlSessionFactory對象創建的過程,有了SqlSessionFactory對象工廠就可以創建SqlSession了,下面就來具體分析一下SqlSession對象創建的過程。

一、SqlSession對象創建過程分析

入口程序:

private SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream is = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(is);}@Testpublic void testMyBatis3Simple() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();// 將斷點打在下面代碼的前面SqlSession sqlSession = sqlSessionFactory.openSession();}

1.首先會跳到DefaultSqlSessionFactory類中的openSession()方法中。

// ====== DefaultSqlSessionFactory 類中的方法 ======@Overridepublic SqlSession openSession() {return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);}

在上面這個方法中調用了另一個方法:
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit),這個方法做了些什么呢?下面我們就來追蹤一下。

// ====== DefaultSqlSessionFactory 類中的方法 ======private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {Transaction tx = null;try {final Environment environment = configuration.getEnvironment();final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);// 獲取配置環境中的一些信息,用于創建事務tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);/*** ExecutorType 是一個枚舉類型,默認是SIMPLE* 根據ExecutorType 創建一個Executor 對象* 因為Executor 對象比較重要,下面來分析一下Executor 對象創建的過程*/final Executor executor = configuration.newExecutor(tx, execType);return new DefaultSqlSession(configuration, executor, autoCommit);} catch (Exception e) {closeTransaction(tx); // may have fetched a connection so lets call close()throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);} finally {ErrorContext.instance().reset();}}

2.configuration.newExecutor(tx, execType)創建過程如下:

// ====== Configuration 類中的方法 ======public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Executor executor;/*** 根據executorType 類型創建對應的Executor * BatchExecutor:批量執行器* ReuseExecutor:會執行預處理的執行器* SimpleExecutor:簡單的執行器*/if (ExecutorType.BATCH == executorType) {executor = new BatchExecutor(this, transaction);} else if (ExecutorType.REUSE == executorType) {executor = new ReuseExecutor(this, transaction);} else {executor = new SimpleExecutor(this, transaction);}/*** 如果開啟了二級緩存,則使用CachingExecutor 來包裝executor,* 在查詢之前都會先查詢緩存中是否有對應的數據* 包裝的過程使用了裝飾者模式,裝飾者模式可參考博文:* http://blog.csdn.net/codejas/article/details/79112824*/if (cacheEnabled) {executor = new CachingExecutor(executor);// 最后使用每個攔截器重新包裝executor 并返回executor = (Executor) interceptorChain.pluginAll(executor);// executor 對象創建完成并返回return executor;}

3.Executor 對象創建完成后,會接著執行
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法。

// ====== DefaultSqlSessionFactory 類中的方法 ======private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {Transaction tx = null;try {final Environment environment = configuration.getEnvironment();final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);final Executor executor = configuration.newExecutor(tx, execType);/*** configuration 對象在創建SqlSessionFactory 對象的時候就已經創建了* Executor 對象創建完成后,使用executor與configuration 對象來創建DefaultSqlSession* 返回DefaultSqlSession 對象*/return new DefaultSqlSession(configuration, executor, autoCommit);} catch (Exception e) {closeTransaction(tx); // may have fetched a connection so lets call close()throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);} finally {ErrorContext.instance().reset();}}

到這里會接著向上一步返回,SqlSession對象創建的過程也就結束了。

調用過程時序圖:

二、總結

這篇博文對sqlSessionFactory創建SqlSession對象的過程進行了源碼分析,最后返回的SqlSession中包含有兩個重要的對象,分別是configuration與executor。configuration對象在創建SqlSessionFactory的時候就已經被創建出來了,用來保存全局配置文件與SQL 映射文件中的信息,executor是一個執行器對象。如果你想了解更多的細節,可以自己查看源碼,希望這篇博文能夠為你提供幫助。

總結

以上是生活随笔為你收集整理的MyBatis运行原理(二)SqlSession对象创建过程分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。