最近项目遇到一个需求,单号从001开始递增

下面用到了redis处理

代码如下:

public String getId() {
    String key = "providerManager";
    Long incr = getIncr(key);
    if (incr == 0) {
        incr = getIncr(key);//从001开始
    }
    DecimalFormat df = new DecimalFormat("000");//三位***
    return df.format(incr);
}

public Long getIncr(String key) {
    RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
    Long increment = entityIdCounter.getAndIncrement();
    return increment;
}

 

使用Redis让单号从001递增

 

效果如下图所示

使用Redis让单号从001递增

相关文章: