Mybatis-plus如何提前獲取實(shí)體類(lèi)用雪花算法生成的ID
Mybatis-plus中,通過(guò)設(shè)置@TableId可以讓Mybatis-plus自動(dòng)為我們生成雪花算法的ID號(hào),該ID號(hào)是一個(gè)長(zhǎng)整型數(shù)據(jù),非常方便。但是雪花算法的ID號(hào)是在Insert執(zhí)行的時(shí)候生成的,我們?cè)贗nsert執(zhí)行前是不知道Entity會(huì)獲得一個(gè)什么ID號(hào)。
但是在某些情況下,我們想提前獲取這個(gè)ID,這樣可以通過(guò)一些計(jì)算來(lái)生成其他字段的值。例如我們用此ID號(hào)做秘鑰來(lái)加密密碼。
這種情況下,需要提前生成ID號(hào),手動(dòng)設(shè)置給Entity。在實(shí)體類(lèi)中,通過(guò)下面這個(gè)注解將自動(dòng)ID改為有程序控制輸入:
@TableId(type=IdType.INPUT)
那么我們需要用雪花算法生成一個(gè)ID號(hào)。是不是還需要另外自己寫(xiě)一個(gè)雪花算法生成類(lèi)呢?
完全不用。因?yàn)镸ybatis-plus中內(nèi)置了雪花算法生成功能,我們找出來(lái)調(diào)用就行了,就是下面這個(gè)類(lèi):
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
我們可以這樣調(diào)用:
Long ID=IdWorker.getId(entity);
如果有更高的需求,還可以設(shè)置雪花算法的其他參數(shù)。
這個(gè)類(lèi)源碼如下:
/* ?* Copyright (c) 2011-2020, baomidou (jobob@qq.com). ?* <p> ?* Licensed under the Apache License, Version 2.0 (the "License"); you may not ?* use this file except in compliance with the License. You may obtain a copy of ?* the License at ?* <p> ?* https://www.apache.org/licenses/LICENSE-2.0 ?* <p> ?* Unless required by applicable law or agreed to in writing, software ?* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ?* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the ?* License for the specific language governing permissions and limitations under ?* the License. ?*/ package com.baomidou.mybatisplus.core.toolkit; ? import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator; import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; ? import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; ? /** ?* id 獲取器 ?* ?* @author hubin ?* @since 2016-08-01 ?*/ public class IdWorker { ? ? ? /** ? ? ?* 主機(jī)和進(jìn)程的機(jī)器碼 ? ? ?*/ ? ? private static IdentifierGenerator IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator(); ? ? ? /** ? ? ?* 毫秒格式化時(shí)間 ? ? ?*/ ? ? public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); ? ? ? /** ? ? ?* 獲取唯一ID ? ? ?* ? ? ?* @return id ? ? ?*/ ? ? public static long getId() { ? ? ? ? return getId(new Object()); ? ? } ? ? ? /** ? ? ?* 獲取唯一ID ? ? ?* ? ? ?* @return id ? ? ?*/ ? ? public static long getId(Object entity) { ? ? ? ? return IDENTIFIER_GENERATOR.nextId(entity).longValue(); ? ? } ? ? ? /** ? ? ?* 獲取唯一ID ? ? ?* ? ? ?* @return id ? ? ?*/ ? ? public static String getIdStr() { ? ? ? ? return getIdStr(new Object()); ? ? } ? ? ? /** ? ? ?* 獲取唯一ID ? ? ?* ? ? ?* @return id ? ? ?*/ ? ? public static String getIdStr(Object entity) { ? ? ? ? return IDENTIFIER_GENERATOR.nextId(entity).toString(); ? ? } ? ? ? /** ? ? ?* 格式化的毫秒時(shí)間 ? ? ?*/ ? ? public static String getMillisecond() { ? ? ? ? return LocalDateTime.now().format(MILLISECOND); ? ? } ? ? ? /** ? ? ?* 時(shí)間 ID = Time + ID ? ? ?* <p>例如:可用于商品訂單 ID</p> ? ? ?*/ ? ? public static String getTimeId() { ? ? ? ? return getMillisecond() + getIdStr(); ? ? } ? ? ? /** ? ? ?* 有參構(gòu)造器 ? ? ?* ? ? ?* @param workerId ? ? 工作機(jī)器 ID ? ? ?* @param dataCenterId 序列號(hào) ? ? ?* @see #setIdentifierGenerator(IdentifierGenerator) ? ? ?*/ ? ? public static void initSequence(long workerId, long dataCenterId) { ? ? ? ? IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator(workerId, dataCenterId); ? ? } ? ? ? /** ? ? ?* 自定義id 生成方式 ? ? ?* ? ? ?* @param identifierGenerator id 生成器 ? ? ?* @see GlobalConfig#setIdentifierGenerator(IdentifierGenerator) ? ? ?*/ ? ? public static void setIdentifierGenerator(IdentifierGenerator identifierGenerator) { ? ? ? ? IDENTIFIER_GENERATOR = identifierGenerator; ? ? } ? ? ? /** ? ? ?* 使用ThreadLocalRandom獲取UUID獲取更優(yōu)的效果 去掉"-" ? ? ?*/ ? ? public static String get32UUID() { ? ? ? ? ThreadLocalRandom random = ThreadLocalRandom.current(); ? ? ? ? return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY); ? ? } }
到此這篇關(guān)于Mybatis-plus如何提前獲取實(shí)體類(lèi)用雪花算法生成的ID的文章就介紹到這了,更多相關(guān)Mybatis-plus獲取雪花算法生成ID內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus如何使用sql的date_format()函數(shù)查詢(xún)數(shù)據(jù)
- MyBatis-Plus?實(shí)體類(lèi)注解的實(shí)現(xiàn)示例
- Mybatis-Plus實(shí)體類(lèi)繼承Model的使用小結(jié)
- MyBatis-Plus動(dòng)態(tài)返回實(shí)體類(lèi)示例詳解
- Mybatis-Plus實(shí)體類(lèi)注解方法與mapper層和service層的CRUD方法
- mybatis-plus實(shí)體類(lèi)中出現(xiàn)非數(shù)據(jù)庫(kù)映射字段解決辦法
- 詳解mybatis-plus實(shí)體類(lèi)中字段和數(shù)據(jù)庫(kù)中字段名不對(duì)應(yīng)解決辦法
- MyBatis-Plus ORM數(shù)據(jù)庫(kù)和實(shí)體類(lèi)映射方式
相關(guān)文章
SpringBoot整合Spring?Data?JPA的詳細(xì)方法
JPA全稱(chēng)為Java Persistence API(Java持久層API),是一個(gè)基于ORM的標(biāo)準(zhǔn)規(guī)范,在這個(gè)規(guī)范中,JPA只定義標(biāo)準(zhǔn)規(guī)則,不提供實(shí)現(xiàn),本文重點(diǎn)給大家介紹SpringBoot整合Spring?Data?JPA的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-02-02SpringBoot如何指定某些類(lèi)優(yōu)先啟動(dòng)
這篇文章主要介紹了SpringBoot如何指定某些類(lèi)優(yōu)先啟動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java線(xiàn)程狀態(tài)及切換、關(guān)閉線(xiàn)程的正確姿勢(shì)分享
這篇文章主要給大家介紹了關(guān)于Java線(xiàn)程狀態(tài)及切換、關(guān)閉線(xiàn)程的正確姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java聊天室之使用Socket實(shí)現(xiàn)傳遞圖片
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易聊天室之使用Socket實(shí)現(xiàn)傳遞圖片功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10IDEA 開(kāi)發(fā)多項(xiàng)目依賴(lài)的方法(圖文)
這篇文章主要介紹了IDEA 開(kāi)發(fā)多項(xiàng)目依賴(lài)的方法(圖文),本文講一下關(guān)于使用IntelliJ IDEA基于Maven創(chuàng)建多模塊項(xiàng)目的實(shí)際開(kāi)發(fā),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10Java8中用foreach循環(huán)獲取對(duì)象的index下標(biāo)詳解
這篇文章主要給大家介紹了關(guān)于Java8中用foreach循環(huán)獲取對(duì)象的index下標(biāo)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04IntelliJ IDEA 詳細(xì)圖解最常用的配置(適合剛剛用的新人)
這篇文章主要介紹了IntelliJ IDEA 詳細(xì)圖解最常用的配置,本篇教程非常適合剛剛用的新人,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08