MyBatis 源码解析 —— SqlSessionFactory

发布时间:2020-05-27 12:14:48 作者:Java_老男孩
来源:网络 阅读:496

本文主要分析SqlSessionFactory的构建过程

SqlSessionFactoryBuilder从XML中构建SqlSessionFactory

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

MyBatis 源码解析 —— SqlSessionFactory

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactoryBuilder用来创建SqlSessionFactory实例,一旦创建了SqlSessionFactory,就不再需要它了

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

调用

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties)

源码如下:

/**
   * 根据配置创建SqlSessionFactory
   *
   * @param inputStream 配置文件输入流
   * @param environment 环境名称
   * @param properties  外部配置
   * @return
   */
  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      //parser.parse()读取配置文件返回configuration
      //build()根据返回的configuration创建SqlSessionFactory
      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.
      }
    }
  }

最终创建DefaultSqlSessionFactory实例

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

其中

XMLConfigBuilder与Configuration

MyBatis 源码解析 —— SqlSessionFactory

XMLConfigBuilder的方法parse()

public Configuration parse() {
    if (parsed) {
        throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    //读取mybatis-config.xml配置信息,"configuration"是根结点
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
}

XMLConfigBuilder的方法parseConfiguration(XNode root)

/**
 * 读取配置文件组装configuration
 * @param root 配置文件的configuration节点
 */
private void parseConfiguration(XNode root) {
    try {
        //issue #117 read properties first
        propertiesElement(root.evalNode("properties"));
        typeAliasesElement(root.evalNode("typeAliases"));
        pluginElement(root.evalNode("plugins"));
        objectFactoryElement(root.evalNode("objectFactory"));
        objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        reflectionFactoryElement(root.evalNode("reflectionFactory"));
        settingsElement(root.evalNode("settings"));
        // read it after objectFactory and objectWrapperFactory issue #631
        environmentsElement(root.evalNode("environments"));
        databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        typeHandlerElement(root.evalNode("typeHandlers"));
        mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
}

关于配置文件相关的源码分析参看:

http://www.iocoder.cn/MyBatis/udbwcso/Configuration

设计模式

从SqlSessionFactory和Configuration的创建过程中可以看出它们都使用了建造者模式.

推荐阅读:
  1. Mybatis源码[01.SqlSessionFactoryBuilder]
  2. MyBatis源码浅析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 程序员 mybatis

上一篇:mysql多种安装方法

下一篇:Oracle 11g physical dataguard之快照备用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》