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的更多相关文章

  1. MyBatis 入门到精通(一) 了解MyBatis获取SqlSession

    MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...

  2. MyBatis获取SqlSession

    package com.ykmimi.dao; import org.apache.ibatis.session.SqlSession; import com.ykmimi.entity.Studen ...

  3. spring中的mybatis的sqlSession是如何做到线程隔离的?

    项目中常常使用mybatis配合spring进行数据库操作,但是我们知道,数据的操作是要求做到线程安全的,而且按照原来的jdbc的使用方式,每次操作完成之后都要将连接关闭,但是实际使用中我们并没有这么 ...

  4. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  5. Mybatis获取自增主键的值

    pojo: public class User { private Integer id; private String name; private String pwd; setter和getter ...

  6. SSM-MyBatis-10:Mybatis中SqlSession的getMapper()和简单的工具类MyBatisUtils

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- getMapper的作用,获取到接口,直接通过点的方式调用方法,以免直接手打的方式写错方法名,(强类型的方式) ...

  7. Mybatis的SqlSession理解(二)

    Mybaits加载执行该xml配置 class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, Initi ...

  8. Mybatis获取自动增长Id

    Mybatis获取自动增长Id MyBatis成功插入后获取自动增长的id 1.向xxMapping.xml配置中加上两个配置. <insert id="insertUser" ...

  9. MyBatis 获取插入记录的 id

    现在的项目改用 Guns 了,也是一个很不错的框架,用起来也感觉很不错,上手也挺方便的.毕竟对于只是应用层面的知识,也基本上就是看看手册,熟悉熟悉就可以轻松上手了.如果是想要深入,或者刨根问底,那么就 ...

  10. Mybatis获取数据库自增主键

    一般我们都为将表中主键列设置为自增,当我们执行插入语句时,比如这样 //测试添加 Employee employee = new Employee(null, "jerry4",n ...

随机推荐

  1. #195 game(动态规划+二分)

    考虑第一问的部分分.显然设f[i]为i子树从根开始扩展的所需步数,考虑根节点的扩展顺序,显然应该按儿子子树所需步数从大到小进行扩展,将其排序即可. 要做到n=3e5,考虑换根dp.计算某点答案时先将其 ...

  2. groovy的效率问题

    刚开始学groovy,知道了它会先变异成class 文件,然后再用jvm 执行.写了Hello World程序,查看它的编译文件,发现groovy的效率挺低的.不但编译文件的代码多,而且需要依赖很多g ...

  3. word 2013 题注、图注、插入图片自动修改大小、批量更新题注编号

    1 .题注 图片下面的文字说明,如  图 1.1.1 2.图注 图的标题格式,可以右键修改段落为居中,选中图片,点下此格式快捷居中等其他格式 3. 题注插入 效果 如下 4.题注自动居中对齐 先点击图 ...

  4. pytest_01_安装和入门

    目录 pytest 安装与入门 1.pip install -U pytest 2.创建一个test01.py的文件 3.在该目录下执行pytest(venv) 4.执行多个,新建一个py文件 tes ...

  5. poj3680 Intervals (费用流)

    建图((x,y,c,l)表示x到y,费用c,流量l) (S,1,0,K) (i,i+1,0,K) 这个边上的流量,表示i还可以被覆盖的次数 (N,T,0,K) (i,j,w,1)对于权值为w的区间[i ...

  6. NOIP2017逛公园(dp+最短路)

    策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要花的时间. 策策每天都会 ...

  7. Linux下搜狗输入法和快捷键Ctrl+Space冲突的解决

    配置快捷键(不是搜狗的设置) 把搜狗的启动快捷键给删了(如果有两个键盘[英+中],你按Shift就可以切换了,完全没必要占着茅坑) 其他快捷键冲突在键盘里面设置 over

  8. log4j2 自定义配置文件,java载入

    http://logging.apache.org/log4j/2.x/faq.html#separate_log_files How do I reconfigure log4j2 in code ...

  9. 洛谷P2178 品酒大会

    题意:若两个字符开始的后面r个字符都一样,则称这两个字符是r相似的.它们也是r-1相似的. 对于r∈[0,n)分别求有多少种方案,其中权值最大方案权值是多少.此处权值是选出的两个字符的权值之积. 解: ...

  10. A1141. PAT Ranking of Institutions

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...