企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
今天在测试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名,否则就会报上面类似的异常 。