生成的流水号格式:字符串+"转换后的时间串"+多少位的流水号。
/**
* @Title: createCode
* @Description: TODO(生成流水号[前缀+日期+流水号])
* @param prefix 前缀
* @param dateStr 日期串
* @param length 流水号长度 length要大于0
* @return
* @author XXX 2018年1月8日 下午5:53:52
*/
public static String createCode(String prefix,String dateStr,int length){
StringBuffer code = new StringBuffer();
if((prefix != null && !prefix.equals("")) || (dateStr != null && !dateStr.equals(""))){
if(prefix != null && !prefix.equals("")){
code.append(prefix);
}
if(dateStr != null && !dateStr.equals("")){
code.append(dateStr);
}
synchronized (code) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
SerialNumberService ser = (SerialNumberService) ac.getBean("serialNumberService");
SerialNumber serialNumber = new SerialNumber();
serialNumber.setPrefix(prefix);
serialNumber.setDateStr(dateStr);
serialNumber.setLength(length);
SerialNumber serialNumber_ = ser.getSerialNumber(serialNumber);
Integer seq = null;
if(serialNumber_ != null) {
seq = serialNumber_.getSeq();
System.out.println("seq:"+seq);
}
if(seq == null){
seq = 1;
serialNumber.setSeq(seq);
ser.addSerialNumber(serialNumber);
}else{
seq++;
serialNumber.setSeq(seq);
ser.updateSerialNumber(serialNumber);
}
if(length > 0){
code.append(String.format("%0"+length+"d", seq));
}else{
code.append(seq);
}
}
}
return code.toString();
}
流水号表:
CREATE TABLE "SYS_CODE_FACTORY"
( "PREFIX_" VARCHAR2(20),
"DATESTR" VARCHAR2(25),
"SEQ" NUMBER(*,0),
"LENGTH" NUMBER(38,0)
);
ALTER TABLE "SYS_CODE_FACTORY" ADD PRIMARY KEY ("PREFIX_", "DATESTR", "LENGTH");
COMMENT ON COLUMN "SYS_CODE_FACTORY"."PREFIX_" IS '流水号前缀';
COMMENT ON COLUMN "SYS_CODE_FACTORY"."DATESTR" IS '时间格式';
COMMENT ON COLUMN "SYS_CODE_FACTORY"."SEQ" IS '流水号数字编号';
COMMENT ON COLUMN "SYS_CODE_FACTORY"."LENGTH" IS '流水号的数字编码长度';
COMMENT ON TABLE "SYS_CODE_FACTORY" IS '流水号表';
源码下载
来源:CSDN
作者:qq_30270931
链接:https://blog.csdn.net/qq_30270931/article/details/79141354