Spring Task 定时任务

applicationContext.xml添加注解

<!-- 开启Spring Task 定时任务的注解模式-->
 <task:annotation-driven/>

Spring Task 定时任务 · MingCaiXiong/spring-learn@7f013f9


@Component
public class CompteTask {
    @Resource
    private BookService bookService;

    //任务调度
    @Scheduled(cron = "0 * * * * ?")
    public void updaEvaluation() {
        bookService.updateEvaluationScoreAndQuantity();
        System.out.println(" bookService.updateEvaluationScoreAndQuantity" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

定时汇总sql

 update book as superior
        set evaluation_score    =(
            select ifnull(AVG(score), 0)
            from evaluation
            where book_id = superior.book_id
              and state = 'enable'
        ),
            evaluation_quantity =(
                select ifnull(COUNT(score), 0)
                from evaluation
                where book_id = superior.book_id
                  and state = 'enable'
            )