uuid算法
package com.java.activiti.page;
import java.util.UUID;
/**
* <p>Title: Chief Designer</p>
* <p>Description: neration ( www.neration.com)</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: neration soft</p>
*
* @author zhiguo chen (zhiguochen@hotmail.com)
* @version 1.0
*/
public final class UuidUtils {
private static final int LOMASK = 15;
private static final int HIMASK = 240;
private static final int LO8BITMASK = 255;
private static final int BITS8 = 8;
private static final int BYTELEN = 16;
private static final long MAX_LONG = 2147483647;
public static String generateUUID() {
return bytesToString(generateByteArray());
}
private static byte[] generateByteArray() {
byte bytes[] = new byte[16];
int idx = 15;
long val = (long) (Math.random() * MAX_LONG) + (long) (Math.random() * MAX_LONG) * 1000000000;
for (int i = 0; i < 8; i++) {
bytes[idx--] = (byte) (int) (val & (long) 255);
val >>= 8;
}
val = (long) System.currentTimeMillis();
for (int i = 0; i < 8; i++) {
bytes[idx--] = (byte) (int) (val & (long) 255);
val >>= 8;
}
return bytes;
}
private static String bytesToString(byte bytes[]) {
// if(16 != bytes.length)
// return "** Bad UuidUtils Format/Value **";
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 16; i++) {
buf.append(Integer.toHexString(hiNibble(bytes[i])));
buf.append(Integer.toHexString(loNibble(bytes[i])));
}
return buf.toString();
}
private static final byte loNibble(byte b) {
return (byte) (b & 0xf);
}
private static final byte hiNibble(byte b) {
return (byte) (b >> 4 & 0xf);
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
System.out.println("" + UuidUtils.generateUUID());
}
}
}
实体类里要加入
private static final long serialVersionUID = 1L;
在接口里设置
例如:
user.setId(UuidUtils.generateUUID());
int userResult=userService.addUser(user);