MyBatis 源码解析 —— SqlSessionFactory

本文分析了SqlSessionFactory的构建过程,SqlSessionFactoryBuilder从XML中构建其实例,最终创建DefaultSqlSessionFactory。还涉及XMLConfigBuilder相关方法。此外,从SqlSessionFactory和Configuration的创建过程可知,它们运用了建造者模式。

本文主要分析SqlSessionFactory的构建过程

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,就不再需要它了

public SqlSessionFactory build(InputStream inputStream) {
  return build(inputStream, null, null);
}
复制代码

调用

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties)
复制代码

源码如下:

/**
   * 根据配置创建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);
}
复制代码

其中

XMLConfigBuilder与Configuration

XMLConfigBuilder的方法parse()

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);
	}
}
复制代码

关于配置文件相关的源码分析参看:

www.iocoder.cn/MyBatis/udb…

设计模式

从SqlSessionFactory和Configuration的创建过程中可以看出它们都使用了建造者模式.

转载于:https://juejin.im/post/5ce79e24518825332a1ef480

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值