Mybatis-plus中,通過設置@TableId可以讓Mybatis-plus自動爲我們生成雪花算法的ID號,該ID號是一個長整型數據,非常方便。但是雪花算法的ID號是在Insert執行的時候生成的,我們在Insert執行前是不知道Entity會獲得一個什麼ID號。
但是在某些情況下,我們想提前獲取這個ID,這樣可以通過一些計算來生成其他字段的值。例如我們用此ID號做祕鑰來加密密碼。
這種情況下,需要提前生成ID號,手動設置給Entity。在實體類中,通過下面這個註解將自動ID改爲有程序控制輸入:
@TableId(type=IdType.INPUT)
那麼我們需要用雪花算法生成一個ID號。是不是還需要另外自己寫一個雪花算法生成類呢?
完全不用。因爲Mybatis-plus中內置了雪花算法生成功能,我們找出來調用就行了,就是下面這個類:
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
我們可以這樣調用:
Long ID=IdWorker.getId(entity);
如果有更高的需求,還可以設置雪花算法的其他參數。
這個類源碼如下:
/*
* Copyright (c) 2011-2020, baomidou ([email protected]).
* <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();
}
/**
* 有參構造器
*
* @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獲取更優的效果 去掉"-"
*/
public static String get32UUID() {
ThreadLocalRandom random = ThreadLocalRandom.current();
return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY);
}
}