Mybatis工作机制源码分析—初始化

       本文以Spring集成Mybatis的形式,在启动工程过程中,Spring容器会实例化SqlSessionFactoryBean,来讲解Mybatis是如何进行初始化工作。

Spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans"
  	   	xmlns:aop="http://www.springframework.org/schema/aop"
	   	xmlns:tx="http://www.springframework.org/schema/tx"
  	   	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   	xmlns:context="http://www.springframework.org/schema/context"  
	   	xmlns:p="http://www.springframework.org/schema/p"
  	   	xsi:schemaLocation="
          	http://www.springframework.org/schema/beans    
       		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           	http://www.springframework.org/schema/tx      
     		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           	http://www.springframework.org/schema/context
           	http://www.springframework.org/schema/context/spring-context-3.0.xsd
           	http://www.springframework.org/schema/aop
           	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           	http://www.springframework.org/schema/jdbc 
           	http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd ">
           	
	<context:property-placeholder location="classpath:META-INF/dbproperties/*.properties" />

	<!-- 扫描注解文件  -->
	<context:component-scan base-package="org.test" />	
	
	<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
	    <property name="driverClassName" value="${db.driver}"></property>
 		<property name="url" value="${db.url}" />
        <property name="username" value="${db.userName}" />
        <property name="password" value="${db.password}" />
	    <property name="maxTotal" value="10"/>
     	<property name="initialSize" value="5"/>
    	<property name="defaultReadOnly" value="false"/>
    </bean>
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:META-INF/mybatis/config/mybatis-config.xml"></property>
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="${sqlMapperLocations}" />
	</bean>
	
	
	  <!-- 配置sqlSessionTemplate --> 
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">  
        <constructor-arg index="0" ref="sqlSessionFactory" />  
    </bean>

	<!-- 事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 事务注解支持 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
</beans>

SqlSessionFactoryBean与SqlSessionTemplate

       SqlSessionFactoryBean的主要工作为创建SqlSessionFactory实例;SqlSessionFactory再基于connection或DataSource来创建SqlSesion,SqlSesion可理解为一次sql会话,可进行sql语句形式的增删改查以及事务提交回滚等;SqlSessionTemplate则面向Mybatis对外的 DAO的使用者,通过以SqlSesion代理的形式,对外提供基于Spring事务管理的SqlSesion功能,SqlSessionTemplate是线程安全的,可多个DAO共用。

        SqlSessionFactoryBean部分源码:

