本文共 1838 字,大约阅读时间需要 6 分钟。
importjava.math.BigDecimal;importorg.quickbundle.project.RmProjectHelper;importorg.quickbundle.project.common.vo.RmCommonVo;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** 流水号生成器
*@authorAdministrator
**/
public classSeriaNumberGenerator {
Logger logger= LoggerFactory.getLogger(SeriaNumberGenerator.class);private static int MAX_VALUE=9999;private static int MIN_VALUE = 1;/*** 根据业务标识符取到下一个流水号
*@parambusinessCode
*@return
*/
public synchronizedString getNextVal(String businessCode){intcurrentValue ;intnextValue ;
RmCommonVo comonVo=null;try{
comonVo= RmProjectHelper.getCommonServiceInstance().doQueryForObject("select CURRENT_VALUE from SERIAL_NUMBER WHERE KEY =‘"+businessCode+"‘");
}catch( org.springframework.dao.EmptyResultDataAccessException ex){
logger.error("结果集为空,SpringJdbc抛出异常,不影响程序执行...");
}//该key不存在(存放进去)
if(comonVo == null ||comonVo.size()== 0){
currentValue=MIN_VALUE;
nextValue= currentValue +1;
RmProjectHelper.getCommonServiceInstance().doUpdate(" insert into SERIAL_NUMBER (KEY, CURRENT_VALUE ) VALUES (‘"+businessCode+"‘,‘"+nextValue+"‘)");
}// else{
currentValue= ((BigDecimal)comonVo.get("CURRENT_VALUE")).intValue();//已经到达最大值
if(MAX_VALUE==currentValue){
nextValue=MIN_VALUE;
}else{
nextValue= currentValue+1;
}
RmProjectHelper.getCommonServiceInstance().doUpdate("update SERIAL_NUMBER set CURRENT_VALUE=‘"+nextValue+"‘ WHERE KEY=‘"+businessCode+"‘");
}
String finalValue= this.formatString(currentValue);returnfinalValue ;
}publicSeriaNumberGenerator(){
}/*** 将数字转换为字符串(当不足4位时高位补0)
*@paraminput
*@return
*/
public String formatString(intinput){
String result ;//大于1000时直接转换成字符串返回
if(input > 1000){
result= input+"";
}else{//根据位数的不同前边补不同的0
int length = (input +"").length();if(length == 1){
result= "000"+input;
}else if(length ==2){
result= "00"+input;
}else{
result= "0"+input;
}
}returnresult;
}
}
转载地址:http://tqudv.baihongyu.com/