上篇文章《深入淺出Mybatis系列(一)---Mybatis入門》, 寫了一個Demo簡單體現了一下Mybatis的流程。本次,將簡單介紹一下Mybatis的配置文件:
上次例子中,我們以 SqlsessionFactoryBuilder 去創建 SqlSessionFactory, 那么,我們就先從SqlSessionFactoryBuilder入手, 咱們先看看源碼是怎么實現的:
SqlSessionFactoryBuilder源碼片段:
public class SqlSessionFactoryBuilder { //Reader讀取mybatis配置文件,傳入構造方法 //除了Reader外,其實還有對應的inputStream作為參數的構造方法, //這也體現了mybatis配置的靈活性 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(Configuration config) { return new DefaultSqlSessionFactory(config); }}通過源碼,我們可以看到SqlSessionFactoryBuilder 通過XMLConfigBuilder 去解析我們傳入的mybatis的配置文件, 下面就接著看看 XMLConfigBuilder 部分源碼:/** * mybatis 配置文件解析 */public class XMLConfigBuilder extends BaseBuilder { 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配置文件進行解析 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節點下的子節點 //由此也可看出,我們在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); } }}通過以上源碼,我們就能看出,在mybatis的配置文件中:
1. configuration節點為根節點。
2. 在configuration節點之下,我們可以配置10個子節點, 分別為:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。
本篇文章就先只介紹這些內容,接下來的文章將依次分析解析這個10個節點中比較重要的幾個節點的源碼,看看在解析這些節點的時候,到底做了些什么。
|
新聞熱點
疑難解答