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

junbaor

问题

使用 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"/>
阅读 12.4k

广泛涉略,快速学习

836 声望
34 粉丝
0 条评论
你知道吗?

广泛涉略,快速学习

836 声望
34 粉丝
宣传栏