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

探讨使用mybatis-generator-maven-plugin生成代码时,如何实现XML文件的完全覆盖而非累加的问题。通过调整配置和引入特定插件最终解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

使用 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"/>
MyBatis Plus Generator Maven PluginMyBatis-Plus 提供的一个代码生成工具,它可以帮助开发者快速生成基于数据库表的实体类、Mapper 接口、Service 层以及 Controller 层等代码文件。该插件是基于 MyBatis Generator 扩展而来,具备更高的自动化程度和灵活性。 ### 配置方式 在 `pom.xml` 中添加 `mybatis-plus-generator-maven-plugin` 插件的基本配置如下: ```xml <build> <plugins> <plugin> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator-maven-plugin</artifactId> <version>3.5.1</version> <!-- 请根据需要选择最新版本 --> <configuration> <outputDir>${project.basedir}/src/main/java</outputDir> <overwrite>true</overwrite> <open>false</open> <enableKotlin>false</enableKotlin> <packageInfo> <parent>com.example.demo</parent> <moduleName>user</moduleName> <entity>model</entity> <mapper>mapper</mapper> <service>service</service> <controller>controller</controller> </packageInfo> <strategyConfig> <tablePrefix>t_</tablePrefix> <idType>auto</idType> <fieldPrefix>t_</fieldPrefix> </strategyConfig> </configuration> </plugin> </plugins> </build> ``` ### 核心配置说明 - **outputDir**:指定生成代码的输出路径。 - **overwrite**:是否覆盖已有的生成文件。 - **open**:是否打开输出目录(通常用于调试)。 - **enableKotlin**:是否启用 Kotlin 支持。 - **packageInfo**:定义包结构信息,包括父包名、模块名以及各层代码的子包名。 - **strategyConfig**:定义生成策略,如表前缀、字段前缀、主键类型等。 ### 使用方式 1. 在命令行中执行以下命令以运行插件: ```bash mvn mybatis-plus-generator:generate ``` 2. 插件会根据 `pom.xml` 中的配置读取数据库元数据,并按照设定的包结构生成对应的 Java 文件。 3. 生成的代码通常包含以下内容: - **Entity 类**:与数据库表一一映射的 POJO 对象。 - **Mapper 接口**:继承自 `BaseMapper`,提供基础的 CRUD 操作。 - **Service 接口及实现类**:封装业务逻辑。 - **Controller 类**:处理 HTTP 请求,对外暴露 RESTful API。 ### 数据库连接配置 为了确保插件能够正常访问数据库并生成代码,需在项目的 `application.yml` 或 `application.properties` 文件中正确配置数据库连接信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` ### 自定义模板支持 MyBatis Plus Generator 允许通过 Velocity 模板引擎来自定义生成的代码格式。可以通过设置 `<templateEngine>` 标签来启用自定义模板: ```xml <configuration> ... <templateEngine> <type>velocity</type> <customTemplate> entity: /path/to/entity_template.vm mapper: /path/to/mapper_template.vm </customTemplate> </templateEngine> </configuration> ``` ### 注意事项 - 确保数据库驱动依赖已经正确引入项目中。 - 若使用 MySQL 8.x 版本,请注意时区参数的配置- 表名和字段名应遵循一定的命名规范,以便插件能正确识别并生成代码。 - 可以通过 `<ignore>` 标签排除某些不需要生成的表或字段 [^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值