异常:java.lang.IllegalArgumentException: Result Maps collection already contains value

10 篇文章 1 订阅

今天在测试mybatis接口的时候出现一个异常,花费了不少时间解决了,特此将问题记录下来,希望能帮助更多的同学。

异常信息

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [H:\User\Administrator\IntelliJProjects\SpringBootProject\codegen-java\codegen-dao\target\classes\mappers\TGenResultTemplatePropMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.codegen.dao.TGenResultTemplatePropMapper.BaseResultMap
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
	... 78 more
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [H:\User\Administrator\IntelliJProjects\SpringBootProject\codegen-java\codegen-dao\target\classes\mappers\TGenResultTemplatePropMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.codegen.dao.TGenResultTemplatePropMapper.BaseResultMap
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:523)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380)
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:547)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:153)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$67009919.CGLIB$sqlSessionFactory$0(<generated>)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$67009919$$FastClassBySpringCGLIB$$6a893377.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$67009919.sqlSessionFactory(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 79 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.codegen.dao.TGenResultTemplatePropMapper.BaseResultMap
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:521)
	... 92 more
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.codegen.dao.TGenResultTemplatePropMapper.BaseResultMap
	at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:859)
	at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:831)
	at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:613)
	at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:214)
	at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:47)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:285)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
	... 94 more

原因

找到核心异常

Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.codegen.dao.TGenResultTemplatePropMapper.BaseResultMap

BaseResultMap出了问题,这个mapper.xml都是自动生成的怎么会有问题,原因是因为 BaseResultMap这个结果出现了多次。也就是有两个 id 为 BaseResultMap

解决办法

最后找到报错的 mapper.xml 文件 搜索BaseResultMap 这个关键字,果然出现了两次 BaseResultMap
这个问题的产生原因是:使用 mybatis 逆向工程多次生成 mapper 时,未将之前生成的 mapper.xml 删除,导致其在原有的xml里面追加内容。最终导致 BaseResultMap 重复。删除已生成的 mapper.xml 文件, 重新生成即可。

总结

mapper.xml里面不能有同样的id名,否则就会报上面类似的异常

================================================================================
感谢阅读,写得不好的地方请指教,能帮助到你是对我最好的回报,不卑不亢,加油。
请你记住比你优秀的一定比你努力,比你努力的人一定比你优秀。
================================================================================
  • 43
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值