在处理需要生成的id时,java有自带的id生成根据UUID,但不是很满足在实际应用中的需求,所以自己手动处理
package com.xiaobai.minio.util;
import cn.hutool.core.util.IdUtil;
import java.io.Serializable;
/**
* @author yangdaji
*
*/
/**
* @author yangdaji
* @version 1.0
* @Description: uuid8位
* @date 22-2-25 下午 10:49
*/
public class UuidUtils implements Serializable {
private static final String[] strings = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
/**
* 生成随机uuid,简化横线
*
* @return
*/
public static String getUuid() {
return IdUtil.fastSimpleUUID();
}
/**
* 生成 8位不重复id,性能优于32位id
*
* @return
*/
public static String getShortUuid() {
StringBuffer stringBuffer = new StringBuffer();
String uuid = IdUtil.fastSimpleUUID();
for (int i = 0; i < 8; i++) {
String substring = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(substring, 16);
stringBuffer.append(strings[x % 0x3E]);
}
return stringBuffer.toString();
}
}
- 以上就是手动处理的一个简单uuid生成工具了