java生成订单编号 日期+流水号

java生成订单编号 日期+流水号

    • 采用redis记录流水号
    • 查询数据库表当前最大流水号

参考网上的方法,一般有两种,一种是采用redis记录流水号,另一种是每次都去数据库查询最大值+1,我这里做了个结合,redis为空的时候才去数据库取。

采用redis记录流水号

// An highlighted block
package com.zwwl.mm.business.service;import com.zwwl.mm.business.mapper.OrderDao;
import com.zwwl.mm.system.exception.TransactionException;
import com.zwwl.mm.system.service.RedisService;
import com.zwwl.mm.system.utils.RedisKeyUtils;
import com.zwwl.mm.system.utils.StrUtils;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;/*** 单据编号生成*/
@Service
public class SerialNumberService {private static final String PATTERN = "000000000000000000";@Resourceprivate RedisService<String, Integer> redisService;@Resourceprivate OrderDao orderDao;/*** 生成 前缀 + yyyyMMdd + XXXX  编号* @param preSn 前缀* @param width 流水号位数* @return*/public synchronized String generateSn(String preSn, int width){String todayDate = new SimpleDateFormat("yyyyMMdd").format(new Date());StringBuilder sb = new StringBuilder(preSn).append(todayDate);String key = RedisKeyUtils.PREFIX_SERIAL_NUMBER + sb.toString();Integer count = redisService.getForValue(key);if (count != null) {count = count + 1;} else {//方案二 从数据库查询当天的最大的流水号String maxSn = orderDao.getMaxSnByPrefix(sb.toString());if(StrUtils.isNotEmpty(maxSn)){String str = maxSn.substring(sb.length());count = Integer.parseInt(str) + 1;}else{count = 1;}}if(count.toString().length() > width){throw new TransactionException("单据流水号已经超过指定长度");}DecimalFormat df = new DecimalFormat(PATTERN.substring(0, width));sb.append(df.format(count));//写入redisredisService.setForValue(key, count,1L, TimeUnit.DAYS);return sb.toString();}}

查询数据库表当前最大流水号

<select id="getMaxSnByPrefix" resultType="java.lang.String">SELECT snFROM t_orderWHERE sn LIKE CONCAT(#{prefix},'%')order by sn desclimit 1;</select>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部