自定义数据库id增长

6 篇文章 0 订阅

一般对对于数据量大的数据 , 一般的数据库自增长满足不了自增长 的因此采用时间戳的方式进行自定义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);
}


}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值