Mybatis源码学习(17)-Mybatis启动时的初始化过程

一、概述

  在Mybatis项目中,在启动的过程中,需要加载一系列的配置文件,首先需要加载Mybatis-config.xml文件,然后在根据Mybatis-config.xml文件中的配置,加载相关联的其他文件,常见需要加载的文件有Mapper配置文件、jdbc配置文件等。其中,Mapper配置文件可以通过注解等代替对应XML配置文件。

二、初始化过程
1、初始化入口

  Mybatis框架初始化时,是通过SqlSessionFactoryBuilder的build()方法来完成的,首先通过Resources来载入配置文件,然后通过SqlSessionFactoryBuilder的build()方法进行解析和初始化。代码如下:

	InputStream input = Resources.getResourceAsStream("myBatis-temp.xml");
	SqlSessionFactory mySqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
2、SqlSessionFactoryBuilder类

  通过前面的代码可以知道,在构建SqlSessionFactory实例的过程中,完成了对配置文件等进行了初始化操作。SqlSessionFactory实例对象又是通过SqlSessionFactoryBuilder实例对象的build()方法进行创建,下面就详细分析SqlSessionFactoryBuilder类的结构和方法。
  SqlSessionFactoryBuilder类是SqlSession工厂类的构建类,即构建SqlSessionFactory实例,通过分析该类的源码可以发现,主要有三类方法,分别是:

  • build(Reader, String, Properties)
    根据Reader创建SqlSessionFactory实例,通过XMLConfigBuilder类解析读取到的配置文件,并解析成Configuration实例对象, 然后调用build(Configuration)方法,实现创建SqlSessionFactory实例。
  • build(InputStream, String, Properties)
    根据InputStream创建SqlSessionFactory实例,同上面方法类似。
  • build(Configuration)
    根据Configuration创建SqlSessionFactory实例,该方法是真正实现创建SqlSession工厂实例的方法,其他两类方法最后都是通过调用该方法实现创建工厂实例,根据默认的DefaultSqlSessionFactory类创建SqlSessionFactory实例。
  1. build(Reader, String, Properties)方法
    根据Reader创建SqlSessionFactory实例的方法,一共有四个方法,其中下面贴出代码的是最基础的方法,其他三个方法都是通过设置默认参数来实现。
 public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
      return build(parser.parse());
    } 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.
      }
    }
  }
  1. build(InputStream, String, Properties)方法
    和上述方法一样,根据InputStream创建SqlSessionFactory实例的方法,也有四个方法。核心方法代码如下:
 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.
      }
    }
  }
  1. build(Configuration)方法
    根据Configuration创建SqlSessionFactory实例的方法是真正实现创建SqlSession工厂实例的方法,其他两类方法最后都是通过调用该方法实现创建工厂实例。该方法中就是根据默认的DefaultSqlSessionFactory类创建SqlSessionFactory实例。代码如下:
 public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

  通过上面分析的三类build()方法,我们可以发现,创建SqlSessionFactory 实例对象由build(Configuration)方法完成,另外两个方法主要用来加载文件,并通过XMLConfigBuilder类及其parse()方法来生成Configuration对象,其实这个过程就是真正实现配置文件加载的地方。下面继续分析。

三、XMLConfigBuilder类

  XMLConfigBuilder是BaseBuilder的子类,主要负责解析mybatis-config.xml配置文件。

1、字段

其中,XPathParser字段是定义的XPath解析器,提供了一些解析XML的方法,该类的具体用法可以参考《Mybatis源码学习(5)-解析器模块之XNode、XPathParser》;localReflectorFactory实例对象负责创建和缓存Reflector对象,其中Reflector对象缓存了反射操作需要使用的类的元信息,即类的属性、方法等信息,具体使用方法可以参考《Mybatis源码学习(9)-反射模块之Reflector、ReflectorFactory》

 /**
   * 标识是否已经解析过mybatis-config.xml配置文件
   */
  private boolean parsed;
  /**
   * mybatis-config.xml配置文件的XPath解析器,提供了一些解析XML的方法。
   */
  private final XPathParser parser;
  /**
   * 标识<environment>配置的名称,默认读取<environment>标签的default属性
   */
  private String environment;
  /**
   * ReflectorFactory负责创建和缓存Reflector对象
   */
  private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();
2、构造函数

XMLConfigBuilder类的构造函数分为三类,分别是:

  • 以Reader为参数的构造函数
    该类型的构造函数最终通过构建XPathParser对象,然后调用XMLConfigBuilder(XPathParser parser, String environment, Properties props)构造函数实现。
 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);
  }
  • 以InputStream 为参数的构造函数
    该类型的构造函数和上面的一样,最终也是通过构建XPathParser对象,然后调用XMLConfigBuilder(XPathParser parser, String environment, Properties 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);
  }
  • 以XPathParser为参数的构造函数
    该类中的其他构造函数最终都是通过该构造函数实现了对象的初始化工作。
 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;
  }
