1.字段与关键字冲突
在实体类添加@TableField注解即可
2.用stream流进行集合赋值
3.分页查询的框架写法
4. 接收路径参数id
5.使用stream流去统计数量
6.关于时间的一些方法
比较时间之前或者之后
获取指定时间之后的时间:
localDateTime可以获取具体到时分秒
7.更新的链式编程
8.当用户下单时连续点击结算怎么办
我们可以有个小技巧,就是用户点击去下单的时候先生成一个预订单id,这一步的id还没插入到数据库,下一步点击结算的时候直接把这个预订单id进行插入订单表,根据主键的唯一性,就算用户连续点击结算也不会产生重复订单。
@Override
public OrderConfirmVO prePlaceOrder(List<Long> courseIds) {
// 1.查询课程信息
List<CourseSimpleInfoDTO> courseInfos = courseClient.getSimpleInfoList(courseIds);
if (CollUtils.isEmpty(courseInfos)) {
throw new BizIllegalException(TradeErrorInfo.COURSE_NOT_EXISTS);
}
List<OrderCourseVO> courses = BeanUtils.copyList(courseInfos, OrderCourseVO.class);
// 2.计算总价
int total = courseInfos.stream().mapToInt(CourseSimpleInfoDTO::getPrice).sum();
// 3.计算折扣
List<OrderCourseDTO> orderCourses = courseInfos.stream()
.map(ci -> new OrderCourseDTO().setId(ci.getId()).setCateId(ci.getThirdCateId()).setPrice(ci.getPrice()))
.collect(Collectors.toList());
List<CouponDiscountDTO> discountSolution = promotionClient.findDiscountSolution(orderCourses);
// 4.生成订单id
long orderId = IdWorker.getId();
// 5.组织返回
OrderConfirmVO vo = new OrderConfirmVO();
vo.setOrderId(orderId);
vo.setTotalAmount(total);
vo.setDiscounts(discountSolution);
vo.setCourses(courses);
return vo;
}
9.使用stream流进行累加的操作
// 2.1.计算订单总价
int totalAmount = orderCourses.stream().mapToInt(OrderCourseDTO::getPrice).sum();
// 2.2.筛选可用券
List<Coupon> availableCoupons = coupons.stream()
.filter(c -> DiscountStrategy.getDiscount(c.getDiscountType()).canUse(totalAmount, c))
.collect(Collectors.toList());
if (CollUtils.isEmpty(availableCoupons)) {
return CollUtils.emptyList();
}
10.如何使用@RequestParam去接收List
11.使用stream流将List转换成map
// 2.初始化折扣明细的映射
Map<Long, Integer> detailMap = courses.stream().collect(Collectors.toMap(OrderCourseDTO::getId, oc -> 0));