经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
MyBatis实现MySQL表字段及结构的自动增删
来源:cnblogs  作者:scottyzh  时间:2024/6/5 9:26:54  对本文有异议

前言

在开发过程中,总会涉及到数据库表结构字段的增加或者删除,或者是索引的增加和减少,这个时候能把修改表结构字段这些工作都交给程序来进行,那能大大方便开发。正好有一个现成的工具可以在springboot里面实现这个流程。

介绍

mybatis-enhance-actable

上述是gitee链接。这个工具是mybatis-enhance-actable,引用作者的介绍:A.CTable是一个基于Spring和Mybatis的Maven项目,mybatis-enhance-actable支持springboot,增强了Mybatis的功能,通过配置model注解的方式来创建表,修改表结构,提供通用的单表CUDR工具,实现了mybatis自动建表的能力,目前支持Mysql。

使用

pom导包

  1. <dependency>
  2. <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
  3. <artifactId>mybatis-enhance-actable</artifactId>
  4. <version>1.5.0.RELEASE</version>
  5. </dependency>

配置application.yml

  1. #自动建表设置
  2. mybatis:
  3. table:
  4. #create系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据;
  5. #update系统会自动判断哪些表是新建的.哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据;
  6. #add新增表/新增字段/新增索引新增唯一约束的功能,不做做修改和删除(只在版本1.0.9.RELEASE及以上支持);
  7. #none系统不做任何处理;
  8. auto: update
  9. model:
  10. #扫描用于创建表的对象的包名 填入domain包路径
  11. pack: com.xx.xx.domain
  12. database:
  13. #数据库类型目前只支持mysql
  14. type: mysql
  15. mybatis-plus: #数据库格式配置
  16. global-config:
  17. banner: false # 数据库启动的banner
  18. db-config:
  19. id-type: auto #id生成规则:mysql数据库id自增
  20. configuration:
  21. map-underscore-to-camel-case: true #开启驼峰,处理数据库“_"的字段
  22. auto-mapping-behavior: full #自动映射任何复杂的结果
  23. # 注意下面,一定要添加前面actable的xml
  24. mapper-locations: com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml,classpath*:mapper/*.xml

SpringBootApplication启动类配置

  1. @SpringBootApplication
  2. @EnableScheduling
  3. @EnableCaching
  4. @MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*", "com.*.*.mapper"})
  5. @ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*", "com.*.*.*"})
  6. public class ReceiveCardTestSystemApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ReceiveCardTestSystemApplication.class, args);
  9. }
  10. }

关键是这两行

  1. @MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*", "com.*.*.mapper"})
  2. @ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*", "com.*.*.*"}) //扫描所有的包 最后一个是*,中间两个*换成实际路径

要增加上actable的扫包路径。

Domain类的配置

  1. @Getter
  2. @Setter
  3. @Accessors(chain = true)
  4. @Builder
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. @TableName("users")
  8. @Table(name = "users", isSimple = true)
  9. public class UsersDO implements Serializable {
  10. private static final long serialVersionUID = 1L;
  11. @TableId(value = "id", type = IdType.AUTO)
  12. @IsKey
  13. @IsAutoIncrement
  14. @Column(comment = "id")
  15. private Integer id;
  16. @Column(name = "user_name", length = 50, comment = "用户名", isNull = true)
  17. @Index
  18. private String userName;
  19. @Column(name = "password", length = 255, comment = "密码", isNull = true)
  20. private String password;
  21. @Column(name = "employee_id", length = 50, comment = "工号", isNull = true)
  22. @Unique
  23. private String employeeId;
  24. @Column(name = "role", comment = "0->管理员,1->测试员", isNull = true)
  25. private Integer role;
  26. @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, isNull = true, comment = "创建时间")
  27. @TableField(fill = FieldFill.INSERT)
  28. private Date createTime;
  29. @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, isNull = true, comment = "更新时间")
  30. @TableField(fill = FieldFill.INSERT_UPDATE)
  31. private Date updateTime;
  32. }

注意:

@Table(name = "users", isSimple = true)

当开启isSimple后 @Column里面不写name则会默认将驼峰命名改为下划线,比如updateTime变成了update_time

普通索引使用@Index,唯一索引使用@Unique

组合索引则这样使用:@Index(columns = {"country", "province", "city"})

通过上述配置,启动系统的时候就会自动对MySQL表进行更新了。

原文链接:https://www.cnblogs.com/Johnyzh/p/18228817

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

本站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号