3、parse()方法

是解析mybatis-config.xml配置文件的入口方法,结果返回Configuration对象,即所有解析后的内容都封装到了Configuration对象中。从下面代码可以看出,首先通过parser.evalNode()方法解析出config配置文件中<configuration>节点对应的XNode对象,然后用该XNode对象作为参数,通过parseConfiguration()方法继续进行解析子节点元素。

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

该方法是真正解析mybatis-config.xml配置文件的方法。

  • <configuration>节点
    <configuration>节点是mybatis-config.xml配置文件的根据节点,该节点下的直接子节点,可以参考mybatis-3-config.dtd中定义的代码。parseConfiguration()方法就是分别解析对应的节点元素。
<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>
  • parseConfiguration()方法
    在该方法中,完成了上述所有节点的解析工作,每个节点元素都对应了一个解析方法,即该方法是所有节点元素解析方法的有序集合。首先通过evalNode()方法解析DOM节点对应的XNode对象,然后把该XNode作为参数,调用对应的解析方法完成解析工作。
  private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
    	//解析配置文件中的properties元素
      propertiesElement(root.evalNode("properties"));
      //解析配置文件中的settings元素
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      //如果settings配置中包含了vfsImpl相关配置,则设置configuration的vfsImpl
      loadCustomVfs(settings);
      //解析配置文件中的typeAliases元素
      typeAliasesElement(root.evalNode("typeAliases"));
      //解析配置文件中的plugins元素
      pluginElement(root.evalNode("plugins"));
      /**
       * 解析配置文件中的objectFactory元素。
       * MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。 
       * 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认构造方法,要么在参数映射存在的时候通过参数构造方法来实例化。
       *  如果想覆盖对象工厂的默认行为,则可以通过创建自己的对象工厂来实现
       */
      objectFactoryElement(root.evalNode("objectFactory"));
      //解析配置文件中的objectWrapperFactory元素
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      //解析配置文件中的reflectorFactory元素
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      //把settings的相关配置赋值到Configuration对应的属性中
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      //解析配置文件中的environments元素
      environmentsElement(root.evalNode("environments"));
      /**
       * 解析配置文件中的databaseIdProvider元素
       * 
       * MyBatis 可以根据不同的数据库厂商执行不同的语句,这种多厂商的支持是基于映射语句中的 databaseId 属性。
       *  MyBatis 会加载不带 databaseId 属性和带有匹配当前数据库 databaseId 属性的所有语句。
       *   如果同时找到带有 databaseId 和不带 databaseId 的相同语句,则后者会被舍弃。 
       * 为支持多厂商特性只要像下面这样在 mybatis-config.xml 文件中加入 databaseIdProvider
       */
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      //解析配置文件中的typeHandlers元素
      typeHandlerElement(root.evalNode("typeHandlers"));
      //解析配置文件中的mappers元素
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }
5、propertiesElement()方法

该方法主要用来解析config配置文件根目录下的<properties>元素,已经在《Mybatis源码学习(4)-解析器模块之标签应用》这篇博客中已经分析过了,这里不再重复。

6、settingsElement()方法