/** SqlSessionFactoryBean.java */
// 主要工作:创建SqlSessionFactory,在Spring容器中可通过依赖注入的形式被多个DAO共用
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {

  private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);

  private Resource configLocation; // MyBatis config配置文件

  private Resource[] mapperLocations; // MyBatis mapper配置文件

  private DataSource dataSource; // 数据库源(一般带连接池)

  private TransactionFactory transactionFactory;

  private Properties configurationProperties;

  private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

  private SqlSessionFactory sqlSessionFactory; // sqlSession Factory,用来管理sqlSession
  
  ...
  
  // SqlSessionFactoryBean为FactoryBean,在Spring配置文件中直接获取,则获取sqlSessionFactory
  public SqlSessionFactory getObject() throws Exception {
    if (this.sqlSessionFactory == null) {
      afterPropertiesSet();
    }

    return this.sqlSessionFactory;
  }
  
  // SqlSessionFactoryBean为InitializingBean,SqlSessionFactoryBean实例化过程中,主要初始化工作在afterPropertiesSet
  public void afterPropertiesSet() throws Exception {
    notNull(dataSource, "Property 'dataSource' is required");
    notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");

    this.sqlSessionFactory = buildSqlSessionFactory();
  }
  
  // MyBatis初始化的核心工作,主要做两件事情:
  // 1)解析Mybatis config、mapper配置文件;2)用解析结果configuration创建DefaultSqlSessionFactory实例
  protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
      xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
      configuration = xmlConfigBuilder.getConfiguration();
    } else {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
      }
      configuration = new Configuration();
      configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
      configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
      configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
      String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
          ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
      for (String packageToScan : typeAliasPackageArray) {
        configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
        }
      }
    }

    if (!isEmpty(this.typeAliases)) {
      for (Class<?> typeAlias : this.typeAliases) {
        configuration.getTypeAliasRegistry().registerAlias(typeAlias);
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Registered type alias: '" + typeAlias + "'");
        }
      }
    }

    if (!isEmpty(this.plugins)) {
      for (Interceptor plugin : this.plugins) {
        configuration.addInterceptor(plugin);
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Registered plugin: '" + plugin + "'");
        }
      }
    }

    if (hasLength(this.typeHandlersPackage)) {
      String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
          ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
      for (String packageToScan : typeHandlersPackageArray) {
        configuration.getTypeHandlerRegistry().register(packageToScan);
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
        }
      }
    }

    if (!isEmpty(this.typeHandlers)) {
      for (TypeHandler<?> typeHandler : this.typeHandlers) {
        configuration.getTypeHandlerRegistry().register(typeHandler);
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Registered type handler: '" + typeHandler + "'");
        }
      }
    }

    if (xmlConfigBuilder != null) {
      try {
        xmlConfigBuilder.parse();

        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
        }
      } catch (Exception ex) {
        throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
      } finally {
        ErrorContext.instance().reset();
      }
    }

    if (this.transactionFactory == null) {
      this.transactionFactory = new SpringManagedTransactionFactory();
    }

    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

    if (this.databaseIdProvider != null) {
      try {
        configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
      } catch (SQLException e) {
        throw new NestedIOException("Failed getting a databaseId", e);
      }
    }

    if (!isEmpty(this.mapperLocations)) {
      for (Resource mapperLocation : this.mapperLocations) {
        if (mapperLocation == null) {
          continue;
        }

        try {
          XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
              configuration, mapperLocation.toString(), configuration.getSqlFragments());
          xmlMapperBuilder.parse();
        } catch (Exception e) {
          throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
        } finally {
          ErrorContext.instance().reset();
        }

        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
        }
      }
    } else {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
      }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
  }
  
  // SqlSessionFactoryBean为ApplicationListener,响应ApplicationEvent事件
  public void onApplicationEvent(ApplicationEvent event) {
    if (failFast && event instanceof ContextRefreshedEvent) {
      // fail-fast -> check all statements are completed
      this.sqlSessionFactory.getConfiguration().getMappedStatementNames();
    }
  }
  
  ...
}  

        SqlSessionFactory接口:

/** SqlSessionFactory.java */
// SqlSessionFactory基于connection或DataSource来创建SqlSesion
public interface SqlSessionFactory {

  SqlSession openSession();

  SqlSession openSession(boolean autoCommit);
  SqlSession openSession(Connection connection);
  SqlSession openSession(TransactionIsolationLevel level);

  SqlSession openSession(ExecutorType execType);
  SqlSession openSession(ExecutorType execType, boolean autoCommit);
  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType, Connection connection);

  Configuration getConfiguration();

}

       SqlSesion接口概览:


       SqlSessionTemplate部分源码:

/** SqlSessionTemplate.java */
// SqlSessionTemplate以SqlSesion代理的形式,对外提供基于Spring事务管理的SqlSesion功能;
// SqlSessionTemplate是线程安全的,可多个DAO共用
public class SqlSessionTemplate implements SqlSession {

  private final SqlSessionFactory sqlSessionFactory; // 借助sqlSessionFactory来创建SqlSessions

  private final ExecutorType executorType;

  private final SqlSession sqlSessionProxy; // 对外的sqlSession代理,对使用者是透明的

  private final PersistenceExceptionTranslator exceptionTranslator;
  
