mybatis源码分析一之SqlSessionFactory

环境准备

  1. mybatis 3.1.0

SqlSessionFactory

  1. sqlSessionFactory是Mybatis的核心类之一,最重要的功能就是创建SqlSession.SqlSessionFactory应该是单例的,如果采用多例则它对数据库连接的消耗是很大的,所以每一个数据库只对应一个SqlSessionFactory,避免过多的Connection被消耗
  2. SqlSessionFactoryBuilder构建SqlSessionFactory
    • 通过XMLConfigBuilder解析xml文件读取配置参数,并存入Configuration类中(mybatis几乎所有配置都存放在这里)
    • 使用Configuration对象去创建SqlSessionFactory(默认实现是DefaultSqlSessionFactory)
    • SqlSessionFactoryBuilder作用就是构建器,一旦我们创建了SqlSessionFactory,生命周期就结束了,此时应该回收。所以它的生命周期只存在于方法的局部

SqlSessionFactoryBuilder

  1. 源码分析
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);
  }
 //mybatis配置文件 + properties, 此时mybatis配置文件中可以不配置properties,也能使用${}形式
  public SqlSessionFactory build(Reader reader, Properties properties) {
    return build(reader, null, properties);
  }

  //通过XMLConfigBuilder解析mybatis配置,然后创建SqlSessionFactory对象
  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) {
    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);
  }

  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.
      }
    }
  }
  /**
  这里是写死的,创建的是DefaultSqlSessionFactory
  ***/  
  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

}

XMLConfigBuilder

  1. 源码分析
/***
`configuration`为根节点
在configuration节点之下,我们可以配置10个子节点, 分别为:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。
***/
public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each MapperConfigParser can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

 private void parseConfiguration(XNode root) {
    try {
      propertiesElement(root.evalNode("properties")); //issue #117 read properties first 全局参数
      typeAliasesElement(root.evalNode("typeAliases"));//别名
      pluginElement(root.evalNode("plugins"));//插件(拦截器)
      objectFactoryElement(root.evalNode("objectFactory"));//对象
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      settingsElement(root.evalNode("settings"));//设置
      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);
    }
  }

Configuration

  1. 作用
    • 读入配置文件,基础配置文件比如Configuration.xml和bean的映射mapper文件
    • 初始化基础配置,比如别名,插件,映射器,ObjectFactory等

猜你喜欢

转载自blog.csdn.net/usagoole/article/details/80639235
今日推荐