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

來源:互聯網
上載者:User

標籤:看到了   iss   page   mybatis   接下來   erro   null   log   this   

在 <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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.