1.写定时器
1.1为什么要写一个定时器呢?
因为优惠券包括系统优惠券 和 用户优惠券
超过过期时间后就没啥卵用,所以就检测过期的优惠券,有过期的话就把他删除了
1.2代码编写
@Component
public class CouponExpiryScheduler {
@Resource
private CouponsService couponsService;
@Resource
private CouponsDao couponsDao;
@Resource
private UserCouponsDao userCouponsDao;
//每天检查一次 若优惠券过期自动删除
//每天凌晨00:00执行一次
@Scheduled(cron = "0 0 0 * * ?")
public void deleteExpiredCoupons() {
//获取过期的优惠券
List<CouponsVo> couponsVos = couponsService.queryExpiredCoupons();
if (!couponsVos.isEmpty()) {
//使用stream流抽取id
List<Integer> idsToDelete = couponsVos.stream()
.map(CouponsVo::getId)
.collect(Collectors.toList());
int i = couponsDao.deleteBatchIds(idsToDelete);
for (Integer integer : idsToDelete) {
QueryWrapper queryWrapper = new QueryWrapper<>().eq("coupon_id", integer);
int n = userCouponsDao.delete(queryWrapper);
if (n != 0) {
System.out.println("删除" + n + "条用户优惠券按数据成功");
}
if (i != 0) {
System.out.println("删除" + i + "条优惠券成功");
}
}
}
}
}
另外要在spring boot启动类中加入 EnableSchduling注解
2.关于我使用PostMapping进行添加时报错参数找不到
我的sql语句是这样写的
<insert id="insert">
INSERT INTO user_coupons(user_id, coupon_id)
VALUES (#{userId}, #{couponId})
</insert>
我想着我使用@RequestParam注解 直接传userId 和 couponId 就可以了
谁知道给我报这个错误 参数找不到
org.apache.ibatis.binding.BindingException: Parameter 'couponId' not found. Available parameters are [arg1, arg0, param1, param2]
我的理解就是请求体的问题 post 一般把数据都封装到请求体里面了 ,参数不裸露出来
然后xml语句 再从请求体中解析