• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Mybatis-plus提前获取实体类用雪花算法生成的ID

武飞扬头像
H_bbo
帮助1

在Mybatis-plus中,通过设置@TableId可以让Mybatis-plus自动为我们生成雪花算法的ID号,该ID号是一个长整型数据,非常方便。但是雪花算法的ID号是在Insert执行的时候生成的,我们在Insert执行前是不知道Entity会获得一个什么ID号。

但是在某些情况下,我们想提前获取这个ID,这样可以通过一些计算来生成其他字段的值。

那么我们需要用雪花算法生成一个ID号。是不是还需要另外自己写一个雪花算法生成类呢?

完全不用。因为Mybatis-plus中内置了雪花算法生成功能,我们找出来调用就行了,就是下面这个类:

import com.baomidou.mybatisplus.core.toolkit.IdWorker;

我们可以这样调用:

Long ID=IdWorker.getId(entity);

如果有更高的需求,还可以设置雪花算法的其他参数。

这个类源码如下:

  1.  
    package com.baomidou.mybatisplus.core.toolkit;
  2.  
     
  3.  
    import com.baomidou.mybatisplus.core.config.GlobalConfig;
  4.  
    import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
  5.  
    import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
  6.  
     
  7.  
    import java.time.LocalDateTime;
  8.  
    import java.time.format.DateTimeFormatter;
  9.  
    import java.util.UUID;
  10.  
    import java.util.concurrent.ThreadLocalRandom;
  11.  
     
  12.  
    /**
  13.  
     * id 获取器
  14.  
     *
  15.  
     */
  16.  
    public class IdWorker {
  17.  
     
  18.  
        /**
  19.  
         * 主机和进程的机器码
  20.  
         */
  21.  
        private static IdentifierGenerator IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator();
  22.  
     
  23.  
        /**
  24.  
         * 毫秒格式化时间
  25.  
         */
  26.  
        public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
  27.  
     
  28.  
        /**
  29.  
         * 获取唯一ID
  30.  
         *
  31.  
         * @return id
  32.  
         */
  33.  
        public static long getId() {
  34.  
            return getId(new Object());
  35.  
        }
  36.  
     
  37.  
        /**
  38.  
         * 获取唯一ID
  39.  
         *
  40.  
         * @return id
  41.  
         */
  42.  
        public static long getId(Object entity) {
  43.  
            return IDENTIFIER_GENERATOR.nextId(entity).longValue();
  44.  
        }
  45.  
     
  46.  
        /**
  47.  
         * 获取唯一ID
  48.  
         *
  49.  
         * @return id
  50.  
         */
  51.  
        public static String getIdStr() {
  52.  
            return getIdStr(new Object());
  53.  
        }
  54.  
     
  55.  
        /**
  56.  
         * 获取唯一ID
  57.  
         *
  58.  
         * @return id
  59.  
         */
  60.  
        public static String getIdStr(Object entity) {
  61.  
            return IDENTIFIER_GENERATOR.nextId(entity).toString();
  62.  
        }
  63.  
     
  64.  
        /**
  65.  
         * 格式化的毫秒时间
  66.  
         */
  67.  
        public static String getMillisecond() {
  68.  
            return LocalDateTime.now().format(MILLISECOND);
  69.  
        }
  70.  
     
  71.  
        /**
  72.  
         * 时间 ID = Time ID
  73.  
         * <p>例如:可用于商品订单 ID</p>
  74.  
         */
  75.  
        public static String getTimeId() {
  76.  
            return getMillisecond() getIdStr();
  77.  
        }
  78.  
     
  79.  
        /**
  80.  
         * 有参构造器
  81.  
         *
  82.  
         * @param workerId     工作机器 ID
  83.  
         * @param dataCenterId 序列号
  84.  
         * @see #setIdentifierGenerator(IdentifierGenerator)
  85.  
         */
  86.  
        public static void initSequence(long workerId, long dataCenterId) {
  87.  
            IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator(workerId, dataCenterId);
  88.  
        }
  89.  
     
  90.  
        /**
  91.  
         * 自定义id 生成方式
  92.  
         *
  93.  
         * @param identifierGenerator id 生成器
  94.  
         * @see GlobalConfig#setIdentifierGenerator(IdentifierGenerator)
  95.  
         */
  96.  
        public static void setIdentifierGenerator(IdentifierGenerator identifierGenerator) {
  97.  
            IDENTIFIER_GENERATOR = identifierGenerator;
  98.  
        }
  99.  
     
  100.  
        /**
  101.  
         * 使用ThreadLocalRandom获取UUID获取更优的效果 去掉"-"
  102.  
         */
  103.  
        public static String get32UUID() {
  104.  
            ThreadLocalRandom random = ThreadLocalRandom.current();
  105.  
            return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY);
  106.  
        }
  107.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghaia
系列文章
更多 icon
同类精品
更多 icon
继续加载