该方法主要用来解析config配置文件根目录下的<settings>元素。在调用该方法前,首先使用settingsAsProperties()方法把<settings>元素中的参数转换成了Properties对象,然后在调用settingsElement()方法,完成对全局唯一变量configuration对象中关于settings参数的初始化工作。

  1. settingsAsProperties()方法
    该方法完成了把<settings>元素中的参数转换成了Properties对象的工作。
 private Properties settingsAsProperties(XNode context) {
    if (context == null) {
      return new Properties();
    }
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
    for (Object key : props.keySet()) {
      if (!metaConfig.hasSetter(String.valueOf(key))) {
        throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
      }
    }
    return props;
  }
  1. settingsElement()方法
    完成对全局唯一变量configuration对象中关于settings参数的初始化工作。
  private void settingsElement(Properties props) throws Exception {
	//指定 MyBatis应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。 FULL 会自动映射任意复杂的结果集(无论是否嵌套)。
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    //指定发现自动映射目标未知列(或者未知属性类型)的行为。
    //NONE: 不做任何反应
    //WARNING: 输出提醒日志 ('org.apache.ibatis.session.AutoMappingUnknownColumnBehavior' 的日志等级必须设置为 WARN)
    //FAILING: 映射失败 (抛出 SqlSessionException)
    configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
    //全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存。
    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
    //指定 Mybatis 创建具有延迟加载能力的对象所用到的代理工具。	CGLIB | JAVASSIST
    configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
    configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
    configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
    configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
    configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    //配置默认的枚举类型处理器
    @SuppressWarnings("unchecked")
    Class<? extends TypeHandler> typeHandler = (Class<? extends TypeHandler>)resolveClass(props.getProperty("defaultEnumTypeHandler"));
    configuration.setDefaultEnumTypeHandler(typeHandler);
    //指定当结果集中值为 null 的时候是否调用映射对象的 setter(map 对象时为 put)方法,这对于有 Map.keySet() 依赖或 null 值初始化的时候是有用的。注意基本类型(int、boolean等)是不能设置成 null 的。
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    //允许使用方法签名中的名称作为语句参数名称。 为了使用该特性,你的工程必须采用Java 8编译,并且加上-parameters选项。(从3.4.1开始)
    configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
    //当返回行的所有列都是空时,MyBatis默认返回null。 当开启这个设置时,MyBatis会返回一个空实例。 请注意,它也适用于嵌套的结果集 (i.e. collectioin and association)。(从3.4.2开始)
    configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
    //指定 MyBatis 增加到日志名称的前缀。
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    //配置mybatis使用的日志实现类
    @SuppressWarnings("unchecked")
    Class<? extends Log> logImpl = (Class<? extends Log>)resolveClass(props.getProperty("logImpl"));
    configuration.setLogImpl(logImpl);
    /**
     * Configuration factory class.
     * Used to create Configuration for loading deserialized unread properties.
     */
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
  }
7、typeAliasesElement()方法

该方法主要用来解析config配置文件根目录下的<typeAliases>元素,具体用法可以参考《Mybatis源码学习(11)-类型处理器之TypeHandlerRegistry、TypeAliasRegistry、Alias、MappedJdbcTypes、MappedTypes》。在该方法中,首先完成了对元素节点的解析,然后调用typeAliasRegistry.registerAlias()方法,实现对别名的注册,其实就是完成对全局唯一变量configuration对象中关于别名定义的初始化工作。

 private void typeAliasesElement(XNode parent) {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
    	  //解析<package name="#"/>类型的子元素
        if ("package".equals(child.getName())) {
          String typeAliasPackage = child.getStringAttribute("name");
          configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
        } else {//解析<typeAlias alias="#" type="#"/>类型的子元素
          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);
          }
        }
      }
    }
  }
8、pluginElement()方法

解析配置文件中的<plugins>元素。

