Mybatis源码解析之初始化分析

Mybatis的初始化过程就是mybatis配置文件的解析过程并将解析结果保存到Configuration类。

一、 Mybatis配置文件

configuation是mybatis配置文件的根节点。

configuration 配置
	-properties 属性
	-settings 设置
	-typeAliases 类型别名
	-typeHandlers 类型处理器
	-objectFactory 对象工厂
	-plugins 插件
	-environments 环境
		-environment 环境变量
			-transactionManager 事务管理器
			-dataSource 数据源
			-databaseIdProvider 数据库厂商标识
	-mappers 映射器

本文主要讲解源码,不对配置文件展开,详细内容可以参考http://www.mybatis.org/mybatis-3/zh/configuration.html#

二、配置文件解析过程

1. 初始化demo

下文代码是mybatis通过解析xml配置文件进行初始化的一个简单demo,后续我们将根据这个demo追踪mybatis源码的处理过程。

InputStream inputStream = Resources.getResourceAsStream("mybatis-coonfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();

Mybatis初始化时序图

2. SqlSessionFactoryBuilder#build(InputStream, String. Properties)

SqlSessionFactoryBuilder的build(inputStream)方法通过调用同名的重载方法SqlSessionFactoryBuilder#build(InputStream, String. Properties)来生成需要的SqlSessionFacotry。

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
   try {
     XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
     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.
     }
   }
 }
   
 public SqlSessionFactory build(Configuration config) {
   return new DefaultSqlSessionFactory(config);
 }

首先,通过XmlConfigBuilder解析配置文件,并得到configuration类对象。
然后根据configuration作为参数调用build得到所需的SqlSessionFactory,可以看到最终得到的SqlSessionFactory类型是DefaultSqlSessionFactory。

3. XMLConfigBuilder#parse()

public Configuration parse() {
   if (parsed) {
     throw new BuilderException("Each XMLConfigBuilder can only be used once.");
   }
   parsed = true;
   parseConfiguration(parser.evalNode("/configuration"));
   return configuration;
 }

 private void parseConfiguration(XNode root) {
   try {
     //issue #117 read properties first
     propertiesElement(root.evalNode("properties"));
     Properties settings = settingsAsProperties(root.evalNode("settings"));
     loadCustomVfs(settings);
     typeAliasesElement(root.evalNode("typeAliases"));
     pluginElement(root.evalNode("plugins"));
     objectFactoryElement(root.evalNode("objectFactory"));
     objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
     reflectorFactoryElement(root.evalNode("reflectorFactory"));
     settingsElement(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);
   }
 }

可以看到,XMLConfigBuilder对xml配置文件中对configuration的子节点进行逐个解析。
以typeAliases为例,遍历typeAliases的子节点,根据配置情况将其加入到configuration对象中对应的typeAliasRegistry属性,其它节点的解析也类似。

private void typeAliasesElement(XNode parent) {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       if ("package".equals(child.getName())) {
         String typeAliasPackage = child.getStringAttribute("name");
         configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
       } else {
         String alias = child.getStringAttribute("alias");
         String type = child.getStringAttribute("type");
         try {
           Class<?> clazz = Resources.classForName(type);
           if (alias == null) {
             typeAliasRegistry.registerAlias(clazz);
           } else {
             typeAliasRegistry.registerAlias(alias, clazz);
           }
         } catch (ClassNotFoundException e) {
           throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
         }
       }
     }
   }
 }

4. DefaultSqlSessionFactory#openSession()

@Override
public SqlSession openSession() {
   return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
 }
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  Transaction tx = null;
  try {
    final Environment environment = configuration.getEnvironment();
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    final Executor executor = configuration.newExecutor(tx, execType);
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}

DefaultSqlSessionFactory#openSession()最终通过DefaultSqlSessionFactory#openSessionFromDataSource(ExecutorType, TransactionLevel, boolean)试下,可以看到最后获得的SqlSession类型是DefaultSqlSession,其中executor类型是SimpleExecutor。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值