欢迎您访问新疆栾骏商贸有限公司,公司主营电子五金轴承产品批发业务!
全国咨询热线: 400-8878-609

新闻资讯

常见问题

mybatis-generator生成文件覆盖问题的解决

作者:用户投稿2026-01-11 06:45:37
目录
  • mybatis-generator生成文件覆盖
    • 解决方案
  • mybatis-generator避免覆盖自定义的sql方法

    mybatis-generator生成文件覆盖

    在Idea中使用Mybatis-generator plugin时遇到的问题,我的mybatis配置到的DB的服务中,每次部署微服务时需要install db这个微服务,将其打成jar包,供其他服务引用。

    可是发现,我每次install或者package时候,mybatis-generator都会随编译自动运行,导致工程中的的mapper和dao都被冲掉。

    解决方案

    <configuration>
        <!--配置文件的位置-->
        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <!-- 该配置可避免maven install或者package时候运行该插件,导致本地mapper重新生成 -->
            <phase>deploy</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

    官方文档中有如下描述:

    The MBG plugin is bound to the generate-sources phase of a Maven build, so it will execute before the compile step. Also note that MBG generates both Java source files and XML resources. The MBG goal will bind both generated Java files and XML resources to the build and they will both be included in any JAR generated by the build.

    mybatis-generator避免覆盖自定义的sql方法

    编写PersonExtMapper.java 接口文件 编写自定义方法

    编写PersonExtMapper.xml 映射文件 配置映射

    PersonExtMapper.xml 和PersontMapper.xml(Mybatis生成器生成)的区别

    指向各自的Maper接口文件

    但相同

    当数据库字段发生改变 执行指令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate 不会覆盖自定义的方法

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持。