zoukankan      html  css  js  c++  java
  • MP支持的主键策略

    MP 支持多种主键策略 默认是推特的“” 雪花算法“” ,也可以设置其他策略下面我演示主键策略使用

    MP的主键定义在一个一个枚举类中 源码如下

    public enum IdType {
        AUTO(0),//数据库自增 依赖数据库
        NONE(1),// 表示该类型未甚至主键类型 (如果没有主键策略)默认根据雪花算法生成
        INPUT(2),//用户输入ID(该类型可以通过自己注册填充插件进行填充)
      //下面这三种类型,只有当插入对象id为空时 才会自动填充。
        ID_WORKER(3),//全局唯一(idWorker)数值类型
        UUID(4),//全局唯一(UUID)
        ID_WORKER_STR(5);//全局唯一(idWorker的字符串表示)
    
        private final int key;
    
        private IdType(int key) {
            this.key = key;
        }
    
        public int getKey() {
            return this.key;
        }
    }

    .局部主键策略:

    1,局部主键策略实现
    
    在实体类中 ID属性加注解
    
    @TableId(type = IdType.AUTO) 主键自增 数据库中需要设置主键自增
    private Long id;
    @TableId(type = IdType.NONE) 默认 跟随全局策略走
    private Long id;
    @TableId(type = IdType.UUID) UUID类型主键
    private Long id;
    @TableId(type = IdType.ID_WORKER) 数值类型  数据库中也必须是数值类型 否则会报错
    private Long id;
    @TableId(type = IdType.ID_WORKER_STR) 字符串类型   数据库也要保证一样字符类型
    private Long id;
    @TableId(type = IdType.INPUT) 用户自定义了  数据类型和数据库保持一致就行
    private Long id;

    2,全局主键策略实现

     需要在application.yml文件中

    添加

    mybatis-plus:
    mapper-locations:
    - com/mp/mapper/*
    global-config:
    db-config:
    id-type: uuid/none/input/id_worker/id_worker_str/auto 表示全局主键都采用该策略(如果全局策略和局部策略都有设置,局部策略优先级高)
  • 相关阅读:
    设计模式-工厂设计模式
    Spring Batch BATCH_JOB_SEQ 出现死锁问题
    windows 安装 jenkins 自动化构建部署至linux服务器上
    Git安装
    MAVEN(一) 安装和环境变量配置
    Jenkins 安装
    jenkins操作
    linux firewalld 防火墙操作命令
    【Azure Redis 缓存】Azure Redis读写比较慢/卡的问题排查
    【Azure 服务总线】向服务总线发送消息时,返回错误代码Error code : 50009
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/11920807.html
Copyright © 2011-2022 走看看