  ...
  
  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
  }
  
  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
      PersistenceExceptionTranslator exceptionTranslator) {

    notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
    notNull(executorType, "Property 'executorType' is required");

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;
	// JDK动态代理
    this.sqlSessionProxy = (SqlSession) newProxyInstance(
        SqlSessionFactory.class.getClassLoader(),
        new Class[] { SqlSession.class },
        new SqlSessionInterceptor());
  }
  
  private class SqlSessionInterceptor implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      SqlSession sqlSession = getSqlSession(
          SqlSessionTemplate.this.sqlSessionFactory,
          SqlSessionTemplate.this.executorType,
          SqlSessionTemplate.this.exceptionTranslator);
      try {
        Object result = method.invoke(sqlSession, args); // 代理方法调用,实际调用的是sqlSessionFactory创建的sqlSession
        if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
          // force commit even on non-dirty sessions because some databases require
          // a commit/rollback before calling close()
          sqlSession.commit(true);
        }
        return result;
      } catch (Throwable t) {
        Throwable unwrapped = unwrapThrowable(t);
        if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
          // release the connection to avoid a deadlock if the translator is no loaded. See issue #22
          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
          sqlSession = null;
          Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
          if (translated != null) {
            unwrapped = translated;
          }
        }
        throw unwrapped;
      } finally {
        if (sqlSession != null) {
          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
        }
      }
    }
  }

  ...
}

整体流程

         Mybatis初始化核心工作在SqlSessionFactoryBean的实例化,其为FactoryBean且实现InitializingBean接口:

public void afterPropertiesSet() throws Exception {
    notNull(dataSource, "Property 'dataSource' is required");
    notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");

    this.sqlSessionFactory = buildSqlSessionFactory();
  }

buildSqlSessionFactory()则进行初始化工作。

时序图


相关类结构图


相关源码

/** SqlSessionFactoryBean.java */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

	Configuration configuration;

	XMLConfigBuilder xmlConfigBuilder = null;
	if (this.configLocation != null) {
	  // 构建xmlConfigBuilder,核心工作为构造XPathParser:完成Mybatis的配置文件inputStream到Document对象的转化;设置xpath sax解析工具
	  xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
	  // 解析结果configuration
	  configuration = xmlConfigBuilder.getConfiguration();
	} else {
	  if (LOGGER.isDebugEnabled()) {
		LOGGER.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
	  }
	  configuration = new Configuration();
	  configuration.setVariables(this.configurationProperties);
	}

	// configuration设置SqlSessionFactoryBean依赖注入的objectFactory、objectWrapperFactory、typeAliasesPackage
	// typeAliases、plugins、typeHandlersPackage、typeHandlers实例到相应的属性
	if (this.objectFactory != null) {
	  configuration.setObjectFactory(this.objectFactory);
	}

	if (this.objectWrapperFactory != null) {
	  configuration.setObjectWrapperFactory(this.objectWrapperFactory);
	}

	if (hasLength(this.typeAliasesPackage)) {
	  String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
		  ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
	  for (String packageToScan : typeAliasPackageArray) {
		configuration.getTypeAliasRegistry().registerAliases(packageToScan,
				typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
		}
	  }
	}

	if (!isEmpty(this.typeAliases)) {
	  for (Class<?> typeAlias : this.typeAliases) {
		configuration.getTypeAliasRegistry().registerAlias(typeAlias);
		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Registered type alias: '" + typeAlias + "'");
		}
	  }
	}

	if (!isEmpty(this.plugins)) {
	  for (Interceptor plugin : this.plugins) {
		configuration.addInterceptor(plugin);
		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Registered plugin: '" + plugin + "'");
		}
	  }
	}

	if (hasLength(this.typeHandlersPackage)) {
	  String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
		  ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
	  for (String packageToScan : typeHandlersPackageArray) {
		configuration.getTypeHandlerRegistry().register(packageToScan);
		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
		}
	  }
	}

	if (!isEmpty(this.typeHandlers)) {
	  for (TypeHandler<?> typeHandler : this.typeHandlers) {
		configuration.getTypeHandlerRegistry().register(typeHandler);
		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Registered type handler: '" + typeHandler + "'");
		}
	  }
	}

	if (xmlConfigBuilder != null) {
	  try {
		// 解析MyBatis config配置文件
		xmlConfigBuilder.parse();

		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
		}
	  } catch (Exception ex) {
		throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
	  } finally {
		ErrorContext.instance().reset();
	  }
	}

	// configuration设置environment、databaseId
	if (this.transactionFactory == null) {
	  // 采用Spring管理的TransactionFactory
	  this.transactionFactory = new SpringManagedTransactionFactory();
	}

	// 覆盖Mybatis config配置文件中配置的Environment,主要涉及SpringManagedTransactionFactory
	configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

	if (this.databaseIdProvider != null) {
	  try {
		configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
	  } catch (SQLException e) {
		throw new NestedIOException("Failed getting a databaseId", e);
	  }
	}

	if (!isEmpty(this.mapperLocations)) {
	  for (Resource mapperLocation : this.mapperLocations) {
		if (mapperLocation == null) {
		  continue;
		}

		try {
		  // 构造XMLMapperBuilder
		  XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
			  configuration, mapperLocation.toString(), configuration.getSqlFragments());
		  // 解析MyBatis mapper配置文件
		  xmlMapperBuilder.parse();
		} catch (Exception e) {
		  throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
		} finally {
		  ErrorContext.instance().reset();
		}

		if (LOGGER.isDebugEnabled()) {
		  LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
		}
	  }
	} else {
	  if (LOGGER.isDebugEnabled()) {
		LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
	  }
	}

	// 返回DefaultSqlSessionFactory
	return this.sqlSessionFactoryBuilder.build(configuration);
}

