基于SqlSessionFactory的openSession方法使用
SqlSessionFactory的openSession方法
正如其名,Sqlsession對(duì)應(yīng)著一次數(shù)據(jù)庫(kù)會(huì)話。
由于數(shù)據(jù)庫(kù)回話不是永久的,因此Sqlsession的生命周期也不應(yīng)該是永久的,相反,在你每次訪問(wèn)數(shù)據(jù)庫(kù)時(shí)都需要?jiǎng)?chuàng)建它(當(dāng)然并不是說(shuō)在Sqlsession里只能執(zhí)行一次sql,你可以執(zhí)行多次,當(dāng)一旦關(guān)閉了Sqlsession就需要重新創(chuàng)建它)。
創(chuàng)建Sqlsession的地方只有一個(gè)
那就是SqlsessionFactory的openSession方法
public SqlSessionopenSession() { returnopenSessionFromDataSource(configuration.getDefaultExecutorType(),null, false); }
我們可以看到實(shí)際創(chuàng)建SqlSession的地方
是openSessionFromDataSource,如下:
private SqlSessionopenSessionFromDataSource(ExecutorType execType, TransactionIsolationLevellevel, boolean autoCommit) { Connectionconnection = null; try { finalEnvironment environment = configuration.getEnvironment(); final DataSourcedataSource = getDataSourceFromEnvironment(environment); TransactionFactory transactionFactory =getTransactionFactoryFromEnvironment(environment); connection = dataSource.getConnection(); if (level != null) { connection.setTransactionIsolation(level.getLevel()); } connection = wrapConnection(connection); Transaction tx = transactionFactory.newTransaction(connection,autoCommit); Executorexecutor = configuration.newExecutor(tx, execType); returnnewDefaultSqlSession(configuration, executor, autoCommit); } catch (Exceptione) { closeConnection(connection); throwExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
可以看出,創(chuàng)建sqlsession經(jīng)過(guò)了以下幾個(gè)主要步驟:
1) 從配置中獲取Environment;
2) 從Environment中取得DataSource;
3) 從Environment中取得TransactionFactory;
4) 從DataSource里獲取數(shù)據(jù)庫(kù)連接對(duì)象Connection;
5) 在取得的數(shù)據(jù)庫(kù)連接上創(chuàng)建事務(wù)對(duì)象Transaction;
6) 創(chuàng)建Executor對(duì)象(該對(duì)象非常重要,事實(shí)上sqlsession的所有操作都是通過(guò)它完成的);
7) 創(chuàng)建sqlsession對(duì)象。
Executor的創(chuàng)建
Executor與Sqlsession的關(guān)系就像市長(zhǎng)與書(shū)記,Sqlsession只是個(gè)門面,真正干事的是Executor,Sqlsession對(duì)數(shù)據(jù)庫(kù)的操作都是通過(guò)Executor來(lái)完成的。與Sqlsession一樣,Executor也是動(dòng)態(tài)創(chuàng)建的:
public ExecutornewExecutor(Transaction transaction, ExecutorType executorType) { executorType = executorType == null ? defaultExecutorType :executorType; executorType = executorType == null ?ExecutorType.SIMPLE : executorType; Executor executor; if(ExecutorType.BATCH == executorType) { executor = new BatchExecutor(this,transaction); } elseif(ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this,transaction); } else { executor = newSimpleExecutor(this, transaction); } if (cacheEnabled) { executor = new CachingExecutor(executor); } executor =(Executor) interceptorChain.pluginAll(executor); return executor; }
可以看出,如果不開(kāi)啟cache的話,創(chuàng)建的Executor只是3中基礎(chǔ)類型之一,BatchExecutor專門用于執(zhí)行批量sql操作,ReuseExecutor會(huì)重用statement執(zhí)行sql操作,SimpleExecutor只是簡(jiǎn)單執(zhí)行sql沒(méi)有什么特別的。開(kāi)啟cache的話(默認(rèn)是開(kāi)啟的并且沒(méi)有任何理由去關(guān)閉它),就會(huì)創(chuàng)建CachingExecutor,它以前面創(chuàng)建的Executor作為唯一參數(shù)。CachingExecutor在查詢數(shù)據(jù)庫(kù)前先查找緩存,若沒(méi)找到的話調(diào)用delegate(就是構(gòu)造時(shí)傳入的Executor對(duì)象)從數(shù)據(jù)庫(kù)查詢,并將查詢結(jié)果存入緩存中。
Executor對(duì)象是可以被插件攔截的,如果定義了針對(duì)Executor類型的插件,最終生成的Executor對(duì)象是被各個(gè)插件插入后的代理對(duì)象
Mapper
Mybatis官方手冊(cè)建議通過(guò)mapper對(duì)象訪問(wèn)mybatis,因?yàn)槭褂胢apper看起來(lái)更優(yōu)雅,就像下面這樣:
session = sqlSessionFactory.openSession(); UserDao userDao= session.getMapper(UserDao.class); UserDto user =new UserDto(); user.setUsername("iMbatis"); user.setPassword("iMbatis"); userDao.insertUser(user);
看起來(lái)沒(méi)什么特別的,和其他代理類的創(chuàng)建一樣,我們重點(diǎn)關(guān)注一下MapperProxy的invoke方法
MapperProxy的invoke
我們知道對(duì)被代理對(duì)象的方法的訪問(wèn)都會(huì)落實(shí)到代理者的invoke上來(lái),MapperProxy的invoke如下:
public Objectinvoke(Object proxy, Method method, Object[] args) throws Throwable{ if (method.getDeclaringClass()== Object.class) { return method.invoke(this, args); } finalClass<?> declaringInterface = findDeclaringInterface(proxy, method); finalMapperMethod mapperMethod = newMapperMethod(declaringInterface, method, sqlSession); final Objectresult = mapperMethod.execute(args); if (result ==null && method.getReturnType().isPrimitive()&& !method.getReturnType().equals(Void.TYPE)) { thrownewBindingException("Mapper method '" + method.getName() + "'(" + method.getDeclaringClass() + ") attempted toreturn null from a method with a primitive return type (" + method.getReturnType() + ")."); } return result; }
可以看到invoke把執(zhí)行權(quán)轉(zhuǎn)交給了MapperMethod,我們來(lái)看看MapperMethod里又是怎么運(yùn)作的:
public Objectexecute(Object[] args) { Objectresult = null; if(SqlCommandType.INSERT == type) { Objectparam = getParam(args); result= sqlSession.insert(commandName, param); } elseif(SqlCommandType.UPDATE == type) { Object param = getParam(args); result= sqlSession.update(commandName, param); } elseif(SqlCommandType.DELETE == type) { Objectparam = getParam(args); result= sqlSession.delete(commandName, param); } elseif(SqlCommandType.SELECT == type) { if (returnsVoid &&resultHandlerIndex != null) { executeWithResultHandler(args); } elseif (returnsList) { result = executeForList(args); } elseif (returnsMap) { result = executeForMap(args); } else { Object param = getParam(args); result = sqlSession.selectOne(commandName, param); } } else { thrownewBindingException("Unknown execution method for: " + commandName); } return result; }
可以看到, MapperMethod 就像是一個(gè)分發(fā)者,他根據(jù)參數(shù)和返回值類型選擇不同的 sqlsession 方法來(lái)執(zhí)行。這樣 mapper 對(duì)象與 sqlsession 就真正的關(guān)聯(lián)起來(lái)了
openSession()到底做了什么
從環(huán)境中獲取事務(wù)的工廠,返回一個(gè)environment對(duì)象獲取事務(wù)工廠
事務(wù)工廠創(chuàng)建事務(wù)
通過(guò)configuration拿到一個(gè)執(zhí)行器傳入事務(wù)(Transaction)和類型(execType(枚舉))
最后返回一個(gè)DefaultSqlSession
openSession底層就是做各種成員變量的初始化
例如:configuration,executor,dirty(內(nèi)存當(dāng)中的數(shù)據(jù)與數(shù)據(jù)庫(kù)中
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 關(guān)于MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解
- MyBatis源碼解析——獲取SqlSessionFactory方式
- mybatis初始化SqlSessionFactory失敗的幾個(gè)原因分析
- 關(guān)于springboot中對(duì)sqlSessionFactoryBean的自定義
- Springboot?配置SqlSessionFactory方式
- 解析Mybatis SqlSessionFactory初始化原理
- Spring3 整合MyBatis3 配置多數(shù)據(jù)源動(dòng)態(tài)選擇SqlSessionFactory詳細(xì)教程
- 詳解 MapperScannerConfigurer之sqlSessionFactory注入方式
- 使用Mybatis-Plus時(shí)的SqlSessionFactory問(wèn)題及處理
相關(guān)文章
mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換方法詳解
在使用mybatis的時(shí)候經(jīng)常會(huì)遇到枚舉類型的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08Java實(shí)現(xiàn)一個(gè)簡(jiǎn)易版的多級(jí)菜單功能
這篇文章主要給大家介紹了關(guān)于Java如何實(shí)現(xiàn)一個(gè)簡(jiǎn)易版的多級(jí)菜單功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot集成RabbitMQ的方法(死信隊(duì)列)
這篇文章主要介紹了SpringBoot集成RabbitMQ的方法(死信隊(duì)列),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05SpringBoot啟動(dòng)遇到的異常問(wèn)題及解決方案
這篇文章主要介紹了SpringBoot啟動(dòng)遇到的異常問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo
這篇文章主要為大家介紹了java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Spring MessageSource獲取消息不符合預(yù)期的問(wèn)題解決方案
最近我參與的產(chǎn)品要做國(guó)際化支持,選擇了用Spring MessageSource來(lái)實(shí)現(xiàn),這個(gè)Spring 框架提供的工具使用很簡(jiǎn)單,網(wǎng)上有各種教程文章,這里不做贅述,只說(shuō)一個(gè)實(shí)際遇到的問(wèn)題及解決方案,需要的朋友可以參考下2024-01-01利用Maven實(shí)現(xiàn)將代碼打包成第三方公共jar包
在項(xiàng)目開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要將一些公共方法提取出來(lái),然后單獨(dú)封裝成一個(gè)第三方公共jar包,采用普通的方式打包后的jar,依賴的工程執(zhí)行編譯時(shí),卻提示找不到對(duì)應(yīng)的依賴包,那么如何將工程打包為可執(zhí)行jar包呢?下面向大家分享三種方法2022-10-10SpringCloud?Eureka應(yīng)用全面介紹
Eureka是Netflix開(kāi)發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個(gè)基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的2022-09-09Java之Spring簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象
這篇文章主要介紹了Spring的讀取和存儲(chǔ)對(duì)象,獲取 bean 對(duì)象也叫做對(duì)象裝配,是把對(duì)象取出來(lái)放到某個(gè)類中,有時(shí)候也叫對(duì)象注?,想進(jìn)一步了解的同學(xué)可以參考本文2023-04-04