mybatis之SqlSessionFactory

版本:mybatis-3java

主要分析SqlSessionFactory的構建過程sql

SqlSessionFactoryBuilder從XML中構建SqlSessionFactory設計模式

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

 

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactoryBuilder用來建立SqlSessionFactory實例,一旦建立了SqlSessionFactory,就再也不須要它了mybatis

public SqlSessionFactory build(InputStream inputStream) {
  return build(inputStream, null, null);
}

調用app

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties)

源碼以下:ide

/**
   * 根據配置建立SqlSessionFactory
   *
   * @param inputStream 配置文件輸入流
   * @param environment 環境名稱
   * @param properties  外部配置
   * @return
   */
  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      //parser.parse()讀取配置文件返回configuration
      //build()根據返回的configuration建立SqlSessionFactory
      return build(parser.parse());
    } 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.
      }
    }
  }

最終建立DefaultSqlSessionFactory實例源碼分析

public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

其中ui

XMLConfigBuilder與Configurationspa

XMLConfigBuilder的方法parse().net

public Configuration parse() {
	if (parsed) {
	    throw new BuilderException("Each XMLConfigBuilder can only be used once.");
	}
	parsed = true;
	//讀取mybatis-config.xml配置信息,"configuration"是根結點
	parseConfiguration(parser.evalNode("/configuration"));
	return configuration;
}

XMLConfigBuilder的方法parseConfiguration(XNode root)

/**
 * 讀取配置文件組裝configuration
 * @param root 配置文件的configuration節點
 */
private void parseConfiguration(XNode root) {
	try {
		//issue #117 read properties first
		propertiesElement(root.evalNode("properties"));
		typeAliasesElement(root.evalNode("typeAliases"));
		pluginElement(root.evalNode("plugins"));
		objectFactoryElement(root.evalNode("objectFactory"));
		objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
		reflectionFactoryElement(root.evalNode("reflectionFactory"));
		settingsElement(root.evalNode("settings"));
		// read it after objectFactory and objectWrapperFactory issue #631
		environmentsElement(root.evalNode("environments"));
		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);
	}
}

關於配置文件相關的源碼分析參看:

http://www.javashuo.com/article/p-qtlxiwbi-ht.html

設計模式

從SqlSessionFactory和Configuration的建立過程當中能夠看出它們都使用了建造者模式.

相關文章
相關標籤/搜索