文档章节

Mybatis源码分析

麦馍
 麦馍
发布于 12/03 18:24
字数 743
阅读 6
收藏 0

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

(二)SqlSessionFactoryBuilder类

1)内部方法

      根据入参的不同SqlSessionFactoryBuilder内部提供了六个方法用于构造SqlSessionFactory。

public class SqlSessionFactoryBuilder {

  public SqlSessionFactory build(Reader reader) {
    return build(reader, null, null);
  }

  public SqlSessionFactory build(Reader reader, String environment) {
    return build(reader, environment, null);
  }

  public SqlSessionFactory build(Reader reader, Properties properties) {
    return build(reader, null, properties);
  }

  

  public SqlSessionFactory build(InputStream inputStream) {
    return build(inputStream, null, null);
  }

  public SqlSessionFactory build(InputStream inputStream, String environment) {
    return build(inputStream, environment, null);
  }

  public SqlSessionFactory build(InputStream inputStream, Properties properties) {
    return build(inputStream, null, properties);
  }

}

 

2)构造SqlSessionFactory

    上面列出的六个方法最终都通过调用以下两个方法之一来构造SqlSessionFactory

  • SqlSessionFactory build(Reader reader, String environment, Properties properties)
  • SqlSessionFactory build(Reader reader, String environment, Properties properties)
public class SqlSessionFactoryBuilder {

  public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        reader.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

}

        这俩个方法的内部会通过构造XMLConfigBuilder的对象,然后由该对象解析配置,并将解析所得的配置生成Configuration类型的对象并返回

public class XMLConfigBuilder extends BaseBuilder {
  public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }
}

        得到Configuration对象后, 最终调用SqlSessionFactory build(Configuration config)方法构造SqlSessionFactory

public class SqlSessionFactoryBuilder {

  //构造SqlSessionFactory    
  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

}

 

(三)DefaultSqlSessionFactory类

DefaultSqlSessionFactory提供了一个带Configuration参数的构造方法,并提供了多种获取SqlSession的openSession方法

public class DefaultSqlSessionFactory implements SqlSessionFactory {

  private final Configuration configuration;

  public DefaultSqlSessionFactory(Configuration configuration) {
    this.configuration = configuration;
  }

  public Configuration getConfiguration() {
    return configuration;
  }

  //使用默认ExecutorType
  public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

  //使用默认ExecutorType,指定是否自动提交
  public SqlSession openSession(boolean autoCommit) {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
  }

  //指定ExecutorType,非自动提交
  public SqlSession openSession(ExecutorType execType) {
    return openSessionFromDataSource(execType, null, false);
  }

  //使用默认ExecutorType,指定隔离级别
  public SqlSession openSession(TransactionIsolationLevel level) {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);
  }

  //指定ExecutorType、隔离级别,非自动提交
  public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
    return openSessionFromDataSource(execType, level, false);
  }

  //指定ExecutorType,指定是否自动提交
  public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
    return openSessionFromDataSource(execType, null, autoCommit);
  }

  //使用默认ExecutorType
  public SqlSession openSession(Connection connection) {
    return openSessionFromConnection(configuration.getDefaultExecutorType(), connection);
  }

  指定ExecutorType
  public SqlSession openSession(ExecutorType execType, Connection connection) {
    return openSessionFromConnection(execType, connection);
  }
}

 

上面所列的所有openSession方法最终都会调用以下两个方法之一,获取SqlSession

  • SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
  • SqlSession openSessionFromConnection(ExecutorType execType, Connection connection)
public class DefaultSqlSessionFactory implements SqlSessionFactory {

  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, autoCommit);
      return new DefaultSqlSession(configuration, executor);
    } 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();
    }
  }

  private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
    try {
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      final Transaction tx = transactionFactory.newTransaction(connection);
      final Executor executor = configuration.newExecutor(tx, execType, connection.getAutoCommit());
      return new DefaultSqlSession(configuration, executor);
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
  
}

© 著作权归作者所有

下一篇: Semaphore
麦馍
粉丝 1
博文 27
码字总数 28216
作品 0
私信 提问
mybatis源码分析之SqlSession的创建过程

mybatis之SqlSessionFactory mybatis源码分析之Configuration mybatis源码分析之事务管理器 以上是之前的分析,在mybatis源码分析之事务管理器里分析到了事务管理器 SqlSession session = s...

udbwcso
2016/04/21
2.2K
0
mybatis源码分析之TypeHandler

上一篇: mybatis源码分析之MapperMethod https://my.oschina.net/u/657390/blog/755787 分析了MapperMethod从创建到执行的过程,MapperMethod的执行包括执行sql返回结果. 在执行sql和返回结果...

udbwcso
2016/11/18
627
0
mybatis(三)配置详解之properties与environments

上一篇我们通过对mybatis源码的简单分析,可看出,在mybatis配置文件中,在configuration根节点下面,可配置properties、typeAliases、plugins、objectFactory、objectWrapperFactory、setti...

LCZ777
2014/12/18
38
0
早前学习Java记录

Spring 对 iBATIS 的支持】 Spring 通过 DAO 模式,提供了对 iBATIS 的良好支持。 SqlMapClient:是 iBATIS 中的主要接口,通过 xml 配置文件可以让 Spring 容器来管理 SqlMapClient 对象的创...

大风厂蔡成功
2016/07/10
43
0
深入浅出MyBatis_Index

深入浅出MyBatis系列 【深入浅出MyBatis系列一】MyBatis入门 【深入浅出MyBatis系列二】配置简介(MyBatis源码篇) 【深入浅出MyBatis系列三】Mapper映射文件配置 【深入浅出MyBatis系列四】...

陶邦仁
2015/12/22
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

CrashReport

CrashReport.initCrashReport(getApplicationContext(), buglyID, false); https://bugly.qq.com/v2/ https://blog.csdn.net/Crystal_xing/article/details/86249373......

shzwork
8分钟前
0
0
OSChina 周日乱弹 —— 吃这个吮指原味小松鼠

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @这次装个文艺青年吧 :#今日歌曲推荐# 分享Sam Jonsson/Mattafix的单曲《Big City Life (Sam Jonsson Remix)》: 《Big City Life (Sam Jons...

小小编辑
今天
8
0
使用Docker部署第一个Springboot项目

创建springboot项目后pom文件添加 <packaging>jar</packaging> 双击package打包。 双击package即可,最后只要等待控制台输出SUCCESS即可。 我们会在项目中的target文件夹中到自己打包的jar。...

Ryub
今天
7
0
Spring Boot 中使用@DateTimeFormat和@JsonFormat注解

import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data;import lombok.experimental.Accessors;import org.springframework.format.annotation.DateTimeFormat;......

不再熬夜
昨天
6
0
Qt编写图片及视频TCP/UDP网络传输

一、前言 很多年前就做过类似的项目,无非就是将本地的图片上传到服务器,就这么简单,其实用http的post上传比较简单容易,无需自定义协议,直接设置好二进制数据即可,而采用TCP或者UDP通信...

飞扬青云
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部