springBoot中@Update @Select和定时器一起使用

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.开启定时任务
@EnableAsync // 2.开启多线程
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”);
// }
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页