在 <MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置與使用> 的demo中看到了SessionFactory的建立過程:html
SqlSessionFactory sessionFactory = null;
String resource = "mybatisConfig.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
那麼咱們就從SqlSessionFactoryBuilder開始,看看Mybatis的加載過程。java
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實例。apache
接下來咱們看看 XMLConfigBuilder 是怎樣解析Mybatis的配置文件的,下面是部分源碼:session
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 {
//解析子節點properties propertiesElement(root.evalNode("properties")); //issue #117 read properties first
//解析子節點typeAliases 別名 typeAliasesElement(root.evalNode("typeAliases"));
//解析子節點plugins 插件 pluginElement(root.evalNode("plugins"));
//解析子節點objectFactory mybatis爲結果建立對象時都會用到objectFactory objectFactoryElement(root.evalNode("objectFactory"));
//解析子節點objectWrapperFactory objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析settings定義一些全局性的配置 settingsElement(root.evalNode("settings"));
//解析environments 能夠配置多個運行環境,可是每一個SqlSessionFactory 實例只能選擇一個運行環境 environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
//解析databaseIdProvider MyBatis可以執行不一樣的語句取決於你提供的數據庫供應商。許多數據庫供應商的支持是基於databaseId映射 databaseIdProviderElement(root.evalNode("databaseIdProvider"));
//解析typeHandlers 當MyBatis設置參數到PreparedStatement 或者從ResultSet 結果集中取得值時,就會使用TypeHandler 來處理數據庫類型與java 類型之間轉換 typeHandlerElement(root.evalNode("typeHandlers"));
//解析mappers 主要的crud操做都是在mappers中定義的 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