朝天锅是哪个地方的菜,蛇王站好趴下,薛蒙
分析:既然是订单号/交易流水号,首先是不能重复,其次需考虑到性能问题。
设计如下:
"HF"+时间戳+随机数+循环数
代码如下:
1 int x = 1000; 2 for(int i=0;i<10;i++){ 3 x+=1; 4 System.out.println("HF"+System.currentTimeMillis()+RandomUtils.getNo(2)+x); 5 }
其中:RandomUtils类
1 package com.test.common.util; 2 3 import org.apache.commons.lang.RandomStringUtils; 4 5 public class RandomUtils 6 { 7 private static String randString = ""; 8 9 public synchronized static String getNo(int k) 10 { 11 if (randString.length() > 20000) 12 { 13 randString = ""; 14 } 15 String rno = getNoNo(k); 16 while (randString.indexOf(rno + ",") >= 0) 17 { 18 rno = getNoNo(k); 19 } 20 randString += rno + ","; 21 return rno; 22 } 23 24 private static String getNoNo(int k) 25 { 26 try 27 { 28 Thread.sleep(1); 29 } 30 catch (InterruptedException e) 31 { 32 e.printStackTrace(); 33 } 34 return RandomStringUtils.randomNumeric(k); 35 } 36 }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论