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

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

问题重现

示例代码

使用的是mall-tiny-02的代码,代码地址:https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-02

直接运行MallTinyApplication的main函数

发现正常运行,启动成功!640?wx_fmt=png

运行代码生成器

运行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文件信息是直接追加在原来的文件上的,并不是直接覆盖,导致了这个错误。640?wx_fmt=png

问题解决

以前一直以为是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" />

重新运行代码生成器

发现PmsBrandMapper.xml生成已经正常,应用也可以正常运行了。640?wx_fmt=png

640?wx_fmt=png

项目源码地址

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-02

推荐阅读



640?wx_fmt=jpeg

欢迎关注,点个在看

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Generator一个代码生成器,它可以根据数据库表结构自动生成 MyBatisMapper 文件、Java 实体类和 XML 映射文件等代码。使用 MyBatis Generator 可以大大减少开发人员的工作量,提高开发效率。 下面是使用 MyBatis Generator 的步骤: 1. 配置 generatorConfig.xml 文件,该文件用于配置生成代码的详细信息,包括数据库连接信息、生成的文件类型、生成的表等。 2. 运行 MyBatis Generator生成代码。 3. 在项目引入生成的代码,例如 Mapper 接口、实体类等。 以下是一个简单的 generatorConfig.xml 文件的示例: ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <classPathEntry location="mysql-connector-java-5.1.39-bin.jar" /> <context id="MysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password=""> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.mybatis.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.mybatis.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user" domainObjectName="User" > </table> </context> </generatorConfiguration> ``` 这个示例配置文件会生成一个 UserMapper 接口、一个 User 实体类和一个 user.xml 文件。其,User 实体类的包路径为 com.mybatis.example.model,UserMapper 接口和 user.xml 文件的包路径为 com.mybatis.example.mapper。 在运行 MyBatis Generator 之后,可以在项目引入生成的代码,例如: ``` @Autowired private UserMapper userMapper; ``` 以上代码会注入一个 UserMapper 对象,可以通过该对象来操作数据库表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值