```java
/**
   * 解析配置文件中的插件(plugin),并注册到interceptor中
   * @param parent
   * @throws Exception
   */
  private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        String interceptor = child.getStringAttribute("interceptor");
        Properties properties = child.getChildrenAsProperties();
        Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
        interceptorInstance.setProperties(properties);
        configuration.addInterceptor(interceptorInstance);
      }
    }
  }

关于Mybatis的Plugin插件的相关知识,具体请参考《Mybatis拦截器实现分页插件》

9、objectFactoryElement()方法、objectWrapperFactoryElement()方法、reflectorFactoryElement()方法

分别解析配置文件中的<objectFactory>、<objectWrapperFactory>、<reflectorFactory>元素。其中,关于对象工厂<objectFactory>元素的概述:MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认构造方法,要么在参数映射存在的时候通过参数构造方法来实例化。 如果想覆盖对象工厂的默认行为,则可以通过创建自己的对象工厂来实现。比如:

// ExampleObjectFactory.java
    public class ExampleObjectFactory extends DefaultObjectFactory {
      public Object create(Class type) {
        return super.create(type);
      }
      public Object create(Class type, List constructorArgTypes, List constructorArgs) {
        return super.create(type, constructorArgTypes, constructorArgs);
      }
      public void setProperties(Properties properties) {
        super.setProperties(properties);
      }
      public  boolean isCollection(Class type) {
        return Collection.class.isAssignableFrom(type);
      }}
<!-- mybatis-config.xml -->
<objectFactory type="org.mybatis.example.ExampleObjectFactory">
  <property name="someProperty" value="100"/>
</objectFactory>
10、environmentsElement()方法

解析配置文件中的<environments>元素。具体解析请参考《Mybatis初始化时environments的解析过程》

11、mapperElement()方法

解析配置文件中的<mappers>元素。具体解析请参考《后续单独分析》。

12、databaseIdProviderElement()方法
  1. <databaseIdProvider>元素用法
    解析配置文件中的<databaseIdProvider>元素。MyBatis 可以根据不同的数据库厂商执行不同的语句,这种多厂商的支持是基于映射语句中的 databaseId 属性。 MyBatis 会加载不带 databaseId 属性和带有匹配当前数据库 databaseId 属性的所有语句。 如果同时找到带有 databaseId 和不带 databaseId 的相同语句,则后者会被舍弃。 为支持多厂商特性只要像下面这样在 mybatis-config.xml 文件中加入 databaseIdProvider 即可:
<databaseIdProvider type="DB_VENDOR" />

这里的 DB_VENDOR 会通过 DatabaseMetaData#getDatabaseProductName() 返回的字符串进行设置。 由于通常情况下这个字符串都非常长而且相同产品的不同版本会返回不同的值,所以最好通过设置属性别名来使其变短,如下:

<databaseIdProvider type="DB_VENDOR">
  <property name="SQL Server" value="sqlserver"/>
  <property name="DB2" value="db2"/>        
  <property name="Oracle" value="oracle" />
</databaseIdProvider>

在有 properties 时,DB_VENDOR databaseIdProvider 的将被设置为第一个能匹配数据库产品名称的属性键对应的值,如果没有匹配的属性将会设置为 “null”。 在这个例子中,如果 getDatabaseProductName() 返回"Oracle (DataDirect)",databaseId 将被设置为"oracle"。

你可以通过实现接口 org.apache.ibatis.mapping.DatabaseIdProvider 并在 mybatis-config.xml 中注册来构建自己的 DatabaseIdProvider:

   public interface DatabaseIdProvider {
      void setProperties(Properties p);
      String getDatabaseId(DataSource dataSource) throws SQLException;
   }
  1. 元素解析
  private void databaseIdProviderElement(XNode context) throws Exception {
    DatabaseIdProvider databaseIdProvider = null;
    if (context != null) {
      String type = context.getStringAttribute("type");
      // awful patch to keep backward compatibility
      if ("VENDOR".equals(type)) {
          type = "DB_VENDOR";
      }
      Properties properties = context.getChildrenAsProperties();
      databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
      databaseIdProvider.setProperties(properties);
    }
    Environment environment = configuration.getEnvironment();
    if (environment != null && databaseIdProvider != null) {
      String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
      configuration.setDatabaseId(databaseId);
    }
  }

13、typeHandlerElement()方法
解析配置文件中的<typeHandlers>元素。无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型。<typeHandlers>元素就是定义类型转换处理器的,其实在Mybatis中已经默认配置了很多的类型处理器,基本上可以满足大部分的需求,只有在有特殊需求的时候,才需要自己开发特殊的类型转换器。

<typeHandlers>元素的定义有以下两种方式:

<!-- mybatis-config.xml -->
<typeHandlers>
  <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
</typeHandlers>
<!-- mybatis-config.xml -->
<typeHandlers>
  <package name="org.mybatis.example"/>
</typeHandlers>

在typeHandlerElement()方法中,分别解析了上述提到的两种定义的方式。该方法除了完成基本解析外,还通过TypeHandlerRegistry.register()方法实现了类型处理器注册到全局唯一变量configuration中,具体逻辑可以参考《Mybatis源码学习(11)-类型处理器之TypeHandlerRegistry、TypeAliasRegistry、Alias、MappedJdbcTypes、MappedTypes》

  private void typeHandlerElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        if ("package".equals(child.getName())) {
          String typeHandlerPackage = child.getStringAttribute("name");
          typeHandlerRegistry.register(typeHandlerPackage);
        } else {
          String javaTypeName = child.getStringAttribute("javaType");
          String jdbcTypeName = child.getStringAttribute("jdbcType");
          String handlerTypeName = child.getStringAttribute("handler");
          Class<?> javaTypeClass = resolveClass(javaTypeName);
          JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
          Class<?> typeHandlerClass = resolveClass(handlerTypeName);
          if (javaTypeClass != null) {
            if (jdbcType == null) {
              typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
            } else {
              typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
            }
          } else {
            typeHandlerRegistry.register(typeHandlerClass);
          }
        }
      }
    }
  }
四、结尾

在这篇博客中主要分析了在解析XML配置文件过程中使用的类(SqlSessionFactoryBuilder、XMLConfigBuilder)和方法。其中,XMLConfigBuilder类是解析XML配置文件的核心类,其中几个常用的元素节点,比如<plugins>、<environments>、<mappers>等在专门的博客中进行讲解和分析。

发布了48 篇原创文章 · 获赞 3 · 访问量 3131

猜你喜欢

转载自blog.csdn.net/hou_ge/article/details/102668332
今日推荐