@Update @Select in springBoot is used with timer

package com.ly.liweispringboot.task;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Component
@EnableScheduling // 1. Turn on timed tasks
@EnableAsync // 2. Turn on multithreading
public class MultithreadScheduleTask {

@Mapper
public interface userMapper {
    @Select("select id from t_user where faithful_value < 0")
    public List<Integer> getUSerId();

    @Update({"update t_user set status = 2 where id in (${ids})"})
    public int update(@Param("ids") String ids);
    // TODO 这里要加@Param注解,不然报找不到getter方法,如果是类对象,则不用加
}
@Resource     //注入mapper
userMapper userMapper;

//@Scheduled(fixedDelay = 1000)  //间隔1秒
@Async
@Scheduled(cron = "0 0 1 ? * MON")// 每周一的晚上1点执行
public void first() {
    List<Integer> uSerId = userMapper.getUSerId();
    String ids = "";
    for(int i = 0,len = uSerId.size()-1;i<=len;i++){
        if(i == len) {
            ids += uSerId.get(i);
        }else{
            ids += uSerId.get(i)+",";
        }
    }
    userMapper.update(ids);
    Calendar c = Calendar.getInstance();
    System.out.println(c.get(Calendar.YEAR)
            +"-"+(c.get(Calendar.MONTH)+1)
            +"-"+c.get(Calendar.DAY_OF_MONTH)
            +" "+c.get(Calendar.HOUR_OF_DAY)
            +":"+c.get(Calendar.MINUTE)+" 诚信值更新");
}

// @Async
// @Scheduled(cron = “0/5 * * * * *”)// 每5秒
// public void test() {
// System.out.println(“123”);
// }
}

Guess you like

Origin blog.csdn.net/qq_26634873/article/details/110413449