MyBatis 学习笔记(六)---源码分析篇---映射文件的解析过程(一)

本文详细解读了MyBatis映射文件中关键元素<cache>和<cache-ref>的解析过程,包括不同配置方式及其实现细节。通过XMLConfigBuilder类解析mappers节点,介绍了自动扫描包、资源引用和类名映射的处理方式。

概述

前面几篇我们介绍了MyBatis中配置文件的解析过程。今天我们接着来看看MyBatis的另外一个核心知识点—映射文件的解析。本文将重点介绍<cache>节点和<cache-ref>的解析。

前置说明

Mapper 映射文件的解析是从XMLConfigBuilder类的对mappers 节点解析开始。mappers节点的配置有很多形式,如下图所示:

 <!-- 映射器 10.1使用类路径-->
	<mappers>
	  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
	  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
	  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
	</mappers>
        <!-- 10.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>
       <!-- 10.3使用java类名-->
	<mappers>
	  <mapper class="org.mybatis.builder.AuthorMapper"/>
	  <mapper class="org.mybatis.builder.BlogMapper"/>
	  <mapper class="org.mybatis.builder.PostMapper"/>
	</mappers>
 <!-- 10.4自动扫描包下所有映射器 -->
	<mappers>
	  <package name="org.mybatis.builder"/>
	</mappers>

mappers的解析入口方法

  private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        if ("package".equals(child.getName())) {
          //10.4自动扫描包下所有映射器
          String mapperPackage = child.getStringAttribute("name");
//          从指定的包中查找mapper接口,并根据mapper接口解析映射配置
          configuration.addMappers(mapperPackage);
        } else {
//          获取resource/url/class等属性
          String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");
            //resource 不为空,且其他两者为空,则从指定路径中加载配置
          if (resource != null && url == null && mapperClass == null) {
            //10.1使用类路径
            ErrorContext.instance().resource(resource);
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //映射器比较复杂,调用XMLMapperBuilder
            //注意在for循环里每个mapper都重新new一个XMLMapperBuilder,来解析
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
            mapperParser.parse();
          } else if (resource == null && url != null && mapperClass == null) {
            //10.2使用绝对url路径
            ErrorContext.instance().resource(url);
            InputStream inputStream = Resources.getUrlAsStream(url);
            //映射器比较复杂,调用XMLMapperBuilder
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
            mapperParser.parse();
          } else if (resource == null && url == null && mapperClass != null) {
            //10.3使用java类名
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            //直接把这个映射加入配置
            configuration.addMapper(mapperInterface);
          } else {
//            以上条件都不满足,则抛出异常
            throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
          }
        }
      }
    }

上述解析方法的主要流程如下流程图所示:
在这里插入图片描述
如上流程图,mappers节点的解析还是比较复杂的,这里我挑几个部分说下。其中

  1. configuration.addMappers(mapperPackage)还是利用ResolverUtil找出包下所有的类,然后循环调用MapperRegistry类的addMapper方法。待会我们在分析这个方法
  2. 配置resource或者url的都需要先创建一个XMLMapperBuilder对象。然后调用XMLMapperBuilder的parse方法。
    首先我们来分析第一部分。

。。。。。。。。。。。。。。。。。


版权原因,完整文章,请参考如下:

 MyBatis 学习笔记(六)---源码分析篇---映射文件的解析过程(一)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值