zoukankan      html  css  js  c++  java
  • MybatisPlus的各种支持的主键策略!

    注:本文代码样例及sql脚本均已上传至gitee:spring-boot-mybatis-plus学习

    Mybatis支持的主键策略

    MybatisPlus支持的主键策略定义在IdType中:

    描述
    AUTO 数据库ID自增
    NONE 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
    INPUT insert前自行set主键值
    ASSIGN_ID 分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
    ASSIGN_UUID 分配UUID,主键类型为String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default方法)
    ID_WORKER 分布式全局唯一ID 长整型类型(please use ASSIGN_ID)
    UUID 32位UUID字符串(please use ASSIGN_UUID)
    ID_WORKER_STR 分布式全局唯一ID 字符串类型(please use ASSIGN_ID)
    /**
     * 生成ID类型枚举类
     *
     * @author hubin
     * @since 2015-11-10
     */
    @Getter
    public enum IdType {
        /**
         * 数据库ID自增
         */
        AUTO(0),
        /**
         * 该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
         */
        NONE(1),
        /**
         * 用户输入ID
         * <p>该类型可以通过自己注册自动填充插件进行填充</p>
         */
        INPUT(2),
    
        /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
        /**
         * 分配ID (主键类型为number或string),
         * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)
         *
         * @since 3.3.0
         */
        ASSIGN_ID(3),
        /**
         * 分配UUID (主键类型为 string)
         * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))
         */
        ASSIGN_UUID(4),
        /**
         * @deprecated 3.3.0 please use {@link #ASSIGN_ID}
         */
        @Deprecated
        ID_WORKER(3),
        /**
         * @deprecated 3.3.0 please use {@link #ASSIGN_ID}
         */
        @Deprecated
        ID_WORKER_STR(3),
        /**
         * @deprecated 3.3.0 please use {@link #ASSIGN_UUID}
         */
        @Deprecated
        UUID(4);
    
        private final int key;
    
        IdType(int key) {
            this.key = key;
        }
    }
    

    可以使用@TableId确定主键,使用type属性规定主键策略:

        @TableId(type = IdType.AUTO) //指定主键
        private Long id;
    

    测试一下:

        @Test
        void insert() {
            User user = new User();
            user.setName("auto");
            user.setEmail("12222@qq.com");
            user.setRemark("备注信息");
            user.setAge(20);
            user.setCreateTime(new Date());
            int rows = mapper.insert(user);
            System.out.println("影响记录数: " + rows);
            System.out.println("主键id :" + user.getId());
        }
    

    配置主键策略的方式

    文档:https://mybatis.plus/config/#dbconfig

    1. 全局配置
    mybatis-plus:
      global-config:
        db-config:
          id-type: AUTO
    
    1. 局部配置
        @TableId(type = IdType.AUTO) //指定主键
        private Long id;
    
  • 相关阅读:
    sql中生成随机字符串的function
    postgresql中uuid的使用
    sql中循环的存储过程
    java发送http的get、post请求
    data:image/png;base64
    Matcher和Pattern总结
    OPENXML解析sp_xml_preparedocument获取的XML句柄
    SqlServer性能优化
    python的2D绘图库matplotlib
    sift&surf检测关键点及描述子
  • 原文地址:https://www.cnblogs.com/summerday152/p/13870612.html
Copyright © 2011-2022 走看看