Result Maps collection already contains value: 使用MyBatis Generator报错问题解决

957 阅读1分钟

使用MyBatis Generator Main方法生成代码后, 运行出错

1. 报错语句

nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML.
The XML location is 'file [D:\developer\mall-tiny-02\target\classes\com\mall\mapper\PmsBrandMapper.xml]'.
Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mall.mbg.mapper.PmsBrandMapper.BaseResultMap

原因分析:

错误日志表明了PmsBrandMapper.xml文件解析错误,BaseResultMap重复定义。


2. 问题解决

经过排查, 使用MyBatis Generator时, 将生成的代码重复合并追加到了代码中, MyBatis Generator版本为1.3.3

MyBatis Generator 在1.3.7版本提供了解决方案:

在generatorConfig.xml文件中添加插件, 使得生成代码, 覆盖原来代码

<!--生成mapper.xml时覆盖原文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />