Mybatis源码分析[02.XMLConfigBuilder]

// BaseBuilder属性

public abstract class BaseBuilder {
  //需要配置,类型别名注册,类型处理器注册3个东西
  protected final Configuration configuration;
  protected final TypeAliasRegistry typeAliasRegistry;
  protected final TypeHandlerRegistry typeHandlerRegistry;
}

public class XMLConfigBuilder extends BaseBuilder{},可见XMLConfigBuilder继承自BaseBuilder

// XMLConfigBuilder属性
public class XMLConfigBuilder extends BaseBuilder {

  //是否已解析
  private boolean parsed;
  // XPath解析器
  private XPathParser parser;

  // 当前环境
  private String environment;
}

SqlSessionFactoryBuilder调用XMLConfigBuilder的parse()函数

//  <?xml version="1.0" encoding="UTF-8" ?> 
//  <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
//  "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
//  <configuration> 
//  <environments default="development"> 
//  <environment id="development"> 
//  <transactionManager type="JDBC"/> 
//  <dataSource type="POOLED"> 
//  <property name="driver" value="${driver}"/> 
//  <property name="url" value="${url}"/> 
//  <property name="username" value="${username}"/> 
//  <property name="password" value="${password}"/> 
//  </dataSource> 
//  </environment> 
//  </environments>
//  <mappers> 
//  <mapper resource="org/mybatis/example/BlogMapper.xml"/> 
//  </mappers> 
//  </configuration>

  //解析配置
  public Configuration parse() {
    //如果已经解析过了,报错
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;

    //根节点是configuration
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

通过源码可以看到parse()函数实际上是调用的parseConfiguration()函数,内部实现如下:

private void parseConfiguration(XNode root) {
  try {
    //分步骤解析
    //issue #117 read properties first
    //1.properties
    propertiesElement(root.evalNode("properties"));
    //2.类型别名
    typeAliasesElement(root.evalNode("typeAliases"));
    //3.插件
    pluginElement(root.evalNode("plugins"));
    //4.对象工厂
    objectFactoryElement(root.evalNode("objectFactory"));
    //5.对象包装工厂
    objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
    //6.设置
    settingsElement(root.evalNode("settings"));
    // read it after objectFactory and objectWrapperFactory issue #631
    //7.环境
    environmentsElement(root.evalNode("environments"));
    //8.databaseIdProvider
    databaseIdProviderElement(root.evalNode("databaseIdProvider"));
    //9.类型处理器
    typeHandlerElement(root.evalNode("typeHandlers"));
    //10.映射器
    mapperElement(root.evalNode("mappers"));
  } catch (Exception e) {
    throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
  }
}

以上函数为mybatis对于配置文件的关键节点进行解析处理.

  • propertiesElement(XNode context)
//<properties resource="org/mybatis/example/config.properties">
//    <property name="username" value="dev_user"/>
//    <property name="password" value="F2Fa3!33TYyg"/>
//</properties>
  • typeAliasesElement(XNode context)
//<typeAliases>
//  <typeAlias alias="Author" type="domain.blog.Author"/>
//  <typeAlias alias="Blog" type="domain.blog.Blog"/>
//  <typeAlias alias="Comment" type="domain.blog.Comment"/>
//  <typeAlias alias="Post" type="domain.blog.Post"/>
//  <typeAlias alias="Section" type="domain.blog.Section"/>
//  <typeAlias alias="Tag" type="domain.blog.Tag"/>
//</typeAliases>
  • pluginElement(XNode context)
  //MyBatis 允许你在某一点拦截已映射语句执行的调用。默认情况下,MyBatis 允许使用插件来拦截方法调用
//<plugins>
//  <plugin interceptor="org.mybatis.example.ExamplePlugin">
//    <property name="someProperty" value="100"/>
//  </plugin>
//</plugins>  
  • objectFactoryElement(XNode context)
//<objectFactory type="org.mybatis.example.ExampleObjectFactory">
//  <property name="someProperty" value="100"/>
//</objectFactory>
  • objectWrapperFactoryElement(XNode context)
  • settingsElement(XNode context)
//<settings>
//  <setting name="cacheEnabled" value="true"/>
//  <setting name="lazyLoadingEnabled" value="true"/>
//  <setting name="multipleResultSetsEnabled" value="true"/>
//  <setting name="useColumnLabel" value="true"/>
//  <setting name="useGeneratedKeys" value="false"/>
//  <setting name="enhancementEnabled" value="false"/>
//  <setting name="defaultExecutorType" value="SIMPLE"/>
//  <setting name="defaultStatementTimeout" value="25000"/>
//  <setting name="safeRowBoundsEnabled" value="false"/>
//  <setting name="mapUnderscoreToCamelCase" value="false"/>
//  <setting name="localCacheScope" value="SESSION"/>
//  <setting name="jdbcTypeForNull" value="OTHER"/>
//  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
//</settings>
  • environmentsElement(XNode context)
//  <environments default="development">
//    <environment id="development">
//      <transactionManager type="JDBC">
//        <property name="..." value="..."/>
//      </transactionManager>
//      <dataSource type="POOLED">
//        <property name="driver" value="${driver}"/>
//        <property name="url" value="${url}"/>
//        <property name="username" value="${username}"/>
//        <property name="password" value="${password}"/>
//      </dataSource>
//    </environment>
//  </environments>
  • databaseIdProviderElement(XNode context)
//  <databaseIdProvider type="VENDOR">
//    <property name="SQL Server" value="sqlserver"/>
//    <property name="DB2" value="db2"/>        
//    <property name="Oracle" value="oracle" />
//  </databaseIdProvider>
  • typeHandlerElement(XNode context)
//  <typeHandlers>
//    <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
//  </typeHandlers>
  • mapperElement(XNode context)
//  1.使用类路径
//  <mappers>
//    <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
//    <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
//    <mapper resource="org/mybatis/builder/PostMapper.xml"/>
//  </mappers>
//
//  2.使用绝对url路径
//  <mappers>
//    <mapper url="file:///var/mappers/AuthorMapper.xml"/>
//    <mapper url="file:///var/mappers/BlogMapper.xml"/>
//    <mapper url="file:///var/mappers/PostMapper.xml"/>
//  </mappers>
//
//  3.使用java类名
//  <mappers>
//    <mapper class="org.mybatis.builder.AuthorMapper"/>
//    <mapper class="org.mybatis.builder.BlogMapper"/>
//    <mapper class="org.mybatis.builder.PostMapper"/>
//  </mappers>
//
//  4.自动扫描包下所有映射器
//  <mappers>
//    <package name="org.mybatis.builder"/>
//  </mappers>

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/191845.html

(0)
上一篇 2021年11月14日
下一篇 2021年11月14日

相关推荐

发表回复

登录后才能评论