MyBatis Generator使用过程中踩过的一个坑

栏目: Java · 发布时间: 6年前

内容简介:在使用MyBatis Generator生成代码的过程中,曾经遇到一个坑,每次生成mapper.xml的时候并不是直接覆盖原文件,而是在原文件中追加了新的内容,导致运行项目出错,本文主要讲解如何解决这个问题。使用的是mall-tiny-02的代码,代码地址:发现正常运行,启动成功!

在使用MyBatis Generator生成代码的过程中,曾经遇到一个坑,每次生成mapper.xml的时候并不是直接覆盖原文件,而是在原文件中追加了新的内容,导致运行项目出错,本文主要讲解如何解决这个问题。

问题重现

示例代码

使用的是mall-tiny-02的代码,代码地址: github.com/macrozheng/…

直接运行MallTinyApplication的main函数

发现正常运行,启动成功!

MyBatis Generator使用过程中踩过的一个坑

运行代码生成器

运行com.macro.mall.tiny.mbg.Generator的main方法

重新启动MallTinyApplication的main函数

发现已经无法正常运行,其中有这么一行关键性的错误:

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: Result Maps collection already contains value for com.macro.mall.tiny.mbg.mapper.PmsBrandMapper.BaseResultMap
复制代码

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

查看PmsBrandMapper.xml文件

从中可以发现MyBatis Generator生成的mapper.xml文件信息是直接追加在原来的文件上的,并不是直接覆盖,导致了这个错误。

MyBatis Generator使用过程中踩过的一个坑

问题解决

以前一直以为是MyBatis Generator生成的问题,直接删除mapper.xml所在文件夹,重新生成就好了,现在提供一种MyBatis Generator官方提供的解决方法。

升级MyBatis Generator的版本

MyBatis Generator 在1.3.7版本提供了解决方案,我们目前使用的版本为1.3.3。

<!-- MyBatis 生成器 -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
复制代码

在generatorConfig.xml文件中添加覆盖mapper.xml的插件

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

以上所述就是小编给大家介绍的《MyBatis Generator使用过程中踩过的一个坑》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ordering Disorder

Ordering Disorder

Khoi Vinh / New Riders Press / 2010-12-03 / USD 29.99

The grid has long been an invaluable tool for creating order out of chaos for designers of all kinds—from city planners to architects to typesetters and graphic artists. In recent years, web designers......一起来看看 《Ordering Disorder》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具