Backend Development 4 min read

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.

<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>&lt;dependency&gt;
  &lt;groupId&gt;org.mybatis.generator&lt;/groupId&gt;
  &lt;artifactId&gt;mybatis-generator-core&lt;/artifactId&gt;
  &lt;version&gt;1.3.7&lt;/version&gt;
&lt;/dependency&gt;
</code>

Add plugin to overwrite mapper.xml

<code>&lt;!-- Generate mapper.xml with overwrite --&gt;
&lt;plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/&gt;
</code>

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

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

login 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.