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.Generator main method.
Restart application
Fails with an error indicating a parsing problem in PmsBrandMapper.xml and a duplicate BaseResultMap definition.
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.BaseResultMapIndicates 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.
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>Add plugin to overwrite mapper.xml
<!-- Generate mapper.xml with overwrite -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>Rerun the generator
Now PmsBrandMapper.xml is generated correctly and the application runs without errors.
Project source code
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-02
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
