解决 mybatis-generator-maven-plugin 中 overwrite 配置无效的问题

问题

使用 mybatis-generator-maven-plugin 重复生成代码时, xml 文件不会覆盖, 而是每次累加.

目的

每次生成时都会生成全新的 xml 文件并覆盖旧的

配置

pom.xml

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>${mybatis-generator.version}</version>
    <configuration>
        <configurationFile>src/main/resources/mybatis/generator.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.softwareloop</groupId>
            <artifactId>mybatis-generator-lombok-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>
<?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>
    <properties resource="application.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        
        <plugin type="com.softwareloop.mybatis.generator.plugins.LombokPlugin">
            <!-- enable annotations -->
            <property name="builder" value="true"/>
            <property name="allArgsConstructor" value="false"/>
        </plugin>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="${spring.datasource.driverClassName}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <javaTypeResolver type="org.mybatis.generator.internal.types.MyJavaTypeResolverDefaultImpl"/>

        <javaModelGenerator targetPackage="com.junbaor.study.mybatis.model" targetProject="src/main/java">
            <property name="rootClass" value="com.junbaor.study.mybatis.model.BasePo"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mybatis/mapper" targetProject="src/main/resources"/>

        <javaClientGenerator targetPackage="com.junbaor.study.mybatis.mapper" targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <table tableName="md_%">
            <!--mysql 配置-->
            <generatedKey column="id" sqlStatement="mysql" identity="true"/>
            <!--oracle 配置-->
            <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
        <columnOverride column="" javaType=""/>
        </table>

    </context>
</generatorConfiguration>

build 插件中已经加入覆盖配置, 然后并没有什么卵用

<configuration>
    <configurationFile>src/main/resources/mybatis/generator.xml</configurationFile>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
</configuration>

搜索

在官方 issues 中以 overwrite 为关键字进行搜索, 发现两个有价值的信息

https://github.com/mybatis/ge...
https://github.com/mybatis/ge...

第一个链接有一句话

The overwrite property is only used for generated Java files. It should not affect XML files at all. XML files should always be merged.

Have you configured a comment generator with suppressAllComment=true? If so, that would be the cause of this behavior. The XML merge won't delete old elements if the comments are removed.

原来 overwrite 配置只是为了覆盖 java 类, xml 文件是不受这个控制的

第二链接是一个 pull request, 作者添加了一个插件, 新特性刚好就是解决这个问题了, 遗憾的是这个 pr 虽然已经合入 master 分支了, 但是并没有上传到中央仓库, 1.3.7 还只是一个 SNAPSHOT 版本.

解决

自己从 https://github.com/mybatis/ge... 拉下源码 mvn install 一下就可以用了.

安装到本地仓库后还需要修改自己项目中依赖的版本号, 然后在配置文件中添加一个插件, 问题解决.

<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值