欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot 较为简单的定时任务

程序员文章站 2022-07-05 21:19:11
创建定时任务类 每一分钟执行一次package com.cgc.platform.job;import com.cgc.platform.service.AllianceDiscountsActivityService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.annotation.EnableScheduling;import org....
  • 创建定时任务类 每一分钟执行一次

@Component
@EnableScheduling
public class AllianceDiscountsActivityJob {
    @Autowired
    private AllianceDiscountsActivityService allianceDiscountsActivityService;

   // @Scheduled(cron = "0 0 0 * * ?")
    @Scheduled(cron = "0 */1 * * * ?")
    public void checkAllianceDiscounts()throws  Exception{
        allianceDiscountsActivityService.checkAllianceDiscounts();
    }
}

  • 创建service

public interface AllianceDiscountsActivityService {

    void checkAllianceDiscounts() throws  Exception;
}

  • 创建serviceImpl
 @Override
    public void checkAllianceDiscounts() throws Exception {
        int num=allianceDiscountsActivityMapper.checkAllianceDiscountActivity();
    }
  • 之后创建mapper mapper mybatis-mapper 用来实现定时任务的方法
public interface AllianceDiscountsActivityMapper {
        int checkAllianceDiscountActivity();
}

 <update id="checkAllianceDiscountActivity" parameterType="java.lang.String">
    update alliance set status=3 WHERE start_time  <![CDATA[<=]]> NOW() and is_del=0 and status=1;
    update alliance set status=2 WHERE end_time  <![CDATA[<=]]> NOW() and is_del=0 and status=3;
  </update>

本文地址:https://blog.csdn.net/weixin_47287824/article/details/107578254