mybatis自定义之优先从classes目录加载,加载之后遇到相同的类定义时不再加载

  如果mybatis中包含了两个相同定义的mapper,启动时出现下列异常:

Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.xxx.BaseResultMap
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:872) ~[mybatis-3.4.6.jar!/:3.4.6]
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:844) ~[mybatis-3.4.6.jar!/:3.4.6]
at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:626) ~[mybatis-3.4.6.jar!/:3.4.6]
at org.apache.ibatis.session.RefreshConfiguration.addResultMap(RefreshConfiguration.java:57) ~[-starter-service-20191118.062118-160.jar:3.4.6]
at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:214) ~[mybatis-3.4.6.jar!/:3.4.6]
at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:47) ~[mybatis-3.4.6.jar!/:3.4.6]
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:285) ~[mybatis-3.4.6.jar!/:?]

  原因是mybatis定义的StrictMap加了防覆盖判断,通常的解决方法是检查一遍代码去掉重复的即可。但是LZ有时候需要的场景是为了验证快,临时将mapper修改后不再打包,放在外围的classpath目录优先加载,而是重新打包、发包,有时候这甚至不可能。分析源码可知,我们只要在org.apache.ibatis.session.Configuration#addResultMap或org.apache.ibatis.session.Configuration.StrictMap#put加个判断即可。如下:

    public V put(String key, V value) {
      if (containsKey(key)) {  // 可以考虑此处加判断,如果已经存在就什么都不做
        throw new IllegalArgumentException(name + " already contains value for " + key);
      }
      if (key.contains(".")) {
        final String shortKey = getShortName(key);
        if (super.get(shortKey) == null) {
          super.put(shortKey, value);
        } else {
          super.put(shortKey, (V) new Ambiguity(shortKey));
        }
      }
      return super.put(key, value);
    }

 

上一篇:Android studio 使用自定义修改的framework.jar


下一篇:Java - Test - TestNG: testng.xml 元素 class