自定义数据库id增长

一般对对于数据量大的数据 , 一般的数据库自增长满足不了自增长 的因此采用时间戳的方式进行自定义id防止重复




import java.util.Date;
import java.util.Random;


/**
 */
public class CreateId {
// private static CreateId instance = null;
public static int count = 0;


// private CreateId() {
//
// }
//
// private static synchronized void syncInit() {
// if (instance == null) {
// instance = new CreateId();
// }
// }
//
// public static CreateId getInstance() {
// if (instance == null) {
// syncInit();
// }
// return instance;
// }


public static synchronized String getid() throws Exception {
if (CreateId.count > 998) {
CreateId.count = 0;
}
CreateId.count++;
//Thread.sleep(5);
String counts = "";
if ((CreateId.count + "").length() == 1) {
counts = "00" + CreateId.count;
} else if ((CreateId.count + "").length() == 2) {
counts = "0" + CreateId.count;
} else {
counts = "" + CreateId.count;
}
return new Date().getTime() + counts;
}


public static String getNumber3FromRandom() {
Random r = new Random();
int xx = r.nextInt(100);
while (xx < 10) {
xx = r.nextInt(100);
}
return String.valueOf(xx);
}


public static void main(String[] args) throws Exception {
for (int i = 0; i < 20; i++) {
String id = CreateId.getid();
System.out.println(id);
}


}


}

发布了33 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/sinat_26987533/article/details/79930222