上一篇咱們經過對mybatis源碼的簡單分析,可看出,在mybatis配置文件中,在configuration根節點下面,可配置properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers這些節點。那麼本次,就會先介紹properties節點和environments節點。 java
爲了讓你們可以更好地閱讀mybatis源碼,我先簡單的給你們示例一下properties的使用方法。 mysql
<!-- 方法一: 從外部指定properties配置文件, 除了使用resource屬性指定外,還可經過url屬性指定url <properties resource="dbConfig.properties"></properties> --> <!-- 方法二: 直接配置爲xml --> <properties> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test1"/> <property name="username" value="root"/> <property name="password" value="root"/> </properties>
那麼,我要是 兩種方法都同時用了,那麼哪一種方法優先? sql
當以上兩種方法都有時,xml配置優先, 外部指定properties配置其次。至於爲何,接下來的源碼分析會提到,請留意一下。 數據庫
再看一下envirements元素節點的使用方法吧: mybatis
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 若是上面沒有指定數據庫配置的properties文件,那麼此處能夠這樣直接配置 <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test1"/> <property name="username" value="root"/> <property name="password" value="root"/> --> <!-- 上面指定了數據庫配置文件, 配置文件裏面也是對應的這四個屬性 --> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <!-- 我再指定一個environment --> <environment id="test"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <!-- 與上面的url不同 --> <property name="url" value="jdbc:mysql://localhost:3306/demo"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments>
environments元素節點能夠配置多個environment子節點, 怎麼理解呢? app
假如咱們系統的開發環境和正式環境所用的數據庫不同(這是確定的), 那麼能夠設置兩個environment, 兩個id分別對應開發環境(dev)和正式環境(final),那麼經過配置environments的default屬性就能選擇對應的environment了, 例如,我將environments的deault屬性的值配置爲dev, 那麼就會選擇dev的environment。 至於這個是怎麼實現的, 下面源碼就會講。 ide
好啦,上面簡單給你們介紹了一下properties 和 environments 的配置, 接下來就正式開始看源碼了: 源碼分析
上次咱們說過mybatis 是經過XMLConfigBuilder這個類在解析mybatis配置文件的,那麼本次就接着看看XMLConfigBuilder對於properties和environments的解析: 測試
XMLConfigBuilder: ui
public class XMLConfigBuilder extends BaseBuilder { private boolean parsed; //xml解析器 private XPathParser parser; private String environment; //上次說到這個方法是在解析mybatis配置文件中能配置的元素節點 //今天首先要看的就是properties節點和environments節點 private void parseConfiguration(XNode root) { try { //解析properties元素 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")); //解析environments元素 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); } } //下面就看看解析properties的具體方法 private void propertiesElement(XNode context) throws Exception { if (context != null) { //將子節點的 name 以及value屬性set進properties對象 //這兒能夠注意一下順序,xml配置優先, 外部指定properties配置其次 Properties defaults = context.getChildrenAsProperties(); //獲取properties節點上 resource屬性的值 String resource = context.getStringAttribute("resource"); //獲取properties節點上 url屬性的值, resource和url不能同時配置 String url = context.getStringAttribute("url"); if (resource != null && url != null) { throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other."); } //把解析出的properties文件set進Properties對象 if (resource != null) { defaults.putAll(Resources.getResourceAsProperties(resource)); } else if (url != null) { defaults.putAll(Resources.getUrlAsProperties(url)); } //將configuration對象中已配置的Properties屬性與剛剛解析的融合 //configuration這個對象會裝載所解析mybatis配置文件的全部節點元素,之後也會頻頻提到這個對象 //既然configuration對象用有一系列的get/set方法, 那是否就標誌着咱們可使用java代碼直接配置? //答案是確定的, 不過使用配置文件進行配置,優點不言而喻 Properties vars = configuration.getVariables(); if (vars != null) { defaults.putAll(vars); } //把裝有解析配置propertis對象set進解析器, 由於後面可能會用到 parser.setVariables(defaults); //set進configuration對象 configuration.setVariables(defaults); } } //下面再看看解析enviroments元素節點的方法 private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { //解析environments節點的default屬性的值 //例如: <environments default="development"> environment = context.getStringAttribute("default"); } //遞歸解析environments子節點 for (XNode child : context.getChildren()) { //<environment id="development">, 只有enviroment節點有id屬性,那麼這個屬性有何做用? //environments 節點下能夠擁有多個 environment子節點 //相似於這樣: <environments default="development"><environment id="development">...</environment><environment id="test">...</environments> //意思就是咱們能夠對應多個環境,好比開發環境,測試環境等, 由environments的default屬性去選擇對應的enviroment String id = child.getStringAttribute("id"); //isSpecial就是根據由environments的default屬性去選擇對應的enviroment if (isSpecifiedEnvironment(id)) { //事務, mybatis有兩種:JDBC 和 MANAGED, 配置爲JDBC則直接使用JDBC的事務,配置爲MANAGED則是將事務託管給容器, TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); //enviroment節點下面就是dataSource節點了,解析dataSource節點(下面會貼出解析dataSource的具體方法) DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dataSource); //老規矩,會將dataSource設置進configuration對象 configuration.setEnvironment(environmentBuilder.build()); } } } } //下面看看dataSource的解析方法 private DataSourceFactory dataSourceElement(XNode context) throws Exception { if (context != null) { //dataSource的鏈接池 String type = context.getStringAttribute("type"); //子節點 name, value屬性set進一個properties對象 Properties props = context.getChildrenAsProperties(); //建立dataSourceFactory DataSourceFactory factory = (DataSourceFactory) resolveClass(type).newInstance(); factory.setProperties(props); return factory; } throw new BuilderException("Environment declaration requires a DataSourceFactory."); } }
還有一個問題, 上面咱們看到,在配置dataSource的時候使用了 ${driver} 這種表達式, 這種形式是怎麼解析的?其實,是經過PropertyParser這個類解析:
PropertyParser:
/** * 這個類解析${}這種形式的表達式 */ public class PropertyParser { public static String parse(String string, Properties variables) { VariableTokenHandler handler = new VariableTokenHandler(variables); GenericTokenParser parser = new GenericTokenParser("${", "}", handler); return parser.parse(string); } private static class VariableTokenHandler implements TokenHandler { private Properties variables; public VariableTokenHandler(Properties variables) { this.variables = variables; } public String handleToken(String content) { if (variables != null && variables.containsKey(content)) { return variables.getProperty(content); } return "${" + content + "}"; } } }好啦,以上就是對於properties 和 environments元素節點的分析,比較重要的都在對於源碼的註釋中標出。本次文章到此結束,接下來的文章會繼續分析其餘節點的配置。