一:根据主键获取自增流水号:
代码:
package cn.itcast;
/*
* 获取自增流水号
*/
public class SerialNumberUtil {
public static void main(String[] args) {
String a = getId("HT202303",1);
System.out.println("流水号:"+a);
}
private static final Integer ONE = 1;
private static final Integer TWO = 2;
private static final Integer THREE = 3;
// par为自定义前缀,想要几位数可以自行修改0的数量
// id获取方式为从数据库表中查询现在最大id+1(SELECT id+1 from table ORDER BY id DESC LIMIT 1)
public static String getId(String par, Integer id){
//判断位数
String s = id + "";
int count = s.length();
String strNum = par;
if (ONE == count){
strNum += "000"+ String.valueOf(id);
}else if (TWO == count){
strNum += "00"+ String.valueOf(id);
}else if (THREE == count){
strNum += "0" + String.valueOf(id);
}else {
strNum += String.valueOf(id);
}
return strNum;
}
}
测试输出:
流水号:HT2023030001