经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
mybatis-generator生成文件覆盖问题的解决
来源:jb51  时间:2022/2/9 11:18:12  对本文有异议

mybatis-generator生成文件覆盖

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

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

解决方案

  1. <configuration>
  2. ? ? <!--配置文件的位置-->
  3. ? ? <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
  4. ? ? <verbose>true</verbose>
  5. ? ? <overwrite>true</overwrite>
  6. </configuration>
  7. <executions>
  8. ? ? <execution>
  9. ? ? ? ? <id>Generate MyBatis Artifacts</id>
  10. ? ? ? ? <!-- 该配置可避免maven install或者package时候运行该插件,导致本地mapper重新生成 -->
  11. ? ? ? ? <phase>deploy</phase>
  12. ? ? ? ? <goals>
  13. ? ? ? ? ? ? <goal>generate</goal>
  14. ? ? ? ? </goals>
  15. ? ? </execution>
  16. </executions>
  17. <dependencies>
  18. ? ? <dependency>
  19. ? ? ? ? <groupId>org.mybatis.generator</groupId>
  20. ? ? ? ? <artifactId>mybatis-generator-core</artifactId>
  21. ? ? ? ? <version>1.3.2</version>
  22. ? ? </dependency>
  23. </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 不会覆盖自定义的方法

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号