详细流程
构造XMLConfigBuilder

/** XMLConfigBuilder.java */
// XMLConfigBuilder构造器
public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
	// XMLMapperEntityResolver用于加载"org/apache/ibatis/builder/xml/mybatis-3-config.dtd"、
	// "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd"为InputSource,用于sax解析Mybatis的config、mapper配置文件
	// MyBatis采用dtd模式来解析其配置文件
	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;
}

/** XPathParser.java */
// XPathParser构造函数,完成Mybatis的配置文件inputStream到Document对象的转化
public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
	commonConstructor(validation, variables, entityResolver);
	this.document = createDocument(new InputSource(inputStream));
}

// 设置解析工具
private void commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {
	this.validation = validation;
	this.entityResolver = entityResolver;
	this.variables = variables;
	XPathFactory factory = XPathFactory.newInstance();
	// 借助javax.xml.xpath.XPath辅助XPathParser解析
	this.xpath = factory.newXPath();
}

// 将Mybatis的配置文件inputStream转化为Document对象
private Document createDocument(InputSource inputSource) {
	// important: this must only be called AFTER common constructor
	try {
	  // 利用javax.xml.parsers.DocumentBuilderFactory来创建DocumentBuilder
	  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	  factory.setValidating(validation);

	  factory.setNamespaceAware(false);
	  factory.setIgnoringComments(true);
	  factory.setIgnoringElementContentWhitespace(false);
	  factory.setCoalescing(false);
	  factory.setExpandEntityReferences(true);

	  DocumentBuilder builder = factory.newDocumentBuilder();
	  // 设置javax.xml.parsers.DocumentBuilder
	  builder.setEntityResolver(entityResolver);
	  builder.setErrorHandler(new ErrorHandler() {
		@Override
		public void error(SAXParseException exception) throws SAXException {
		  throw exception;
		}

		@Override
		public void fatalError(SAXParseException exception) throws SAXException {
		  throw exception;
		}

		@Override
		public void warning(SAXParseException exception) throws SAXException {
		}
	  });
	  // inputSource解析为Document对象
	  return builder.parse(inputSource);
	} catch (Exception e) {
	  throw new BuilderException("Error creating document instance.  Cause: " + e, e);
	}
}

Mybatis 配置文件sax解析

详见:Mybatis工作机制源码分析—初始化—sax解析

Mybatis config配置文件解析

详见:Mybatis工作机制源码分析—初始化—config配置文件解析

Mybatis mapper配置文件解析

详见:Mybatis工作机制源码分析—初始化—mapper配置文件解析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值