Mybatis-plus如何提前獲取實體類用雪花算法生成的ID
Mybatis-plus中,通過設置@TableId可以讓Mybatis-plus自動為我們生成雪花算法的ID號,該ID號是一個長整型數(shù)據(jù),非常方便。但是雪花算法的ID號是在Insert執(zhí)行的時候生成的,我們在Insert執(zhí)行前是不知道Entity會獲得一個什么ID號。
但是在某些情況下,我們想提前獲取這個ID,這樣可以通過一些計算來生成其他字段的值。例如我們用此ID號做秘鑰來加密密碼。
這種情況下,需要提前生成ID號,手動設置給Entity。在實體類中,通過下面這個注解將自動ID改為有程序控制輸入:
@TableId(type=IdType.INPUT)
那么我們需要用雪花算法生成一個ID號。是不是還需要另外自己寫一個雪花算法生成類呢?
完全不用。因為Mybatis-plus中內(nèi)置了雪花算法生成功能,我們找出來調(diào)用就行了,就是下面這個類:
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
我們可以這樣調(diào)用:
Long ID=IdWorker.getId(entity);
如果有更高的需求,還可以設置雪花算法的其他參數(shù)。
這個類源碼如下:
/* ?* 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 { ? ? ? /** ? ? ?* 主機和進程的機器碼 ? ? ?*/ ? ? private static IdentifierGenerator IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator(); ? ? ? /** ? ? ?* 毫秒格式化時間 ? ? ?*/ ? ? 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(); ? ? } ? ? ? /** ? ? ?* 格式化的毫秒時間 ? ? ?*/ ? ? public static String getMillisecond() { ? ? ? ? return LocalDateTime.now().format(MILLISECOND); ? ? } ? ? ? /** ? ? ?* 時間 ID = Time + ID ? ? ?* <p>例如:可用于商品訂單 ID</p> ? ? ?*/ ? ? public static String getTimeId() { ? ? ? ? return getMillisecond() + getIdStr(); ? ? } ? ? ? /** ? ? ?* 有參構(gòu)造器 ? ? ?* ? ? ?* @param workerId ? ? 工作機器 ID ? ? ?* @param dataCenterId 序列號 ? ? ?* @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); ? ? } }
到此這篇關于Mybatis-plus如何提前獲取實體類用雪花算法生成的ID的文章就介紹到這了,更多相關Mybatis-plus獲取雪花算法生成ID內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- mybatis-plus如何使用sql的date_format()函數(shù)查詢數(shù)據(jù)
- MyBatis-Plus?實體類注解的實現(xiàn)示例
- Mybatis-Plus實體類繼承Model的使用小結(jié)
- MyBatis-Plus動態(tài)返回實體類示例詳解
- Mybatis-Plus實體類注解方法與mapper層和service層的CRUD方法
- mybatis-plus實體類中出現(xiàn)非數(shù)據(jù)庫映射字段解決辦法
- 詳解mybatis-plus實體類中字段和數(shù)據(jù)庫中字段名不對應解決辦法
- MyBatis-Plus ORM數(shù)據(jù)庫和實體類映射方式
相關文章
SpringBoot整合Spring?Data?JPA的詳細方法
JPA全稱為Java Persistence API(Java持久層API),是一個基于ORM的標準規(guī)范,在這個規(guī)范中,JPA只定義標準規(guī)則,不提供實現(xiàn),本文重點給大家介紹SpringBoot整合Spring?Data?JPA的相關知識,感興趣的朋友一起看看吧2022-02-02Java8中用foreach循環(huán)獲取對象的index下標詳解
這篇文章主要給大家介紹了關于Java8中用foreach循環(huán)獲取對象的index下標的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04IntelliJ IDEA 詳細圖解最常用的配置(適合剛剛用的新人)
這篇文章主要介紹了IntelliJ IDEA 詳細圖解最常用的配置,本篇教程非常適合剛剛用的新人,本文圖文并茂給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08