搜索
简帛阁>技术文章>UUID生成工具

UUID生成工具

public class UUIDUtils {
private static SecureRandom SEEDER_STATIC = null;
private static byte[] ADDRESS = null;
private static String MID_VALUE_STATIC = null;
private String midValue = null;
private SecureRandom seeder = null;

static {
try {
ADDRESS = InetAddress.getLocalHost().getAddress();
StringBuffer buffer = new StringBuffer(8);
buffer.append(toHex(toInt(ADDRESS), 8));
MID_VALUE_STATIC = buffer.toString();
SEEDER_STATIC = new SecureRandom();
SEEDER_STATIC.nextInt();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public UUIDUtils() {
StringBuffer buffer = new StringBuffer(16);
buffer.append(MID_VALUE_STATIC);
buffer.append(toHex(System.identityHashCode(this), 8));
midValue = buffer.toString();
seeder = new SecureRandom();
seeder.nextInt();
}

/**
* 该方法用来产生一个32位的唯一的标记String
*
* @param obj
* 传入一个参考的对象
* @return
*/
public static String generate(Object obj) {
StringBuffer uid = new StringBuffer(32);

// get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int) (currentTimeMillis & -1L), 8));

// get the internet address
uid.append(MID_VALUE_STATIC);

// get the object hash value
uid.append(toHex(System.identityHashCode(obj), 8));

// get the random number
uid.append(toHex(getRandom(), 8));

return uid.toString();
}

/**
* 该方法用来产生一个32位的String唯一标记
*/
public String generate() {
StringBuffer uid = new StringBuffer(32);

// get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int) (currentTimeMillis & -1L), 8));

// get the internet address
uid.append(midValue);

// get the random number
uid.append(toHex(seeder.nextInt(), 8));

return uid.toString();
}

private static String toHex(int value, int length) {
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer buffer = new StringBuffer(length);
int shift = length - 1 << 2;
for (int i = -1; ++i < length;) {
buffer.append(hexDigits[value >> shift & 0xf]);
value <<= 4;
}

return buffer.toString();
}

private static int toInt(byte[] bytes) {
int value = 0;
for (int i = -1; ++i < bytes.length;) {
value <<= 8;
value |= bytes[i];
}

return value;
}

private static synchronized int getRandom() {
return SEEDER_STATIC.nextInt();
}

publicclassUUIDUtils{privatestaticSecureRandomSEEDER_STATICnull;privatestaticbyte[]ADDRESSnull;priva
packagecomqiyuanutil;importjavautilUUID;publicclassRanNum{/***生成随机数<br>*GUID:即GloballyUniqueId
UID含义是通用唯一识别码(UniversallyUniqueIdentifier),这是一个软件建构的标准,也是被开源软件基金会(OpenSoftwareFoundation,OSF)的组织在分布式
UUID含义是通用唯一识别码(UniversallyUniqueIdentifier),这是一个软件建构的标准,也是被开源软件基金会(OpenSoftwareFoundation,OSF)的组织在分布
一、UUID是什么UUID就是UniversalUniqueIDentifier的缩写,它是一个128位,16字节的值,并确保在时间和空间上唯一。它是把硬件地址、时间以及随机数结合在一起,它保证对在
UUID函数在MySQL中,可以用uuid()函数来生成一个UUID,如下图:replace函数默认生成uuid含有'',我们可以使用replace函数替换掉'',SQL如下:selectrepl
importjavautilUUID;publicclassMainProcess{publicstaticvoidmain(String[]args){UUIDuuidUUIDrandomUUID(
*生成UUID*@returns*/functionUUID(){vars[];varhexDigits0123456789abcdef;for(vari0;i<36;i++){s[i]hexD
/***生成随机uuid*/exportfunctionuuid(){return'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'replace(/[xy]/g,function(
使用jpa可以生成uuid,但是我直接添加数据没有id值会报错,只在程序中有效,如果直接修改数据库需要手动填写,另外长度不要乱填,之前填了200,找了半天才找到原因。packagecomjava123