基于SqlSessionFactory的openSession方法使用
SqlSessionFactory的openSession方法
正如其名,Sqlsession對應(yīng)著一次數(shù)據(jù)庫會話。
由于數(shù)據(jù)庫回話不是永久的,因此Sqlsession的生命周期也不應(yīng)該是永久的,相反,在你每次訪問數(shù)據(jù)庫時都需要創(chuàng)建它(當(dāng)然并不是說在Sqlsession里只能執(zhí)行一次sql,你可以執(zhí)行多次,當(dāng)一旦關(guān)閉了Sqlsession就需要重新創(chuàng)建它)。
創(chuàng)建Sqlsession的地方只有一個
那就是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)過了以下幾個主要步驟:
1) 從配置中獲取Environment;
2) 從Environment中取得DataSource;
3) 從Environment中取得TransactionFactory;
4) 從DataSource里獲取數(shù)據(jù)庫連接對象Connection;
5) 在取得的數(shù)據(jù)庫連接上創(chuàng)建事務(wù)對象Transaction;
6) 創(chuàng)建Executor對象(該對象非常重要,事實(shí)上sqlsession的所有操作都是通過它完成的);
7) 創(chuàng)建sqlsession對象。
Executor的創(chuàng)建
Executor與Sqlsession的關(guān)系就像市長與書記,Sqlsession只是個門面,真正干事的是Executor,Sqlsession對數(shù)據(jù)庫的操作都是通過Executor來完成的。與Sqlsession一樣,Executor也是動態(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; }
可以看出,如果不開啟cache的話,創(chuàng)建的Executor只是3中基礎(chǔ)類型之一,BatchExecutor專門用于執(zhí)行批量sql操作,ReuseExecutor會重用statement執(zhí)行sql操作,SimpleExecutor只是簡單執(zhí)行sql沒有什么特別的。開啟cache的話(默認(rèn)是開啟的并且沒有任何理由去關(guān)閉它),就會創(chuàng)建CachingExecutor,它以前面創(chuàng)建的Executor作為唯一參數(shù)。CachingExecutor在查詢數(shù)據(jù)庫前先查找緩存,若沒找到的話調(diào)用delegate(就是構(gòu)造時傳入的Executor對象)從數(shù)據(jù)庫查詢,并將查詢結(jié)果存入緩存中。
Executor對象是可以被插件攔截的,如果定義了針對Executor類型的插件,最終生成的Executor對象是被各個插件插入后的代理對象
Mapper
Mybatis官方手冊建議通過mapper對象訪問mybatis,因?yàn)槭褂胢apper看起來更優(yōu)雅,就像下面這樣:
session = sqlSessionFactory.openSession(); UserDao userDao= session.getMapper(UserDao.class); UserDto user =new UserDto(); user.setUsername("iMbatis"); user.setPassword("iMbatis"); userDao.insertUser(user);
看起來沒什么特別的,和其他代理類的創(chuàng)建一樣,我們重點(diǎn)關(guān)注一下MapperProxy的invoke方法
MapperProxy的invoke
我們知道對被代理對象的方法的訪問都會落實(shí)到代理者的invoke上來,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,我們來看看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 就像是一個分發(fā)者,他根據(jù)參數(shù)和返回值類型選擇不同的 sqlsession 方法來執(zhí)行。這樣 mapper 對象與 sqlsession 就真正的關(guān)聯(lián)起來了
openSession()到底做了什么
從環(huán)境中獲取事務(wù)的工廠,返回一個environment對象獲取事務(wù)工廠
事務(wù)工廠創(chuàng)建事務(wù)
通過configuration拿到一個執(zhí)行器傳入事務(wù)(Transaction)和類型(execType(枚舉))
最后返回一個DefaultSqlSession
openSession底層就是做各種成員變量的初始化
例如:configuration,executor,dirty(內(nèi)存當(dāng)中的數(shù)據(jù)與數(shù)據(jù)庫中
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- 關(guān)于MyBatis中SqlSessionFactory和SqlSession簡解
- MyBatis源碼解析——獲取SqlSessionFactory方式
- mybatis初始化SqlSessionFactory失敗的幾個原因分析
- 關(guān)于springboot中對sqlSessionFactoryBean的自定義
- Springboot?配置SqlSessionFactory方式
- 解析Mybatis SqlSessionFactory初始化原理
- Spring3 整合MyBatis3 配置多數(shù)據(jù)源動態(tài)選擇SqlSessionFactory詳細(xì)教程
- 詳解 MapperScannerConfigurer之sqlSessionFactory注入方式
- 使用Mybatis-Plus時的SqlSessionFactory問題及處理
相關(guān)文章
mybatis中實(shí)現(xiàn)枚舉自動轉(zhuǎn)換方法詳解
在使用mybatis的時候經(jīng)常會遇到枚舉類型的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于mybatis中實(shí)現(xiàn)枚舉自動轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08解決Spring配置文件中bean的property屬性中的name出錯問題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot集成RabbitMQ的方法(死信隊(duì)列)
這篇文章主要介紹了SpringBoot集成RabbitMQ的方法(死信隊(duì)列),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo
這篇文章主要為大家介紹了java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Spring MessageSource獲取消息不符合預(yù)期的問題解決方案
最近我參與的產(chǎn)品要做國際化支持,選擇了用Spring MessageSource來實(shí)現(xiàn),這個Spring 框架提供的工具使用很簡單,網(wǎng)上有各種教程文章,這里不做贅述,只說一個實(shí)際遇到的問題及解決方案,需要的朋友可以參考下2024-01-01利用Maven實(shí)現(xiàn)將代碼打包成第三方公共jar包
在項(xiàng)目開發(fā)過程中,我們經(jīng)常需要將一些公共方法提取出來,然后單獨(dú)封裝成一個第三方公共jar包,采用普通的方式打包后的jar,依賴的工程執(zhí)行編譯時,卻提示找不到對應(yīng)的依賴包,那么如何將工程打包為可執(zhí)行jar包呢?下面向大家分享三種方法2022-10-10SpringCloud?Eureka應(yīng)用全面介紹
Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的2022-09-09