1. 程式人生 > >MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder

MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder

.cn 創建 ron 子節點 homepage 解析 調用 sco title

在 <MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置與使用> 的demo中看到了SessionFactory的創建過程:

SqlSessionFactory sessionFactory = null;
        String resource = "mybatisConfig.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources
                    .getResourceAsReader(resource));
        } catch (IOException e) {
            e.printStackTrace();
        }

那麽我們就從SqlSessionFactoryBuilder開始,看看Mybatis的加載過程。

SqlSessionFactoryBuilder的核心源碼:

package org.apache.ibatis.session;
public class SqlSessionFactoryBuilder {
//通過Reader讀取Mybatis配置
 public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
    try {      
      XMLConfigBuilder parser 
= new XMLConfigBuilder(reader, environment, properties); return build(parser.parse()); //parse()方法得到Configuration } 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. } } }
//通過InputStream讀取Mybatis配置
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { try { XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties); return build(parser.parse()); //parse()方法得到Configuration } 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. } } }
//以上2個方法最終調用的是build(Configuration config)
public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); } }

通過源碼,我們可以看到SqlSessionFactoryBuilder 通過XMLConfigBuilder 去解析我們傳入的mybatis的配置文件,構造出Configuration,最終返回new DefaultSqlSessionFactory(config)的SqlSessionFactory實例。

接下來我們看看 XMLConfigBuilder 是怎樣解析Mybatis的配置文件的,下面是部分源碼:

package org.apache.ibatis.builder.xml;


/**
 * 解析Mybatis配置文件
 */
public class XMLConfigBuilder extends BaseBuilder {

  private boolean parsed;
  private XPathParser parser;
  private String environment;

  public XMLConfigBuilder(Reader reader) {
    this(reader, null, null);
  }

  public XMLConfigBuilder(Reader reader, String environment) {
    this(reader, environment, null);
  }

  public XMLConfigBuilder(Reader reader, String environment, Properties props) {
    this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
  }

  public XMLConfigBuilder(InputStream inputStream) {
    this(inputStream, null, null);
  }

  public XMLConfigBuilder(InputStream inputStream, String environment) {
    this(inputStream, environment, null);
  }

  public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
    this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
  }

  private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
    super(new Configuration());
    ErrorContext.instance().resource("SQL Mapper Configuration");
    this.configuration.setVariables(props);
    this.parsed = false;
    this.environment = environment;
    this.parser = parser;
  }

  //調用此方法對mybatis配置文件進行解析,返回Configuration對象
  public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    //從根節點configuration,開始解析
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }
  
  //解析configuration節點下的10個子節點。
  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")); // read it after objectFactory and objectWrapperFactory issue #631
      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);
    }
  }
  
}  

從上面可以看出可以配置10個子節點, 分別為:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。

MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder