MyBatis version

3.5.5

Descrption

HI,I found that parsing the xml file and generating the second-level cache through annotations are inconsistent。

Code location

XMLConfigBuilder.java

private void mapperElement(XNode parent) throws Exception {  
 .....  
 mapperParser.parse();
 .....  
 configuration.addMapper(mapperInterface);
}  

mapperParser.parse();

XMLMapperBuilder.java

private void configurationElement(XNode context) {
    try {
      String namespace = context.getStringAttribute("namespace");
      if (namespace.equals("")) {
          throw new BuilderException("Mapper's namespace cannot be empty");
      }
      builderAssistant.setCurrentNamespace(namespace);
      cacheRefElement(context.evalNode("cache-ref"));  // 1
      cacheElement(context.evalNode("cache"));         // 2
      parameterMapElement(context.evalNodes("/mapper/parameterMap"));
      resultMapElements(context.evalNodes("/mapper/resultMap"));
      sqlElement(context.evalNodes("/mapper/sql"));
      buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
    }
  }

configuration.addMapper(mapperInterface);

mapperAnnotationBuilder.java

public void parse() {
    String resource = type.toString(); 
    if (!configuration.isResourceLoaded(resource)) {  
      loadXmlResource();
      configuration.addLoadedResource(resource); 
      assistant.setCurrentNamespace(type.getName()); 
      parseCache(); // 1
      parseCacheRef(); // 2

      Method[] methods = type.getMethods(); 
      for (Method method : methods) {
        try {
          if (!method.isBridge()) { // issue #237
            parseStatement(method); 
          }
        } catch (IncompleteElementException e) {
          configuration.addIncompleteMethod(new MethodResolver(this, method));
        }
      }
    }
    parsePendingMethods(); 
  }

problem

I think whether the order of cache settings should be kept consistent。

my idea

Put cache-ref in front of cache for analysis

Comment From: harawata

Thank you for the report, @xbcrh ,

Is there any problem caused by this inconsistency? If there is, please provide a test case or small demo project so that we can verify the problem.

Comment From: xbcrh

Sorry, I think there is a problem with what I described。 I want to say that when @CacheNamespace and @CacheNamespaceRef exist at the same time, @CacheNamespaceRef will be used first. But by configuring \ and \ in the xml file,\ will be used in preference to \ The parsing xml code is as follows(XMLMapperBuilder.java): ``` cacheRefElement(context.evalNode("cache-ref")); // 1 cacheElement(context.evalNode("cache")); //2

The code to parse the annotation is as follows(MapperAnnotationBuilder.java):

parseCache(); // 1
parseCacheRef(); // 2
``` I don’t know why the annotation configuration and xml configuration are different。 If it is not a problem, please close this issue, thank you!

Comment From: harawata

I understand what you mean and you are not wrong, but it seems like an unrealistic scenario to me. I mean, why would anyone add both @CacheNamespace and @CacheNamespaceRef (or <cache /> and <cache-ref />) to the same mapper? What kind of result do they expect?

Comment From: xbcrh

Haha, yes, nobody uses it. I just found it when I looked at the source code. Whether these two methods are consistent will be better. Thank you for your answer anyway

Comment From: harawata

Yeah... if nobody uses it, we better not to add extra code for that. But thank you for taking the time to submit the report!