温馨提示×

温馨提示×

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

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

Mybatis源码[01.SqlSessionFactoryBuilder]

发布时间:2020-04-30 20:50:35 来源:网络 阅读:737 作者:IMUKL8 栏目:MySQL数据库

可以说每个MyBatis都是以一个SqlSessionFactory实例为中心的。SqlSessionFactory实例可以通过SqlSessionFactoryBuilder来构建。一是可以通过XML配置文件的方式来构建SqlSessionFactory,二是可以通过Java API的方式来构建。但不管通过什么方式都有一个Configuration贯穿始终,各种配置正是通过Configuration实例来完成实现。

public class SqlSessionFactoryBuilder {

  // (1) 从配置文件获取SqlSessionFactory
  public SqlSessionFactory build(Reader reader) {
    return build(reader, null, null);
  }

  // (2) 从配置文件获取SqlSessionFactory,并设定依赖哪种环境参数(开发环境/生产环境)
  public SqlSessionFactory build(Reader reader, String environment) {
    return build(reader, environment, null);
  }

  // (3) 从配置文件获取SqlSessionFactory,并设定依赖哪些配置参数(属性配置文件,那些属性可以用${propName}语法形式多次用在配置文件中)
  public SqlSessionFactory build(Reader reader, Properties properties) {
    return build(reader, null, properties);
  }

  // 通用构建函数-:(1)、(2)、(3)构建函数内部实现均调用的此函数
  public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
    try {
      //委托XMLConfigBuilder来解析xml文件,并返回一个Configuration对象,SqlSessionFactory的生成依赖于此Configuration对象
      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.
      }
    }
  }

  // (4) 从数据流中获取SqlSessionFactory
  public SqlSessionFactory build(InputStream inputStream) {
    return build(inputStream, null, null);
  }

  // (5) 从数据流中获取SqlSessionFactory,并设定依赖哪种环境参数(开发环境/生产环境)
  public SqlSessionFactory build(InputStream inputStream, String environment) {
    return build(inputStream, environment, null);
  }

  // (6) 从数据流中获取SqlSessionFactory,并设定依赖哪些配置参数(属性配置文件,那些属性可以用${propName}语法形式多次用在配置文件中)
  public SqlSessionFactory build(InputStream inputStream, Properties properties) {
    return build(inputStream, null, properties);
  }

  // 通用构建函数二:(4)、(5)、(6)构建函数内部实现均调用此函数
  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作为参数,并返回DefaultSqlSessionFactory对象
  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

}
向AI问一下细节

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

AI