深刻淺出Mybatis系列(二)---配置簡介(mybatis源碼篇)[轉]

上篇文章《深刻淺出Mybatis系列(一)---Mybatis入門》, 寫了一個Demo簡單體現了一下Mybatis的流程。本次,將簡單介紹一下Mybatis的配置文件:html

上次例子中,咱們以 SqlSessionFactoryBuilder 去建立 SqlSessionFactory,  那麼,咱們就先從SqlSessionFactoryBuilder入手, 我們先看看源碼是怎麼實現的:mybatis

SqlSessionFactoryBuilder源碼片斷:app

 
 1 public class SqlSessionFactoryBuilder {
 2 
 3   //Reader讀取mybatis配置文件,傳入構造方法
 4   //除了Reader外,其實還有對應的inputStream做爲參數的構造方法,
 5   //這也體現了mybatis配置的靈活性
 6   public SqlSessionFactory build(Reader reader) {
 7     return build(reader, null, null);
 8   }
 9 
10   public SqlSessionFactory build(Reader reader, String environment) {
11     return build(reader, environment, null);
12   }
13   
14   //mybatis配置文件 + properties, 此時mybatis配置文件中能夠不配置properties,也能使用${}形式
15   public SqlSessionFactory build(Reader reader, Properties properties) {
16     return build(reader, null, properties);
17   }
18   
19   //經過XMLConfigBuilder解析mybatis配置,而後建立SqlSessionFactory對象
20   public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
21     try {
22       XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
23       //下面看看這個方法的源碼
24       return build(parser.parse());
25     } catch (Exception e) {
26       throw ExceptionFactory.wrapException("Error building SqlSession.", e);
27     } finally {
28       ErrorContext.instance().reset();
29       try {
30         reader.close();
31       } catch (IOException e) {
32         // Intentionally ignore. Prefer previous error.
33       }
34     }
35   }
36 
37   public SqlSessionFactory build(Configuration config) {
38     return new DefaultSqlSessionFactory(config);
39   }
40 
41 }
 

經過源碼,咱們能夠看到SqlSessionFactoryBuilder 經過XMLConfigBuilder 去解析咱們傳入的mybatis的配置文件, 下面就接着看看 XMLConfigBuilder 部分源碼:ide

 
 1 /**
 2  * mybatis 配置文件解析
 3  */
 4 public class XMLConfigBuilder extends BaseBuilder {
 5   public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
 6     this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
 7   }
 8 
 9   private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
10     super(new Configuration());
11     ErrorContext.instance().resource("SQL Mapper Configuration");
12     this.configuration.setVariables(props);
13     this.parsed = false;
14     this.environment = environment;
15     this.parser = parser;
16   }
17   
18   //外部調用此方法對mybatis配置文件進行解析
19   public Configuration parse() {
20     if (parsed) {
21       throw new BuilderException("Each XMLConfigBuilder can only be used once.");
22     }
23     parsed = true;
24     //從根節點configuration
25     parseConfiguration(parser.evalNode("/configuration"));
26     return configuration;
27   }
28 
29   //此方法就是解析configuration節點下的子節點
30   //由此也可看出,咱們在configuration下面能配置的節點爲如下10個節點
31   private void parseConfiguration(XNode root) {
32     try {
33       propertiesElement(root.evalNode("properties")); //issue #117 read properties first
34       typeAliasesElement(root.evalNode("typeAliases"));
35       pluginElement(root.evalNode("plugins"));
36       objectFactoryElement(root.evalNode("objectFactory"));
37       objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
38       settingsElement(root.evalNode("settings"));
39       environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
40       databaseIdProviderElement(root.evalNode("databaseIdProvider"));
41       typeHandlerElement(root.evalNode("typeHandlers"));
42       mapperElement(root.evalNode("mappers"));
43     } catch (Exception e) {
44       throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
45     }
46   }
47 }

經過以上源碼,咱們就能看出,在mybatis的配置文件中:ui

1. configuration節點爲根節點。this

2. 在configuration節點之下,咱們能夠配置10個子節點, 分別爲:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。code

 

本篇文章就先只介紹這些內容,接下來的文章將依次分析解析這個10個節點中比較重要的幾個節點的源碼,看看在解析這些節點的時候,到底作了些什麼。htm

相關文章
相關標籤/搜索