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.

macrozheng
macrozheng
macrozheng
Fix MyBatis Generator XML Append Issue: Overwrite Mapper Files Correctly
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.BaseResultMap

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.

<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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaMyBatisXMLmappergenerator
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.