需求:在当天晚上12点定时将过期数据的状态置为冻结状态
- 版本一代码实现[相当于是一个死代码,因为不能自动调度]
public void updateStatus() throws CommonException, ParseException {String date_str = "2023-07-01 00:00:00";Date expireTime = DateUtils.toDate(date_str);if(userCardMapper.countExpiredDate(userCardPageReqList,expireTime)<=0){throw ExceptionUtils.create(CenterErrorEnum.PROJECT_CODE_NOT_BLANK);}List<UserCard> userCards = userCardMapper.queryExpiredDate(expireTime);userCards = userCards.stream().map(userCard-> {userCard.setEquityUseStatus(EquityUseStatus.FREEZE_06);return userCard;}).collect(Collectors.toList());userCardMapper.updateStatus(userCards);}
mybatis的查询
<select id="countExpiredDate" resultType="java.lang.Integer">select count(*) from table<where>del_flag = 0<if test="expireTime != null">and expire_time <= #{expireTime,VARCHAR=TIMESTAMP}</if></where></select><select id="queryExpiredDate" resultType="UserCard">select * from table2<where>del_flag = 0<if test="expireTime != null">and expire_time <= #{expireTime,VARCHAR=TIMESTAMP}</if></where></select>
- 版本二的更新
问题点:不能写死时间
采用XxlJob任务调度中心调度
首先设置时间,使用cron表达式设置定时时间
然后设置过期任务
@XxlJob("updateStatus")public ReturnT<String> updateStatus(String param) {log.info("===开始===");baseMapper.updateStatus();log.info("===结束===");return ReturnT.SUCCESS;}
<update id="updateStatus" >update tablesetstatus = '06' where status in ('00','01') expire_time <= #{expireTime,VARCHAR=TIMESTAMP}</update>
扩充知识点
- Java日期操作
格式转换器:SimpleDateFormate
SimpleDateFormat format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm E");
Date now=new Date();
System.out.println(format1.format(now));out: 2022/10/12 14:28 星期三
String 转Date
String date_str = "2023-07-01 00:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date expireTime = simpleDateFormat.parse(date_str);
- Java实现定时任务[Timer类实现该方法]
https://blog.51cto.com/u_14152/6305966
- mybatis大于小于等格式
https://blog.csdn.net/p1830095583/article/details/116019413
4. sql查询日期
https://blog.csdn.net/qq_37075841/article/details/123117720
<select id="method" resultMap="BaseResultMap">select * from table_awhere date_format(create_time,'%Y-%m-%d') = #{createTime}
</select>
- java.util.Date 和 java.sql.Date区别
https://blog.csdn.net/Yuan_Hang723/article/details/109688772
java.util.Date通常情况下获取日期
java.sql.Date针对sql,只能获取日期,不能获取时间
- cron表达式
https://blog.csdn.net/weixin_42408447/article/details/117422084