PS:这篇博文承接上一篇:
MyBatis运行原理(一)SqlSessionFactory对象创建过程分析

在上一篇博文中分析了SqlSessionFactory对象创建的过程,有了SqlSessionFactory对象工厂就可以创建SqlSession了,下面就来具体分析一下SqlSession对象创建的过程。

一、SqlSession对象创建过程分析

入口程序:

    private SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream is = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(is);}@Testpublic void testMyBatis3Simple() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();// 将断点打在下面代码的前面SqlSession sqlSession = sqlSessionFactory.openSession();}

1.首先会跳到DefaultSqlSessionFactory类中的openSession()方法中。

    // ====== DefaultSqlSessionFactory 类中的方法 ======@Overridepublic SqlSession openSession() {return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);}

在上面这个方法中调用了另一个方法:
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit),这个方法做了些什么呢?下面我们就来追踪一下。

  // ====== 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);/*** ExecutorType 是一个枚举类型,默认是SIMPLE* 根据ExecutorType 创建一个Executor 对象* 因为Executor 对象比较重要,下面来分析一下Executor 对象创建的过程*/final Executor executor = configuration.newExecutor(tx, execType);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();}}

2.configuration.newExecutor(tx, execType)创建过程如下:

  // ====== Configuration 类中的方法 ======public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Executor executor;/*** 根据executorType 类型创建对应的Executor * BatchExecutor:批量执行器* ReuseExecutor:会执行预处理的执行器* SimpleExecutor:简单的执行器*/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 来包装executor,* 在查询之前都会先查询缓存中是否有对应的数据* 包装的过程使用了装饰者模式,装饰者模式可参考博文:* http://blog.csdn.net/codejas/article/details/79112824*/if (cacheEnabled) {executor = new CachingExecutor(executor);// 最后使用每个拦截器重新包装executor 并返回executor = (Executor) interceptorChain.pluginAll(executor);// executor 对象创建完成并返回return executor;}

3.Executor 对象创建完成后,会接着执行
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法。

  // ====== 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);/*** configuration 对象在创建SqlSessionFactory 对象的时候就已经创建了* Executor 对象创建完成后,使用executor与configuration 对象来创建DefaultSqlSession* 返回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();}}

到这里会接着向上一步返回,SqlSession对象创建的过程也就结束了。

调用过程时序图:

二、总结

这篇博文对sqlSessionFactory创建SqlSession对象的过程进行了源码分析,最后返回的SqlSession中包含有两个重要的对象,分别是configurationexecutorconfiguration对象在创建SqlSessionFactory的时候就已经被创建出来了,用来保存全局配置文件与SQL 映射文件中的信息,executor是一个执行器对象。如果你想了解更多的细节,可以自己查看源码,希望这篇博文能够为你提供帮助。

MyBatis运行原理(二)SqlSession对象创建过程分析相关推荐

  1. MyBatis运行原理(一)SqlSessionFactory对象创建过程分析

    一.写在前面 MyBatis基于接口形式的编程主要步骤: 1.创建SqlSessionFactory对象. 2.根据SqlSessionFactory对象获取SqlSession对象. 3.为mapp ...

  2. Mybatis运行原理及源码解析

    Mybatis源码解析 一.前言 本文旨在mybatis源码解析,将整个mybatis运行原理讲解清楚,本文代码地址: https://github.com/lchpersonal/mybatis-l ...

  3. MyBatis 实现多表查询、resultMap 标签、MyBatis 注解、mybatis运行原理

    内容 Auto Mapping 单表实现(别名方式) 实现单表配置 单个对象关联查询(N+1,外连接) 集合对象关联查询 注解开发 MyBatis 运行原理 一.MyBatis 实现多表查询 Myba ...

  4. MyBatis运行原理(三)接口式编程及创建代理对象原理分析

    一.面向接口开发步骤 定义代理接口,将操作数据库的方法定义在代理接口中. 在SQL 映射文件中编写SQL 语句. 将SQL 映射文件注册在MyBatis 的全局配置文件中. 编写测试代码. 二.环境准 ...

  5. mybatis运行原理(面试回答)

    在 MyBatis 运行开始时需要先通过 Resources 加载全局配置文件.下面 需要实例化 SqlSessionFactoryBuilder 构建器.帮助 SqlSessionFactory 接 ...

  6. Spring学习(二)—— 对象创建方式及依赖注入

    文章目录 对象创建方式 配置 依赖注入 构造器注入 Set方式注入 拓展方式注入 对象创建方式 默认使用无参构造器创建 当我们需要使用有参构造器时,有以下几种方式: 1.下标赋值 <!--第一种 ...

  7. oracle+视图+图形化,如何利用Object Browser图形化工具提高Oracle开发工作效率(二)对象创建管理篇...

    上一篇我们讲解过有关SQL编写与调试方面的技巧,小编在作图时遗漏了一点,就是Unicode的多语言对应,可以同时显示各种语言不乱码,这是其他工具所不具备的.如图: 下面我们就来说一说如何高效的创建和编 ...

  8. 通过源码分析Mybatis运行原理

    SqlSession类关系图 MapperFactoryBean 获取SqlSessionTemplate,SqlSessionTemplate的Configuration持有了mapper **** ...

  9. MyBatis框架 基本配置及运行原理

    MyBatis(半自动,轻量级)简介 原名iBatis,2013年迁移到gitHub,sql与Java编码分离,sql是开发人员控制 PS:对于JDBC,sql夹杂在java代码块中,耦合度高导致编码 ...

最新文章

  1. Arduino(新手之路2)
  2. Sqlserver2014怎样配置远程连接
  3. html里面表格问题
  4. java线程如何避免死锁_Java面试问题,如何避免Java线程中的死锁?
  5. Object C数据类型
  6. union[c++] in gamedev
  7. 为什么说吉利博越定义了智能SUV
  8. 2017上半年软考 第十二章 重要知识点
  9. git add 所有修改文件_Git的安装及创建版本库
  10. 全国大学生智能汽车竞赛-室外光电组无人驾驶挑战赛-2019
  11. hdu 1212 Big Number
  12. MODBUS通讯协议内容讲解
  13. vue 高德地图搜索功能_vue高德地图搜索功能
  14. c语言编程快速收敛的圆周率计算,[原创]圆周率PI的计算(精确到几十万位)
  15. Ubuntu grub recuse 修复方法
  16. 多线程socket服务器(c语言)
  17. 2d游戏循环滚动地图中的拼接缝隙问题
  18. 根据指定字母,顺序输出若干相邻字母 C语言
  19. GVim Leaderf的安装与配置使用
  20. 第四课、软件测试产品说明书的编写

热门文章

  1. mysql主表一条数据对应从表多条数据需要只显示一条
  2. 《数据库系统实训》实验报告——事务的应用
  3. [USACO1.3]修理牛棚 Barn Repair
  4. springboot整合kafka和netty服务简单实例
  5. java8中stream最实用总结和调试技巧
  6. 2016年 第7届 蓝桥杯 Java B组 省赛解析及总结
  7. 第八届 蓝桥杯 承压计算
  8. Android性能优化——内存泄漏优化
  9. phpstorm的php函数文档插件
  10. sqlmap 相关参数