国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

那里可以做旅游網(wǎng)站的嗎我們公司在做網(wǎng)站推廣

那里可以做旅游網(wǎng)站的嗎,我們公司在做網(wǎng)站推廣,用asp做的網(wǎng)站有多少,寧波 做網(wǎng)站的前言: 前文我們從源碼層面梳理了 SqlSessionFactory 的創(chuàng)建過(guò)程,本篇我們繼續(xù)分析一下 SqlSession 的獲取過(guò)程。 初識(shí) MyBatis 【MyBatis 核心概念】 案例代碼: public class MyBatisTest {Testpublic void test() throws IOException {/…

前言:

前文我們從源碼層面梳理了 SqlSessionFactory 的創(chuàng)建過(guò)程,本篇我們繼續(xù)分析一下 SqlSession 的獲取過(guò)程。

初識(shí) MyBatis 【MyBatis 核心概念】

案例代碼:

public class MyBatisTest {@Testpublic void test() throws IOException {//讀取配置文件InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//創(chuàng)建 SqlSessionFactoryBuilder 對(duì)象SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//通過(guò) SqlSessionBuilder 對(duì)象 解析 mybatis-config.xml 文件 構(gòu)建一個(gè)SqlSessionFactory SqlSessionFactory sqlSessionFactory = builder.build(is);//通過(guò)SqlSessionFactory構(gòu)建一個(gè)SqlSessionSqlSession session = sqlSessionFactory.openSession();//通過(guò)SqlSession 獲取 Mapper 實(shí)例UserMapper userMapper = session.getMapper(UserMapper.class);//獲取數(shù)據(jù)List<User> users = userMapper.findAll();//打印輸出for (User user : users) {System.out.println(user);}//關(guān)閉資源session.close();is.close();}
}

本篇我們將主要對(duì) sqlSessionFactory.openSession() 這句代碼進(jìn)行分析。

獲取 SqlSession 源碼分析

DefaultSqlSessionFactory#openSession 方法源碼分析

DefaultSqlSessionFactory#openSession 方法只是調(diào)用了 DefaultSqlSessionFactory#openSessionFromDataSource 方法,并傳入了默認(rèn)的執(zhí)行器類(lèi)型、隔離級(jí)別、是否自動(dòng)提交參數(shù)。

//org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSession()
public SqlSession openSession() {//使用默認(rèn)的執(zhí)行器類(lèi)型(默認(rèn)是SIMPLE) 默認(rèn)隔離級(jí)別 非自動(dòng)提交 委托給 openSessionFromDataSource 方法return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);
}

執(zhí)行器類(lèi)型

  • SIMPLE:簡(jiǎn)單執(zhí)行器 SimpleExecutor,每執(zhí)行一條 SQL,都會(huì)打開(kāi)一個(gè) Statement,執(zhí)行完成后會(huì)關(guān)閉。
  • REUSE:重用執(zhí)行器 ReuseExecutor,其內(nèi)部會(huì)緩存一個(gè) Map<String, Statement> ,每次編譯完成的 Statement 都會(huì)進(jìn)行緩存,不會(huì)關(guān)閉,可以重復(fù)使用。
  • BATCH:批量執(zhí)行器,基于 JDBC 的 addBatch、executeBatch 功能,只能作用于 insert、update、delete 語(yǔ)句。
  • CachingExecutor:緩存執(zhí)行器,使用了裝飾器模式,在開(kāi)啟緩存的時(shí)候,會(huì)在上面三種執(zhí)行器上包裝一層 CachingExecutor。
package org.apache.ibatis.session;public enum ExecutorType {SIMPLE,REUSE,BATCH;private ExecutorType() {}
}

DefaultSqlSessionFactory#openSessionFromDataSource 方法源碼分析

DefaultSqlSessionFactory#openSessionFromDataSource 方法邏輯很簡(jiǎn)單,先獲取創(chuàng)建 SqlSession 的必要參數(shù),然后調(diào)用 DefaultSqlSession 的構(gòu)造方法創(chuàng)建了 SqlSession 。

//org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSessionFromDataSource
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {//事務(wù)Transaction tx = null;//SqlSessionDefaultSqlSession var8;try {//獲取環(huán)境Environment environment = this.configuration.getEnvironment();//獲取事務(wù)工廠TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);//獲取一個(gè)事務(wù)tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);//根據(jù) 事務(wù) 和 執(zhí)行器類(lèi)型創(chuàng)建一個(gè)執(zhí)行器Executor executor = this.configuration.newExecutor(tx, execType);//根據(jù)配置 執(zhí)行器 事務(wù)提交方式創(chuàng)建一個(gè)默認(rèn)的 SqlSessionvar8 = new DefaultSqlSession(this.configuration, executor, autoCommit);} catch (Exception var12) {this.closeTransaction(tx);throw ExceptionFactory.wrapException("Error opening session.  Cause: " + var12, var12);} finally {ErrorContext.instance().reset();}return var8;
}//org.apache.ibatis.session.defaults.DefaultSqlSession#DefaultSqlSession(org.apache.ibatis.session.Configuration, org.apache.ibatis.executor.Executor, boolean)
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {this.configuration = configuration;this.executor = executor;this.dirty = false;this.autoCommit = autoCommit;
}

Configuration#newExecutor 方法源碼分析

Configuration#newExecutor 主要是對(duì)執(zhí)行器類(lèi)型進(jìn)行判斷,然后生成執(zhí)行器,并通過(guò)動(dòng)態(tài)代理得到代理對(duì)象,并將執(zhí)行器加入攔截器鏈。

//org.apache.ibatis.session.Configuration#newExecutor
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? this.defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Object executor;//執(zhí)行器類(lèi)型判斷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);}//是否開(kāi)啟緩存if (this.cacheEnabled) {//開(kāi)啟緩存 創(chuàng)建緩存執(zhí)行器executor = new CachingExecutor((Executor)executor);}//責(zé)任鏈模式 將執(zhí)行器加入攔截器鏈 使用JDK動(dòng)態(tài)代理增強(qiáng)所有的攔截器 Executor executor = (Executor)this.interceptorChain.pluginAll(executor);return executor;
}

獲取 SqlSession 的源碼很簡(jiǎn)單,希望可以幫助到有需要的小伙伴。

歡迎提出建議及對(duì)錯(cuò)誤的地方指出糾正。

http://aloenet.com.cn/news/38088.html

相關(guān)文章:

  • 線上線下購(gòu)物商城系統(tǒng)衡陽(yáng)seo快速排名
  • 國(guó)內(nèi)做的比較好的網(wǎng)站360優(yōu)化大師
  • 網(wǎng)站的透明圖片怎么做網(wǎng)絡(luò)服務(wù)主要包括
  • 網(wǎng)站開(kāi)發(fā)定義名稱(chēng)app優(yōu)化推廣
  • 怎么做網(wǎng)站推廣臨沂關(guān)鍵詞排名手機(jī)優(yōu)化軟件
  • 做網(wǎng)站用c語(yǔ)言可以嗎某個(gè)網(wǎng)站seo分析實(shí)例
  • 電腦版和手機(jī)版網(wǎng)站怎么做的營(yíng)銷(xiāo)推廣策劃方案范文
  • 昆明免費(fèi)網(wǎng)站制作南昌seo技術(shù)外包
  • 鄭州公共住宅建設(shè)投資有限公司網(wǎng)站一站式媒體發(fā)稿平臺(tái)
  • 1000學(xué)習(xí)做網(wǎng)站貴嗎搜索關(guān)鍵詞推薦
  • 網(wǎng)站建設(shè)報(bào)價(jià)明細(xì)表seo優(yōu)化運(yùn)營(yíng)
  • 廈門(mén)建站服務(wù)百度付費(fèi)推廣的費(fèi)用
  • 自助建站門(mén)戶(hù)網(wǎng)站東莞網(wǎng)絡(luò)優(yōu)化公司
  • 買(mǎi)模板建設(shè)網(wǎng)站亞馬遜關(guān)鍵詞工具哪個(gè)最準(zhǔn)
  • 電商 做圖 網(wǎng)站網(wǎng)站數(shù)據(jù)分析
  • 平東網(wǎng)站建設(shè)江北seo綜合優(yōu)化外包
  • 網(wǎng)頁(yè)游戲網(wǎng)站模板外貿(mào)平臺(tái)
  • 重慶房地產(chǎn)新聞上海網(wǎng)站seoseodian
  • 衡陽(yáng)有線寬帶網(wǎng)站怎么做app推廣
  • 南京網(wǎng)站制作設(shè)計(jì)公司尋找客戶(hù)的12種方法
  • 企業(yè)寬帶可以做網(wǎng)站嗎安卓?jī)?yōu)化大師手機(jī)版下載
  • 免費(fèi)直播網(wǎng)站開(kāi)發(fā)seo關(guān)鍵詞優(yōu)化排名
  • 站長(zhǎng)工具亞洲中文精品軟文推廣一般發(fā)布在哪些平臺(tái)
  • 南京網(wǎng)站建設(shè)蘇icp備蘭州正規(guī)seo整站優(yōu)化
  • 上海市住房與城鄉(xiāng)建設(shè)管理委員會(huì)網(wǎng)站網(wǎng)絡(luò)營(yíng)銷(xiāo)咨詢(xún)公司
  • 管理員怎么看網(wǎng)站在線留言越秀seo搜索引擎優(yōu)化
  • 金壇網(wǎng)站建設(shè)哪家好百度網(wǎng)絡(luò)營(yíng)銷(xiāo)
  • wordpress菜單不現(xiàn)實(shí)seo百度快速排名
  • 手機(jī)網(wǎng)站怎么做微信登陸6sem技術(shù)培訓(xùn)
  • 網(wǎng)站策劃書(shū)模板大全怎么做一個(gè)網(wǎng)站的步驟