前言
本篇文章是學習B站黑馬程序員蒼穹外賣的學習筆記📑。我的學習路線是Java基礎語法-JavaWeb-做項目,管理端的功能學習完之后,就進入到了用戶端微信小程序的開發,用戶端開發的流程大致為用戶登錄—商品瀏覽(其中涉及到緩存,之前寫過)—添加/查看/清空購物車—下單支付(到現在沒搞懂😵?💫)—地址簿—訂單查詢/超時/取消處理(巨多)…還有我沒學到的🤪🤪
從訂單支付這里,一直到day10,要不是很難的看不懂的,要不就是一些之前學過的,我是直接根據課程資料將代碼導入,然后稍微看了看邏輯(太多太難了邊學邊做總結吧)。
🤔如果用戶下單但一直不支付,這時就需要我們設置一定的時間,定時清理掉(既然不給錢,那我直接把你轟出去😡)。
🔍下面來看看訂單狀態定時處理功能的開發
1. 🙌先來了解一下Spring Task 和 Tack_cron表達式
Spring Task 是 Spring 框架提供的一個輕量級的任務調度模塊,它允許你在 Spring 應用中簡單地配置和執行定時任務。使用 Spring Task,你可以通過注解或者 XML 配置的方式定義定時任務,無需引入額外的庫或依賴復雜的配置。(這個是ai給我的概念,我看著也有點懵🤪)
?你可以把它理解成 手機里的“鬧鐘”App,只不過它是給你的 Java 程序用的。
你想讓程序:每次下單后15分支不付款,就清除訂單
只要告訴 Spring Task:“到時間就幫我做這件事”,它就會乖乖地按時執行。
cron表達式是一個字符串,通過cron表達式可以定義任務觸發的時間。
一條 cron 任務的基本格式如下:
┌───────────── 分鐘 (0 - 59)
│ ┌──────────── 小時 (0 - 23)
│ │ ┌───────────── 日期 (1 - 31)
│ │ │ ┌────────────── 月份 (1 - 12)
│ │ │ │ ┌─────────────── 星期幾 (0 - 6)(0 和 7 都表示周日)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * * 要執行的命令
常用符號說明:
🙌例:
0 0 * * * /backup.sh :每天零點執行 /backup.sh
*/10 * * * * /ping.sh:每 10 分鐘執行一次
30 8 * * 1-5 /check.log:周一到周五早上 8:30 執行
?注:可以直接用工具生成(更方便):奇Q在線Cron表達式生成器
2. 下面來舉個例子來看看它是怎么使用的
①導入maven
②啟動類添加注解@EnableScheduling開啟任務調度
③自定義定時任務類
package com.sky.task;
...
/*** 自定義定時任務*/@Component
@Slf4j
public class MyTask {/*** 定時任務1*/@Scheduled(cron = "0/5 * * * * ?")public void executeTask(){log.info("定時任務1開始執行:{}", new Date());}}
運行后:控制臺每5秒輸出一次
🧠最后來看看在實際項目中怎么使用:
先來看看要實現的功能:
- 處理超時訂單:15分鐘未支付,取消訂單
- 每天凌晨1點處理一直處于派送中的訂單
🔍??下面來看看代碼:
就是結合剛剛學的內容,多加了兩個注解和一個 select 語句(查詢符合條件的訂單再對其進行更改):
在sky-server的task包(P125創建的)下創建一個OrderTask類:
package com.sky.task;
.../*** 訂單定時任務*/@Component
@Slf4j
public class OrderTask {@Autowiredprivate OrderMapper orderMapper;/*** 處理超時訂單**/@Scheduled(cron = "0 * * * * ? ") // 每分鐘執行一次//@Scheduled(cron = "1/5 * * * * ?")public void processTimeOutOrder(){log.info(" 定時處理超時訂單:{}", LocalDateTime.now());LocalDateTime time = LocalDateTime.now().plusMinutes(-15);List<Orders> orderList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, time);if(orderList != null && orderList.size() > 0){for (Orders orders : orderList) {orders.setStatus(Orders.CANCELLED);orders.setCancelReason("支付超時,自動取消");orders.setCancelTime(LocalDateTime.now());orderMapper.update(orders);}}}/*** 處理一直處于派送中狀態的訂單*/@Scheduled(cron = "0 0 1 * * ? ")//每日凌晨1點執行一次//@Scheduled(cron = "0/5 * * * * ?")public void processDeliveringOrder(){log.info("定時處理支付超時訂單:{}", LocalDateTime.now());List<Orders> orderList =orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().plusMinutes(-60));if(orderList != null && orderList.size() > 0){for (Object orders : orderList) {Orders order = (Orders) orders;order.setStatus(Orders.COMPLETED);orderMapper.update(order);}}}}
mapper包下的OrderMapper:
/*** 統計指定時間區間內訂單數量* @param status* @param orderTime*/@Select("select * from orders where status = #{status} and order_time < #{orderTime}")List<Orders> getByStatusAndOrderTimeLT (Integer status, LocalDateTime orderTime);/*** 修改訂單信息* @param orders*/void update(Orders orders);//這個語句有點長,放到其對應XML文件中了
<update id="update" parameterType="Orders">update orders<set><if test="number != null"> number=#{number}, </if><if test="status != null"> status=#{status}, </if><if test="addressBookId != null"> address_book_id=#{addressBookId}, </if><if test="orderTime != null"> order_time=#{orderTime},</if><if test="checkoutTime != null"> checkout_time=#{checkoutTime}, </if><if test="payMethod != null"> pay_method=#{payMethod}, </if><if test="payStatus != null"> pay_status=#{payStatus}, </if><if test="amount != null"> amount=#{amount}, </if><if test="remark != null"> remark=#{remark}, </if><if test="phone != null"> phone=#{phone}, </if><if test="address != null"> address=#{address}, </if><if test="userName != null"> user_name=#{userName}, </if><if test="consignee != null"> consignee=#{consignee} ,</if><if test="cancelReason != null"> cancel_reason=#{cancelReason}, </if><if test="rejectionReason != null"> rejection_reason=#{rejectionReason}, </if><if test="cancelTime != null"> cancel_time=#{cancelTime}, </if><if test="estimatedDeliveryTime != null"> estimated_delivery_time=#{estimatedDeliveryTime}, </if><if test="deliveryStatus != null"> delivery_status=#{deliveryStatus}, </if><if test="deliveryTime != null"> delivery_Time=#{deliveryTime}, </if><if test="packAmount != null"> pack_amount=#{packAmount},</if><if test="tablewareNumber != null"> tableware_number=#{tablewareNumber}, </if><if test="tablewareStatus != null"> tableware_status=#{tablewareStatus}, </if></set>where id=#{id}</update>
小白啊!!!寫的不好輕噴啊🤯如果覺得寫的不好,點個贊吧🤪(批評是我寫作的動力)
…。。。。。。。。。。。…
…。。。。。。。。。。。…