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. web scraper——简单的爬取数据【二】

    web scraper——安装[一] 在上文中我们已经安装好了web scraper现在我们来进行简单的爬取,就来爬取百度的实时热点吧. http://top.baidu.com/buzz?b=1&a ...

  2. 读取CSV到DataTable

    using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using Sy ...

  3. 使用tree命令导出文件夹/文件的目录树

    前提:己安装扩展: 介绍: TREE [drive:][path] [/F] [/A] /F   显示每个文件夹中文件的名称.(带扩展名)   /A   使用 ASCII 字符,而不使用扩展字符. t ...

  4. 【BZOJ5324】[JXOI2018]守卫(动态规划)

    [BZOJ5324][JXOI2018]守卫(动态规划) 题面 BZOJ 洛谷 题解 既然只能看到横坐标在左侧的点,那么对于任意一个区间\([l,r]\)而言,\(r\)必须被选. 假设\(r\)看不 ...

  5. css文本设置

    常用的应用文本的css样式: color 设置文字的颜色,如: color:red; font-size 设置文字的大小,如:font-size:12px; font-family 设置文字的字体,如 ...

  6. 测试工作常用SQL查询语句

    一.查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>, ...

  7. 构建MFS分布式文件系统

    ++++++++++++++构建MFS分布式文件系统++++++++++++++PB级别:企业存储空间达到PB级别,即100万GB空间.(1PB=1000TB,1TB=1000GB,1GB=1000M ...

  8. Chemical table CFR500 div2D(并查集)

    给定的一个n*m的区域内,给出一些点的坐标,这些点上有一个元素,如果在矩形的子矩形的三个点都有元素,那么第四个点的元素可以自己产生,其他的元素需要购买,问最少需要购买多少中元素才可以把这个区域给填满. ...

  9. 阶乘函数(factorial)——结果在整型范围内的阶乘计算

    定义: 在数学中,正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,计为n!,例如5的阶乘计为5!,其值为120: \[ 5!=5\times 4\times 3\times ...

  10. 只用200行Go代码写一个自己的区块链!

    Coral Health · 大约23小时之前 · 220 次点击 · 预计阅读时间 7 分钟 · 不到1分钟之前 开始浏览 区块链是目前最热门的话题,广大读者都听说过比特币,或许还有智能合约,相信大 ...