Mybatis源码[01.SqlSessionFactoryBuilder]

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

成都创新互联公司专业为企业提供五指山网站建设、五指山做网站、五指山网站设计、五指山网站制作等企业网站建设、网页设计与制作、五指山企业网站模板建站服务,10余年五指山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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);
  }

}

分享题目:Mybatis源码[01.SqlSessionFactoryBuilder]
网址分享:https://www.cdcxhl.com/article42/igjhhc.html

成都网站建设公司_创新互联,为您提供App设计全网营销推广网站设计网站改版网页设计公司外贸网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设