解决方案其实很简单(就是因为mapper 里面出现了重载方法,mapper 接口里面不能出现重载方法的情况),可是百度了很久,根本就没有解决方案。
问题分析:
其实翻看mybatis源码之后,发现这个问题简单,之前得到结果就是看表现
mybatis的解析流程(源码使用了大量的构造器模式,看源码的时候会发现很多以Builder结尾的类,都是通过调用build方法进行构造的)
1,通过 SqlSessionFactoryBuilder.build方法对配置文件进行解析
2,会在build方法中封装对象 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); //这里的reader其实就是读取的配置文件
3,调用XMLConfigBuilder.parser()方法进行真正的解析工作
4,在parser方法中调用parseConfiguration()方法,会对mybatis配置文件和mapper元素进行解析
5,当解析完数据库配置,mybatis配置后,会调用mapperElement(root.evalNode("mappers"));这个方法会对mapper进行真正的解析,并且会将inser|update|delete|select元素的id解析出来
....... 中间繁琐的流程我就不写了,后面我会将mybatis的解析流程图提供出来
6,最后会调用MapperBuilderAssistant.addMappedStatement() ---》configuration.addMappedStatement(statement); 将解析出来的元素信息存放到内存中
7,上面的错误信息就是在这一步抛出来的,因为将解析出来的元素信息放入到内存的集合是mybatis自己封装的一个集合 StrictMap<V> extends HashMap<String, V> 这个集合继承了hashMap,实现原理差不多
8,StrictMap重写了HashMap的put方法,调用put方法的时候,增加了重复判断
@Override
@SuppressWarnings("unchecked")
public V put(String key, V value) {
if (containsKey(key)) {
throw new IllegalArgumentException(name + " already contains value for " + key
+ (conflictMessageProducer == null ? "" : conflictMessageProducer.apply(super.get(key), value)));
}
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);
}
throw new IllegalArgumentException(name + " already contains value for " + key
+ (conflictMessageProducer == null ? "" : conflictMessageProducer.apply(super.get(key), value)));
希望上面的解释对大家有一些帮助
异常信息:
org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:437) - Scanned package: 'com.***.entity' for aliases
org.mybatis.spring.SqlSessionFactoryBean.buildS