MyBatis 3源码解析(三)

一、SqlSessionFactory 对象初始化

 //加载全局配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//1.获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

MyBatis 3源码解析(三)

1.调用SqlSessionFactoryBuilder的build(inputStream);方法,方法如下:

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

实际上调用如下方法;

  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.
      }
    }
  }
上一篇:单独使用MyBatis的简单示例


下一篇:mybatis-spring 启动过程和调用过程