Fix MyBatis Generator XML Append Issue: Overwrite Mapper Files Correctly
This guide explains why MyBatis Generator appends to existing mapper.xml files causing duplicate BaseResultMap errors, and shows how to upgrade to version 1.3.7 and add the UnmergeableXmlMappersPlugin to ensure mapper files are overwritten and the application runs smoothly.
During MyBatis Generator code generation, mapper.xml files were appended instead of overwritten, causing duplicate BaseResultMap definitions and runtime errors.
Problem Reproduction
Example Code
Using the mall‑tiny‑02 project (GitHub link).
Run MallTinyApplication main
Application starts successfully.
Run the code generator
Execute
com.macro.mall.tiny.mbg.Generatormain method.
Restart application
Fails with an error indicating a parsing problem in
PmsBrandMapper.xmland a duplicate
BaseResultMapdefinition.
<code>nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML.
The XML location is 'file [D:\developer\github\mall-learning\mall-tiny-02\target\classes\com\macro\mall\tiny\mbg\mapper\PmsBrandMapper.xml]'.
Cause: java.lang.IllegalArgumentException: ResultMaps collection already contains value for com.macro.mall.tiny.mbg.mapper.PmsBrandMapper.BaseResultMap</code>Indicates that PmsBrandMapper.xml parsing error is due to duplicate BaseResultMap.
Inspect PmsBrandMapper.xml
The generated mapper.xml content is appended to the existing file, leading to duplication.
Solution
Previously, deleting the mapper.xml folder and regenerating worked, but MyBatis Generator provides an official fix.
Upgrade MyBatis Generator version
Version 1.3.7 includes the fix; the current project uses 1.3.3.
<code><dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</code>Add plugin to overwrite mapper.xml
<code><!-- Generate mapper.xml with overwrite -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
</code>Rerun the generator
Now
PmsBrandMapper.xmlis generated correctly and the application runs without errors.
Project source code
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-02
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.