在使用数据库的时候,经常需要生成主键,用过数据库的自增长,但ID太有序了,很容易就能让人知道下一条记录的ID。也用过Hibernate的UUID生成,但生成的主键似乎又太长了点,于是想着自己写一个工具来实现。 首先想到的便是用时间戳+随机码的方式,对于并发量不是特别特别特别高的情况来说,基本是够用的,于是我一般都用Long.parseLong(""+(int)(Math.random()*100)+(new Date()).getTime()+(int)(Math.random()*100))的方式来生成一个Long型的值,但这个值一来太长了,二来很容易给人找出规律,毕竟时间戳部分的前几位是一致的,于是考虑到将这个Long值转成类似于16进制之类的字符串。不过数字+字母(分大小写)一共有62位,16进制只用到了其中的一部分,还有大把没用的,有点浪费。在参考了Long类的toHexString()方法源码后,自己改写了一个工具类。
package com.hing.tools;
import java.util.Date; /** *//** * author:hingwu email:hing3@163.com QQ:550598 MSN:hing3wu@hotmail.com(很少开) * * 2007-3-28 上午11:40:18 */ public class MyLong ...{ public static final long MIN_VALUE = 0x8000000000000000L; public static final long MAX_VALUE = 0x7fffffffffffffffL; final static char[] digits = ...{ ‘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‘, ‘-‘, ‘_‘ }; private static String toUnsignedString(long i, int shift) ...{ char[] buf = new char[64]; int charPos = 64; int radix = 1 << shift; long mask = radix - 1; do ...{ buf[--charPos] = digits[(int) (i & mask)]; i >>>= shift; } while (i != 0); return new String(buf, charPos, (64 - charPos)); } // j为2的次方,如转成16进制就是4,32进制就是5... public static String getRand(long i,int j)...{ return toUnsignedString(i, j); } // 随机码+时间戳+随机码的生成 public static Long getRand()...{ String str1,str2,str3; str1=getRandStr(2); str3=getRandStr(3); str2=(new Date()).getTime()+""; //System.out.println(str1+str2+str3); return Long.parseLong(str1+str2+str3); } // 主键生成 public static String getKey()...{ return getRand(getRand(),6); } // 生成指定长度的随机串 public static String getRandStr(Integer length)...{ String str=""; while(str.length()!=length)...{ str=(Math.random()+"").substring(2,2+length); } return str; } }
测试类:
package com.test;
import java.util.Date; import com.hing.tools.MyLong; /** *//** * author:hingwu * email:hing3@163.com * QQ:550598 * MSN:hing3wu@hotmail.com(很少开) * * 2007-3-15 下午04:37:26 */ public class Test ...{ /** *//** * @param args * @throws IOException * @throws InterruptedException */ public static void main(String[] args)...{ // TODO Auto-generated method stub for(int i=0;i<10;i++)...{ System.out.println("第"+i+"个:"+MyLong.getKey()); } } } 测试结果: 第0个:qK43fCDV 如果把digits中的字符顺序打乱的话,就更难给人找到规律了。 |
|
来自: shaobin0604@1... > 《Java》