springboot中使用Task定时任务非常简单
springboot 中自带的都有注解不需要引入依赖
第一步:在启动类上添加启用定时任务注解
@EnableScheduling //开启任务调度
第二步:创建一个springboot组件用于定时任务管理
package cn.lsy.api.Task;
import cn.lsy.api.Entity.TimeoutOrder;
import cn.lsy.api.Service.TimeoutService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/*
 * @ 定时任务模块
 * @author lsy~
 * @version 1.0
 */
@Component
public class OrderTask {
    @Resource
    private TimeoutService timeoutService;
    @Scheduled(cron = "0/5 * * * * ?")
    public void executeTask() {
        QueryWrapper<TimeoutOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "1");
        wrapper.lt("createtime", LocalDateTime.now().minusMinutes(15));
        List<TimeoutOrder> list = timeoutService.list(wrapper);
        if (list != null && list.size() > 0) {
            for (TimeoutOrder t : list) {
                t.setStatus(2);
                timeoutService.updateById(t);
            }
        }
    }
}
 
第三步:掌握cron表达式
自动生成cron表达式网站:Cron - 在线Cron表达式生成器 (ciding.cc)
 

![[C++][设计模式][备忘录模式]详细讲解](https://img-blog.csdnimg.cn/direct/8b380a9587904a08a6a749765c07af34.png)

















