• MyBatis实现MySQL表字段及结构的自动增删


    前言

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

    介绍

    mybatis-enhance-actable

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

    使用

    pom导包

        <dependency>
            <groupId>com.gitee.sunchenbin.mybatis.actablegroupId>
            <artifactId>mybatis-enhance-actableartifactId>
            <version>1.5.0.RELEASEversion>
        dependency>
    

    配置application.yml

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

    SpringBootApplication启动类配置

    @SpringBootApplication
    @EnableScheduling
    @EnableCaching
    @MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*", "com.*.*.mapper"})
    @ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*", "com.*.*.*"})
    public class ReceiveCardTestSystemApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ReceiveCardTestSystemApplication.class, args);
        }
    
    }
    

    关键是这两行

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

    要增加上actable的扫包路径。

    Domain类的配置

    @Getter
    @Setter
    @Accessors(chain = true)
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @TableName("users")
    @Table(name = "users", isSimple = true)
    public class UsersDO implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @TableId(value = "id", type = IdType.AUTO)
        @IsKey
        @IsAutoIncrement
        @Column(comment = "id")
        private Integer id;
    
        @Column(name = "user_name", length = 50, comment = "用户名", isNull = true)
        @Index
        private String userName;
    
        @Column(name = "password", length = 255, comment = "密码", isNull = true)
        private String password;
    
        @Column(name = "employee_id", length = 50, comment = "工号", isNull = true)
        @Unique
        private String employeeId;
    
        @Column(name = "role", comment = "0->管理员,1->测试员", isNull = true)
        private Integer role;
    
        @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, isNull = true, comment = "创建时间")
        @TableField(fill = FieldFill.INSERT)
        private Date createTime;
    
        @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, isNull = true, comment = "更新时间")
        @TableField(fill = FieldFill.INSERT_UPDATE)
        private Date updateTime;
    
    }
    

    注意:

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

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

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

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

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

本文作者:scottyzh

本文链接:https://www.cnblogs.com/johnyzh/p/18228817

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

  • 相关阅读:
    FinOps能力成熟度模型启动,灵雀云助力云原生降本增效标准制定
    今年三下乡社会实践活动向媒体投稿我有了底气
    Spring Cloud Alibaba系列之nacos:(4)配置管理
    前端项目如何准确预估个人工时
    浅谈 K-D Tree 及其进阶应用
    153-Vue中的vuex内的辅助函数——mapState,mapGetters,mapMutations,mapActions的用法
    ElasticSearch(3)
    【语音编码】基于matlab ADPCM编解码【G.723.1】(Matlab代码实现)
    敏捷开发使用
    ​刘强东卸任京东集团 CEO,徐雷接任;苹果新专利可为多个设备无线充电;Rust公布2024年路线图|极客头条
  • 原文地址:https://www.cnblogs.com/Johnyzh/p/18228817