MyBatis-获取 SqlSession
Main 方法,mybatis 版本为 3.5.0
返回一个 DefaultSQlSession 对象,包含 Executor 和 Configuration
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
sqlSessionFactory.openSession()
org.apache.ibatis.session.defaults.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);
// 产生一个 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();
}
}
configuration.newExecutor(tx, execType)
org.apache.ibatis.session.Configuration
// 产生执行器
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
// 防止将 defaultExecutorType 设成 null ?
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
// 简单的3个分支,产生 3 种执行器 BatchExecutor/ReuseExecutor/SimpleExecutor,用来执行 CURD 操作
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(默认有一级缓存),装饰者模式,所以默认都是返回 CachingExecutor
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
// 此处调用插件,通过插件可以改变 Executor 行为
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
new CachingExecutor(executor)
org.apache.ibatis.executor.CachingExecutor
/**
* 二级缓存执行器
*/
public class CachingExecutor implements Executor { private final Executor delegate;
private final TransactionalCacheManager tcm = new TransactionalCacheManager(); public CachingExecutor(Executor delegate) {
this.delegate = delegate;
delegate.setExecutorWrapper(this);
}
return new DefaultSqlSession(configuration, executor, autoCommit)
org.apache.ibatis.session.defaults.DefaultSqlSession
/**
* 默认SqlSession实现
*/
public class DefaultSqlSession implements SqlSession { private final Configuration configuration;
private final Executor executor; // 是否自动提交
private final boolean autoCommit;
private boolean dirty;
private List<Cursor<?>> cursorList; public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}
时序图

https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis
MyBatis-获取 SqlSession的更多相关文章
- MyBatis 入门到精通(一) 了解MyBatis获取SqlSession
MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...
- MyBatis获取SqlSession
package com.ykmimi.dao; import org.apache.ibatis.session.SqlSession; import com.ykmimi.entity.Studen ...
- spring中的mybatis的sqlSession是如何做到线程隔离的?
项目中常常使用mybatis配合spring进行数据库操作,但是我们知道,数据的操作是要求做到线程安全的,而且按照原来的jdbc的使用方式,每次操作完成之后都要将连接关闭,但是实际使用中我们并没有这么 ...
- 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...
- Mybatis获取自增主键的值
pojo: public class User { private Integer id; private String name; private String pwd; setter和getter ...
- SSM-MyBatis-10:Mybatis中SqlSession的getMapper()和简单的工具类MyBatisUtils
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- getMapper的作用,获取到接口,直接通过点的方式调用方法,以免直接手打的方式写错方法名,(强类型的方式) ...
- Mybatis的SqlSession理解(二)
Mybaits加载执行该xml配置 class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, Initi ...
- Mybatis获取自动增长Id
Mybatis获取自动增长Id MyBatis成功插入后获取自动增长的id 1.向xxMapping.xml配置中加上两个配置. <insert id="insertUser" ...
- MyBatis 获取插入记录的 id
现在的项目改用 Guns 了,也是一个很不错的框架,用起来也感觉很不错,上手也挺方便的.毕竟对于只是应用层面的知识,也基本上就是看看手册,熟悉熟悉就可以轻松上手了.如果是想要深入,或者刨根问底,那么就 ...
- Mybatis获取数据库自增主键
一般我们都为将表中主键列设置为自增,当我们执行插入语句时,比如这样 //测试添加 Employee employee = new Employee(null, "jerry4",n ...
随机推荐
- Codeforces Round #468 Div. 1
D:首先考虑如果给定白棋位置,如何判断胜负.黑棋获胜需要四个方向都有能贴上白棋的棋子.由于每一轮都必须移动,显然先对平面黑白染色一下,只有与白棋所在格异色的黑棋才需要考虑.考虑让一个黑棋去贴上白棋某个 ...
- Codeforces Round #539 Div. 1
A:即求长度为偶数的异或和为0的区间个数,对前缀异或和用桶记录即可. #include<iostream> #include<cstdio> #include<cmath ...
- Typecho——简介及安装
Typecho Typecho是由type和echo两个词合成的,来自于开发团队的头脑风暴.Typecho基于PHP5开发,支持多种数据库,是一款内核强健﹑扩展方便﹑体验友好﹑运行流畅的轻量级开源博客 ...
- POJ 2112-Optimal Milking-二分答案+二分图匹配
此题有多种做法. 使用floyd算法预处理最短路,二分答案,对于每一个mid,如果距离比mid小就连边, 注意把每个机器分成m个点.这样跑一个最大匹配,如果都匹配上就可以减小mid值. 用的算法比较多 ...
- Python中xlutils解析
1.导入模块 import xlrd import xlutils.copy 2.打开模块表 book = xlrd.open_workbook('test.xls', formatting_info ...
- codevs 2606 约数和问题 (数学+分块)
题目描述 Description Smart最近沉迷于对约数的研究中. 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X) ...
- os x && linux 文件传输基础命令
一.从服务器下载文件到本机 1.修改文件所属 由于只能下载文件所属为自己的文件,所以要做修改文件所属的操作. chown hudelei /opt/logs/tomcat/app/tomcat_stk ...
- pytorch CNN 手写数字识别
一个被放弃的入门级的例子终于被我实现了,虽然还不太完美,但还是想记录下 1.预处理 相比较从库里下载数据集(关键是经常失败,格式也看不懂),更喜欢直接拿图片,从网上找了半天,最后从CSDN上下载了一个 ...
- 【转】Qt之JSON保存与读取
简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...
- 【BZOJ5288】[HNOI2018]游戏(拓扑排序)
[BZOJ5288][HNOI2018]游戏(拓扑排序) 题面 BZOJ 洛谷 题解 去年省选的时候这题给我乱搞整过去整过去了,也是虐心了.... 所以当然是来讲正儿八经的正确做法啦. 很明显,我们需 ...