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 表示全局主键都采用该策略(如果全局策略和局部策略都有设置,局部策略优先级高)
  • 相关阅读:
    Something I know about WebDynpro
    Details about support package implementation
    CRM Middleware Performance Topics
    Way to configure the logon navigaion layouts via Business Roles in CRM
    DOM 常用节点类型和方法
    第一届 xdef 会议日程
    去除百度音乐盒广告的chrome插件 持续更新
    从人人网抓取高校数据信息,包括,省份 高校 院系 (提供最终SQL文件下载)
    PHP 与 JSON
    解决HTTPS 发送请求走socket问题
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/11920807.html
Copyright © 2011-2022 走看看