SpringBoot使用redis生成訂單號(hào)的實(shí)現(xiàn)示例
項(xiàng)目場(chǎng)景:
在開發(fā)電商系統(tǒng)等需要生成唯一訂單號(hào)的應(yīng)用程序中,我們經(jīng)常會(huì)遇到需要生成唯一訂單號(hào)的需求。本文將介紹如何使用Spring Boot和Redis來(lái)生成唯一的訂單號(hào),并提供相應(yīng)的代碼示例。
在開始之前,需要確保已經(jīng)安裝并配置好了Java開發(fā)環(huán)境、Spring Boot框架和Redis數(shù)據(jù)庫(kù)。
解決方案:
訂單號(hào)生成規(guī)則: DD+年月日+5位流水號(hào),流水號(hào)當(dāng)天有效,第二天重新計(jì)數(shù)。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* redis的increment 遞增方法 | 處理防重復(fù)和并發(fā)問(wèn)題
*/
@Component
public class OrderNumberCodeUtils {
private static final String PREFIX = "DD";
private static final String DATE_FORMAT = "yyyyMMdd";
private static final String ORDER_SERIAL_NUMBER = "order_serial_number";
private static RedisTemplate redisTemplate;
@Autowired
public void redisTemplate(RedisTemplate redisTemplate){
OrderNumberCodeUtils.redisTemplate = redisTemplate;
}
public static String generateOrderNumber() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(PREFIX);
// 獲取當(dāng)前日期
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String currentDate = dateFormat.format(new Date());
stringBuffer.append(currentDate);
// 獲取流水號(hào)
Long increment = redisTemplate.opsForValue().increment(ORDER_SERIAL_NUMBER, 1);
/**
* 返回值過(guò)期時(shí)間,單位為秒。
* 如果返回-2,則表示該鍵不存在;
* 如果返回-1,則表示該鍵沒有設(shè)置過(guò)期時(shí)間;
*/
Long expire = redisTemplate.getExpire(ORDER_SERIAL_NUMBER, TimeUnit.SECONDS);
if(expire == -1){
// 獲取距離當(dāng)天結(jié)束的秒數(shù)
LocalDateTime endOfDay = LocalDate.now().atTime(23, 59, 59);
long secondsToMidnight = LocalDateTime.now().until(endOfDay, ChronoUnit.SECONDS);
//初始設(shè)置過(guò)期時(shí)間
redisTemplate.expire(ORDER_SERIAL_NUMBER, secondsToMidnight, TimeUnit.SECONDS);
}
String format = String.format("%05d", increment);
stringBuffer.append(format);
return stringBuffer.toString();
}
}到此這篇關(guān)于SpringBoot使用redis生成訂單號(hào)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot redis生成訂單號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot右鍵maven build成功但是直接運(yùn)行main方法出錯(cuò)的解決方案
這篇文章主要介紹了Spring Boot-右鍵maven build成功但是直接運(yùn)行main方法出錯(cuò)的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Java數(shù)據(jù)結(jié)構(gòu)與算法之棧(Stack)實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第二篇,Java數(shù)據(jù)結(jié)構(gòu)與算法之棧Stack實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
SpringBoot整合Servlet和Filter和Listener組件詳解
這篇文章主要介紹了SpringBoot整合Servlet和Filter和Listener組件詳解,在整合某報(bào)表插件時(shí)就需要使用Servlet,Spring Boot中對(duì)于整合這些基本的Web組件也提供了很好的支持,需要的朋友可以參考下2024-01-01
Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析
本文將探討如何在Java中有效地應(yīng)對(duì)這些挑戰(zhàn),介紹一種強(qiáng)大的工具并發(fā)Map,它能夠幫助您管理多線程環(huán)境下的共享數(shù)據(jù),確保數(shù)據(jù)的一致性和高性能,深入了解Java中的并發(fā)Map實(shí)現(xiàn),包括ConcurrentHashMap和ConcurrentSkipListMap,及相關(guān)知識(shí)點(diǎn)2023-09-09
Springmvc如何返回xml及json格式數(shù)據(jù)
這篇文章主要介紹了Springmvc如何返回xml及json格式數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot使用Validator進(jìn)行參數(shù)校驗(yàn)實(shí)戰(zhàn)教程(自定義校驗(yàn),分組校驗(yàn))
這篇文章主要介紹了SpringBoot使用Validator進(jìn)行參數(shù)校驗(yàn)(自定義校驗(yàn),分組校驗(yàn))的實(shí)戰(zhàn)教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-07-07

