基于SqlSessionFactory的openSession方法使用
SqlSessionFactory的openSession方法
正如其名,Sqlsession對(duì)應(yīng)著一次數(shù)據(jù)庫會(huì)話。
由于數(shù)據(jù)庫回話不是永久的,因此Sqlsession的生命周期也不應(yīng)該是永久的,相反,在你每次訪問數(shù)據(jù)庫時(shí)都需要?jiǎng)?chuàng)建它(當(dāng)然并不是說在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)過了以下幾個(gè)主要步驟:
1) 從配置中獲取Environment;
2) 從Environment中取得DataSource;
3) 從Environment中取得TransactionFactory;
4) 從DataSource里獲取數(shù)據(jù)庫連接對(duì)象Connection;
5) 在取得的數(shù)據(jù)庫連接上創(chuàng)建事務(wù)對(duì)象Transaction;
6) 創(chuàng)建Executor對(duì)象(該對(duì)象非常重要,事實(shí)上sqlsession的所有操作都是通過它完成的);
7) 創(chuàng)建sqlsession對(duì)象。
Executor的創(chuàng)建
Executor與Sqlsession的關(guān)系就像市長與書記,Sqlsession只是個(gè)門面,真正干事的是Executor,Sqlsession對(duì)數(shù)據(jù)庫的操作都是通過Executor來完成的。與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;
}
可以看出,如果不開啟cache的話,創(chuàng)建的Executor只是3中基礎(chǔ)類型之一,BatchExecutor專門用于執(zhí)行批量sql操作,ReuseExecutor會(huì)重用statement執(zhí)行sql操作,SimpleExecutor只是簡單執(zhí)行sql沒有什么特別的。開啟cache的話(默認(rèn)是開啟的并且沒有任何理由去關(guān)閉它),就會(huì)創(chuàng)建CachingExecutor,它以前面創(chuàng)建的Executor作為唯一參數(shù)。CachingExecutor在查詢數(shù)據(jù)庫前先查找緩存,若沒找到的話調(diào)用delegate(就是構(gòu)造時(shí)傳入的Executor對(duì)象)從數(shù)據(jù)庫查詢,并將查詢結(jié)果存入緩存中。
Executor對(duì)象是可以被插件攔截的,如果定義了針對(duì)Executor類型的插件,最終生成的Executor對(duì)象是被各個(gè)插件插入后的代理對(duì)象
Mapper
Mybatis官方手冊(cè)建議通過mapper對(duì)象訪問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
我們知道對(duì)被代理對(duì)象的方法的訪問都會(huì)落實(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 就像是一個(gè)分發(fā)者,他根據(jù)參數(shù)和返回值類型選擇不同的 sqlsession 方法來執(zhí)行。這樣 mapper 對(duì)象與 sqlsession 就真正的關(guān)聯(lián)起來了
openSession()到底做了什么


從環(huán)境中獲取事務(wù)的工廠,返回一個(gè)environment對(duì)象獲取事務(wù)工廠
事務(wù)工廠創(chuàng)建事務(wù)
通過configuration拿到一個(gè)執(zhí)行器傳入事務(wù)(Transaction)和類型(execType(枚舉))
最后返回一個(gè)DefaultSqlSession
openSession底層就是做各種成員變量的初始化
例如:configuration,executor,dirty(內(nèi)存當(dāng)中的數(shù)據(jù)與數(shù)據(jù)庫中
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 關(guān)于MyBatis中SqlSessionFactory和SqlSession簡解
- 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問題及處理
相關(guān)文章
帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
SpringBoot--- SpringSecurity進(jìn)行注銷權(quán)限控制的配置方法
這篇文章主要介紹了SpringBoot--- SpringSecurity進(jìn)行注銷,權(quán)限控制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringCloud及Nacos服務(wù)注冊(cè)IP選擇問題解決方法
這篇文章主要介紹了SpringCloud及Nacos服務(wù)注冊(cè)IP選擇問題,為什么注冊(cè)的IP和真實(shí)IP不符合呢,原因是Nacos客戶端在注冊(cè)服務(wù)時(shí)會(huì)從機(jī)器網(wǎng)卡中選擇一個(gè)IP來注冊(cè),所以,當(dāng)注冊(cè)了的是非真實(shí)IP后,另一臺(tái)機(jī)器調(diào)用時(shí)是不可能調(diào)通的,知道問題原因就是解決方法,一起看看吧2024-01-01
MyBatis無縫轉(zhuǎn)MyBatis-plus的基本使用
本文介紹了使用MyBatis-plus來優(yōu)化MyBatis的使用,包括引入依賴、改造Mapper、實(shí)體類注解使用、Service層方法改造等,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
Springboot整合quartz產(chǎn)生錯(cuò)誤及解決方案
這篇文章主要介紹了Springboot整合quartz產(chǎn)生錯(cuò)誤及解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot整合mybatisplus和druid的示例詳解
這篇文章主要介紹了SpringBoot整合mybatisplus和druid的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08

