【Mybatis源码阅读之mappers配置】

  </configuration>

    <mappers>

        <!-- 注册userMapper.xml文件, 

        userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->

        <mapper resource="sql.xml"/>

         <mapper class="cn.com.ibatisdemo.ibatisdemo.UserMapper"/>

         <!-- 注册UserMapper映射接口

       <mapper class="me.gacl.mapping.UserMapperI"/>-->

    </mappers>

</configuration>

XMLConfigBuilder类主要方法说明

方法一:

  public Configuration parse() {

    if (parsed) {

      throw new BuilderException("Each MapperConfigParser can only be used once.");

    }

    parsed = true;

    parseConfiguration(parser.evalNode("/configuration"));

    return configuration;

  }

方法二:

private void parseConfiguration(XNode root) {

    try {

      typeAliasesElement(root.evalNode("typeAliases"));

      pluginElement(root.evalNode("plugins"));

      objectFactoryElement(root.evalNode("objectFactory"));

      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));

      propertiesElement(root.evalNode("properties"));

      settingsElement(root.evalNode("settings"));

      environmentsElement(root.evalNode("environments"));

      typeHandlerElement(root.evalNode("typeHandlers"));

      mapperElement(root.evalNode("mappers"));

    } catch (Exception e) {

      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);

    }

  }

方法三:

 private void mapperElement(XNode parent) throws Exception {

    if (parent != null) {

      for (XNode child : parent.getChildren()) {

        String resource = child.getStringAttribute("resource");

        String url = child.getStringAttribute("url");

        InputStream inputStream;

        if (resource != null && url == null) {

          ErrorContext.instance().resource(resource);

          inputStream = Resources.getResourceAsStream(resource);

          XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());

          mapperParser.parse();

        } else if (url != null && resource == null) {

          ErrorContext.instance().resource(url);

          inputStream = Resources.getUrlAsStream(url);

XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());

          mapperParser.parse();

        } else {

          throw new BuilderException("A mapper element may only specify a url or resource, but not both.");

        }

      }

    }

  }

说明:此版本mybatis-3.0.4有点古老,新版本是resource/url/classes三者必有其一或者使用package动态扫描,下面这个方法是mybatis-3.4.2的方法

private void mapperElement(XNode parent) throws Exception {

    if (parent != null) {

      for (XNode child : parent.getChildren()) {

        if ("package".equals(child.getName())) {

          String mapperPackage = child.getStringAttribute("name");

          configuration.addMappers(mapperPackage);

        } else {

          String resource = child.getStringAttribute("resource");

          String url = child.getStringAttribute("url");

          String mapperClass = child.getStringAttribute("class");

          if (resource != null && url == null && mapperClass == null) {

            ErrorContext.instance().resource(resource);

            InputStream inputStream = Resources.getResourceAsStream(resource);

            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());

            mapperParser.parse();

          } else if (resource == null && url != null && mapperClass == null) {

            ErrorContext.instance().resource(url);

            InputStream inputStream = Resources.getUrlAsStream(url);

            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());

            mapperParser.parse();

          } 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.");

          }

        }

      }

    }

  }

总结如下:

<mappers>  

   <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 -->  

   <package name="com.tiantian.mybatis.mapperinterface"/>  

   <!-- 通过mapper元素的resource属性可以指定一个相对于类路径的Mapper.xml文件 -->  

   <mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/>  

   <!-- 通过mapper元素的url属性可以指定一个通过URL请求道的Mapper.xml文件 -->  

   <mapper url="file:///E:/UserMapper.xml"/>  

   <!-- 通过mapper元素的class属性可以指定一个Mapper接口进行注册 -->  

   <mapper class="com.tiantian.mybatis.mapperinterface.UserMapper"/>  

</mappers>  

 当使用mapper元素进行Mapper定义的时候需要注意:mapper的三个属性resource、url和class对于每个mapper元素只能指定一个,要么指定resource属性,要么指定url属性,要么指定class属性,不能都指定,也不能都不指定。

原创不易,欢迎打赏,请认准正确地址,谨防假冒



 

 


猜你喜欢

转载自gaojingsong.iteye.com/blog/2365217