mybatis随笔二之SqlSessionFactory
在上一篇文章我们已经得到了DefaultSqlSessionFactory

@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
在configuration内部默认指定了protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
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);
      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();
    }
  }
从代码中我们可知开启了一个jdbc事务,并调用newExecutor得到一个执行期
public Executor newExecutor(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);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }
如果开启了cache,那么会得到一个CachingExecutor,这采用的是装饰设计模式,它的语句执行实际是交给内部的delegate来执行的。
里面值得注意的是使用了(Executor) interceptorChain.pluginAll(executor)将所有的拦截器注入进去形成一个拦截链。
public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
其实Plugin是mybatis的一个工具类,用来方便生成代理对象的,注意这个地方只会生成拦截目标是Executor的拦截类的代理对象,如下所示这种就不会调用。
@Intercepts({
        @Signature(type = StatementHandler.class,
                method = "prepare",
                args = {Connection.class})
})
然后返回DefaultSqlSession的一个实例对象
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
    this.configuration = configuration;
    this.executor = executor;
    this.dirty = false;
    this.autoCommit = autoCommit;
  }
至此DefaultSqlSessionFactory创建DefaultSqlSession的过程完成。
mybatis随笔二之SqlSessionFactory的更多相关文章
- mybatis入门(二):增删改查
		mybatis的原理: 1.mybatis是一个持久层框架,是apache下的顶级项目 mybatis托管到googlecode下,目前托管到了github下面 2.mybatis可以将向prepar ... 
- spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
		spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 无法启动 ... 
- MyBatis系列二  之   数据库列名于程序实体类中字段名称不一致
		MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致 情景:当数据库中的列名与我们程序实体类中的字段名称不一致 使用ResultMap节点配置信息 在映射文件中 ... 
- MyBatis笔记二:配置
		MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ... 
- (原创)mybatis学习二,spring和mybatis的融合
		mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ... 
- 33、mybatis(二)
		第十六章回顾SQL99中的连接查询 1)内连接 2)外连接 3)自连接 第十七章回顾hibernate多表开发 1)一对一 2)一对多 3)多对多 第十八章 mybatis一对一映射[学生与身份证] ... 
- mybatis(二)接口编程 、动态sql 、批量删除 、动态更新、连表查询
		原理等不在赘述,这里主要通过代码展现. 在mybatis(一)基础上,新建一个dao包,并在里面编写接口,然后再在xml文件中引入接口路径,其他不变,在运用阶段将比原始方法更节约时间,因为不用再去手动 ... 
- MyBatis之二:简单增删改查
		这一篇在上一篇的基础上简单讲解如何进行增删改查操作. 一.在mybatis的配置文件conf.xml中注册xml与注解映射 <!-- 注册映射文件 --> <mappers> ... 
- Java框架之Mybatis(二)
		本文主要介绍 Mybatis(一)之后剩下的内容: 1 mybatis 中 log4j的配置 2 dao层的开发(使用mapper代理的方式) 3 mybatis的配置详解 4 输入输出映射对应的类型 ... 
随机推荐
- MIT Molecular Biology 笔记7 调控RNA
			视频 https://www.bilibili.com/video/av7973580/ 教材 Molecular biology of the gene 7th edition J.D. Wat ... 
- 基于udp协议的套接字,socketserver模块,多道技术,进程理论
			进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆的代码 进程:程序执行的过程 进程的概念起源于操作系统,进程是操作系统最核心的概念,操作系统的其他所有 ... 
- 20145232 韩文浩 《Java程序设计》第2周学习总结
			教材学习内容总结 这周主要学习了注释的使用,运算符的基本使用,认识了类型,变量与常量,以及一些语句语法.复习了进制转换. 整数 Short整数占用2字节,储存整数范围-32768 ~ 32767(2- ... 
- 判断页面是app打开还是浏览器打开。cookie
			有个需求需要对页面判断不同的打开方式来最里面的链接进行不同调整, 这样就要分四种情况,app,浏览器X安卓系统,苹果系统,起初是对页面url地址带有的参数(安卓)跟用户代理(苹果)返回值判断navig ... 
- JavaScript相关基础知识点
			JavaScript简介: JavaScript是脚本语言,是一种轻量级的编程语言,是可插入 HTML 页面的编程代码,插入 HTML 页面后,可由所有的现代浏览器执行. JavaScript使用: ... 
- MapReduce编程解析
			MapReduce编程模型之案例 wordcount 输入数据 atguigu atguiguss sscls clsjiaobanzhangxuehadoop 输出数据 atguigu 2banzh ... 
- Python自动化开发 - 函数式编程
			本节内容 一.函数式编程 二.高阶函数 1.变量可以指向函数 2.函数名也是变量 3.传入函数 三.返回函数 1.函数作为返回值 2.闭包特性 一.函数式编程 函数是Python内建支持的一种封装,我 ... 
- htpasswd建立和更新存储用户名、密码
			htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpasswd --help Usage: htpass ... 
- 4.动态HTML处理和机器图像识别
			Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上, ... 
- hdu 1.2.6
			勾股定理... #include<cstdio> #include<algorithm> using namespace std; int main() { //freopen ... 
