zoukankan      html  css  js  c++  java
  • mybatis源码-解析配置文件(四)之配置文件Mapper解析

    mybatis源码-解析配置文件(三)之配置文件Configuration解析 中, 讲解了 Configuration 是如何解析的。

    其中, mappers作为configuration节点的一部分配置, 在本文章中, 我们讲解解析mappers节点, 即 xxxMapper.xml 文件的解析。

    1 解析入口

    在解析 mybatis-config.xml 时, 会进行解析 xxxMapper.xml 的文件。

    解析的入口
    在图示流程的 XMLConfigBuilder.parse() 函数中, 该函数内部, 在解析 mappers 节点时, 会调用 mapperElement(root.evalNode("mappers"))

    private void mapperElement(XNode parent) throws Exception {
        if (parent != null) {
          // 遍历其子节点
          for (XNode child : parent.getChildren()) {
            // 如果配置的是包(packege)
            if ("package".equals(child.getName())) {
              String mapperPackage = child.getStringAttribute("name");
              configuration.addMappers(mapperPackage);
            } else {
              // 如果配置的是类(有三种情况 resource / class / url)
              String resource = child.getStringAttribute("resource");
              String url = child.getStringAttribute("url");
              String mapperClass = child.getStringAttribute("class");
              // 配置一:使用 resource 类路径
              if (resource != null && url == null && mapperClass == null) {
                ErrorContext.instance().resource(resource);
                InputStream inputStream = Resources.getResourceAsStream(resource);
                // 创建 XMLMapperBuilder 对象
                XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
                // 解析 xxxMapper.xml 
                mapperParser.parse();
                // 配置二: 使用 url 绝对路径
              } else if (resource == null && url != null && mapperClass == null) {
                ErrorContext.instance().resource(url);
                InputStream inputStream = Resources.getUrlAsStream(url);
                // 创建 XMLMapperBuilder 对象
                XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
                // 解析 xxxMapper.xml 
                mapperParser.parse();
                // 配置三: 使用 class 类名
              } else if (resource == null && url == null && mapperClass != null) {
                // 通过反射创建对象
                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.");
              }
            }
          }
        }
    }
    

    从以上源码中可以发现, 配置时, 一种是通过包的方式, 一种是通过指定文件的方式。

    但不管是怎么配置, 最后的找落点都是 xxxMapper.xml 文件的解析。

    2 解析

    包扫描时, 会加载指定包下的文件, 最终会调用

    private void loadXmlResource() {
        // 判断是否已经加载过
        if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
          String xmlResource = type.getName().replace('.', '/') + ".xml";
          InputStream inputStream = null;
          try {
            inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
          } catch (IOException e) {
            // ignore, resource is not required
          }
          if (inputStream != null) {
            XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
            // 解析
            xmlParser.parse();
          }
        }
    }
    

    因此, 不管是包扫描还是文件扫描, 最终都经历一下 xmlParser.parse() 解析过程。

    2.1 解析流程

    解析 xxxMapper.xml 文件的是下面这个函数,解析 mapper 节点。

      public void parse() {
        // 判断是否已经加载过
        if (!configuration.isResourceLoaded(resource)) {
          // 解析 <mapper> 节点
          configurationElement(parser.evalNode("/mapper"));
          // 标记一下,已经加载过了
          configuration.addLoadedResource(resource);
          // 绑定映射器到namespace
          bindMapperForNamespace();
        }
        // 处理 configurationElement 中解析失败的<resultMap>
        parsePendingResultMaps();
        // 处理configurationElement 中解析失败的<cache-ref>
        parsePendingCacheRefs();
        // 处理 configurationElement 中解析失败的 SQL 语句
        parsePendingStatements();
      }
    
    

    大致流程

    1. 解析调用 configurationElement() 函数来解析各个节点
    2. 标记传入的文件已经解析了
    3. 绑定文件到相应的 namespace, 所以 namespace 需要是唯一的
    4. 处理解析失败的节点

    2.2 解析各个节点

      private void configurationElement(XNode context) {
        try {
          // 获取namespace属性, 其代表者这个文档的标识
          String namespace = context.getStringAttribute("namespace");
          if (namespace == null || namespace.equals("")) {
            throw new BuilderException("Mapper's namespace cannot be empty");
          }
          builderAssistant.setCurrentNamespace(namespace);
          // 解析 <cache-ref> 节点
          cacheRefElement(context.evalNode("cache-ref"));
          // 解析 <cache> 节点
          cacheElement(context.evalNode("cache"));
          // 解析 </mapper/parameterMap> 节点
          parameterMapElement(context.evalNodes("/mapper/parameterMap"));
          // 解析 </mapper/resultMap> 节点
          resultMapElements(context.evalNodes("/mapper/resultMap"));
          // 解析 </mapper/sql> 节点
          sqlElement(context.evalNodes("/mapper/sql"));
          // 解析 select|insert|update|delet 节点
          buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
        } catch (Exception e) {
          throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
        }
      }
    
    

    为了避免篇幅太长, 在此就不深入讲解各个解析过程, 后续会开专门的章节。

    一起学 mybatis

    你想不想来学习 mybatis? 学习其使用和源码呢?那么, 在博客园关注我吧!!

    我自己打算把这个源码系列更新完毕, 同时会更新相应的注释。快去 star 吧!!

    mybatis最新源码和注释

    github项目

  • 相关阅读:
    ubuntu下安装chromium浏览器adobe flash插件
    Eclipse运行时提示failed to create the java virtual machine 解决方法
    64位Ubuntu下不能生成R.java文件的问题(Android)
    Ubuntu下Eclipse的中文支持(GBK)
    Android工程编译错误:The project cannot be built until build path errors are resolved
    Apache POI
    ubuntu下安装Google拼音输入法
    [导入]ThinkPHP新手推荐学习路线
    [导入]示例Blog发表日志的程序流程(总结)
    [导入][视频] ThinkPHP入门第一步
  • 原文地址:https://www.cnblogs.com/homejim/p/9741404.html
Copyright © 2011-2022 走看看