使用PageHelper分页嵌套查询结果映射导致的BUG

image.png
image.png
image.png
我们在查询订单的时候是按规格查询出来,最后根据orderId合并成一个完整的订单,也就是我们查询的是规格,之后嵌套进了订单中,而我们在显示的时候应该显示的是订单条数,每页也应该根据订单来显示10条订单数据,但现在PageHelper却是以规格数来进行统计。
解决方案:
一:先使用订单ID查询出所有订单,再对订单中的每一条商品信息进行查询。
二:PageHelper内置了方法来解决

  1. <resultMap id="myOrdersVO" type="com.shiers.pojo.vo.MyOrdersVO">
  2. <id column="orderId" property="orderId"/>
  3. <result column="createdTime" property="createdTime"/>
  4. <result column="payMethod" property="payMethod"/>
  5. <result column="realPayAmount" property="realPayAmount"/>
  6. <result column="postAmount" property="postAmount"/>
  7. <result column="orderStatus" property="orderStatus"/>
  8. <result column="isComment" property="isComment"/>
  9. ================================👇👇👇嵌套👇👇👇================================
  10. <collection property="subOrderItemList"
  11. =======👇👇👇新增加的解决方案👇👇👇=======
  12. select="getSubItems" 👈这里对这个对象属性进行查询,定义查询方法名
  13. column="orderId" 👈传入参数
  14. =======👆👆👆新增加的解决方案👆👆👆=======
  15. ofType="com.shiers.pojo.vo.MySubOrderItemVO">
  16. <result column="itemId" property="itemId"/>
  17. <result column="itemName" property="itemName"/>
  18. <result column="itemImg" property="itemImg"/>
  19. <result column="itemSpecName" property="itemSpecName"/>
  20. <result column="buyCounts" property="buyCounts"/>
  21. <result column="price" property="price"/>
  22. </collection>
  23. ================================👆👆👆嵌套👆👆👆================================
  24. </resultMap>
  25. ==================👇👇👇SQL将不包含规格表,只查询了订单👇👇👇====================
  26. <select id="queryMyOrders" resultMap="myOrdersVO" parameterType="Map">
  27. SELECT
  28. od.id AS orderId,
  29. od.created_time AS createdTime,
  30. od.pay_method AS payMethod,
  31. od.real_pay_amount AS realPayAmount,
  32. od.post_amount AS orderStatus,
  33. os.order_status AS orderStatus
  34. FROM
  35. orders od
  36. LEFT JOIN
  37. order_status os
  38. ON
  39. od.id = os.order_id
  40. WHERE
  41. od.user_id = #{paramsMap.userId}
  42. AND
  43. od.is_delete = 0
  44. <if test="paramsMap.orderStatus != null">
  45. AND os.order_status = #{paramsMap.orderStatus}
  46. </if>
  47. ORDER BY
  48. od.updated_time ASC
  49. </select>
  50. =============👇👇👇根据订单表的id查询对应规格表,填充到属性中👇👇👇==============
  51. <select id="getSubItems" parameterType="String" resultType="com.shiers.pojo.vo.MySubOrderItemVO">
  52. select
  53. oi.item_id AS itemId,
  54. oi.item_name AS itemName,
  55. oi.item_img AS itemImg,
  56. oi.item_spec_name AS itemSpecName,
  57. oi.buy_counts AS buyCounts,
  58. oi.price AS price
  59. from order_items oi
  60. where oi.order_id = #{orderId}
  61. </select>

Controller

  1. package com.shiers.controller.center;
  2. import com.shiers.controller.BaseController;
  3. import com.shiers.pojo.Orders;
  4. import com.shiers.service.center.MyOrdersService;
  5. import com.shiers.utils.MyJSONResult;
  6. import com.shiers.utils.PagedGridResult;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import io.swagger.annotations.ApiParam;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.web.bind.annotation.*;
  14. /**
  15. * Demo class
  16. *
  17. * @author shierS
  18. * @date 2021/6/6
  19. */
  20. @Api(value = "用户中心我的订单", tags = {"用户中心我的订单相关接口", "巴拉巴拉"})
  21. @RestController
  22. @RequestMapping("myorders")
  23. public class MyOrdersController extends BaseController {
  24. @Autowired
  25. private MyOrdersService myOrdersService;
  26. @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
  27. @PostMapping("query")
  28. public MyJSONResult query(
  29. @ApiParam(name = "userId", value = "用户id", required = true)
  30. @RequestParam String userId,
  31. @ApiParam(name = "orderStatus", value = "订单状态", required = false)
  32. @RequestParam Integer orderStatus,
  33. @ApiParam(name = "page", value = "查询第几页", required = false)
  34. @RequestParam Integer page,
  35. @ApiParam(name = "pageSize", value = "分页的每一页显示条数", required = false)
  36. @RequestParam Integer pageSize) {
  37. System.out.println("xxxx");
  38. if (StringUtils.isBlank(userId)) {
  39. return MyJSONResult.errorMsg(null);
  40. }
  41. if (page == null) {
  42. page = 1;
  43. }
  44. if (pageSize == null) {
  45. pageSize = COMMEN_PAGE_SIZE;
  46. }
  47. PagedGridResult grid = myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize);
  48. return MyJSONResult.ok(grid);
  49. }
  50. // 商家发货没有后端,所以这个接口仅仅只是用于模拟
  51. @ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "GET")
  52. @GetMapping("/deliver")
  53. public MyJSONResult deliver(
  54. @ApiParam(name = "orderId", value = "订单id", required = true)
  55. @RequestParam String orderId) throws Exception {
  56. if (StringUtils.isBlank(orderId)) {
  57. return MyJSONResult.errorMsg("订单ID不能为空");
  58. }
  59. myOrdersService.updateDeliverOrderStatus(orderId);
  60. return MyJSONResult.ok();
  61. }
  62. @ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST")
  63. @PostMapping("/confirmReceive")
  64. public MyJSONResult confirmReceive(
  65. @ApiParam(name = "orderId", value = "订单id", required = true)
  66. @RequestParam String orderId,
  67. @ApiParam(name = "userId", value = "用户id", required = true)
  68. @RequestParam String userId) throws Exception {
  69. MyJSONResult checkResult = checkUserOrder(userId, orderId);
  70. if (checkResult.getStatus() != HttpStatus.OK.value()) {
  71. return checkResult;
  72. }
  73. boolean res = myOrdersService.updateReceiveOrderStatus(orderId);
  74. if (!res) {
  75. return MyJSONResult.errorMsg("订单确认收货失败!");
  76. }
  77. return MyJSONResult.ok();
  78. }
  79. @ApiOperation(value = "用户删除订单", notes = "用户删除订单", httpMethod = "POST")
  80. @PostMapping("/delete")
  81. public MyJSONResult delete(
  82. @ApiParam(name = "orderId", value = "订单id", required = true)
  83. @RequestParam String orderId,
  84. @ApiParam(name = "userId", value = "用户id", required = true)
  85. @RequestParam String userId) throws Exception {
  86. MyJSONResult checkResult = checkUserOrder(userId, orderId);
  87. if (checkResult.getStatus() != HttpStatus.OK.value()) {
  88. return checkResult;
  89. }
  90. boolean res = myOrdersService.deleteOrder(userId, orderId);
  91. if (!res) {
  92. return MyJSONResult.errorMsg("订单删除失败!");
  93. }
  94. return MyJSONResult.ok();
  95. }
  96. /**
  97. * 用于验证用户和订单是否有关联关系,避免非法用户调用
  98. *
  99. * @return
  100. */
  101. private MyJSONResult checkUserOrder(String userId, String orderId) {
  102. Orders order = myOrdersService.queryMyOrder(userId, orderId);
  103. if (order == null) {
  104. return MyJSONResult.errorMsg("订单不存在!");
  105. }
  106. return MyJSONResult.ok();
  107. }
  108. }

Service

  1. package com.shiers.service.center;
  2. import com.shiers.pojo.Orders;
  3. import com.shiers.utils.PagedGridResult;
  4. /**
  5. * Demo class
  6. *
  7. * @author shierS
  8. * @date 2021/6/6
  9. */
  10. public interface MyOrdersService {
  11. /**
  12. * 查询我的订单列表
  13. * @param userId
  14. * @param orderStatus
  15. * @param page
  16. * @param pageSize
  17. * @return
  18. */
  19. public PagedGridResult queryMyOrders(String userId,
  20. Integer orderStatus,
  21. Integer page,
  22. Integer pageSize);
  23. /**
  24. * @Description: 订单状态 --> 商家发货
  25. */
  26. public void updateDeliverOrderStatus(String orderId);
  27. /**
  28. * 查询我的订单,用于用户确认订单时,判断用户id和订单id是否相互关联
  29. *
  30. * @param userId
  31. * @param orderId
  32. * @return
  33. */
  34. public Orders queryMyOrder(String userId, String orderId);
  35. /**
  36. * 更新订单状态 —> 确认收货
  37. *
  38. * @return
  39. */
  40. public boolean updateReceiveOrderStatus(String orderId);
  41. /**
  42. * 删除订单(逻辑删除)
  43. * @param userId
  44. * @param orderId
  45. * @return
  46. */
  47. public boolean deleteOrder(String userId, String orderId);
  48. }

ServiceImpl

  1. package com.shiers.service.impl.center;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.shiers.enums.OrderStatusEnum;
  5. import com.shiers.enums.YesOrNo;
  6. import com.shiers.mapper.OrderStatusMapper;
  7. import com.shiers.mapper.OrdersMapper;
  8. import com.shiers.mapper.OrdersMapperCustom;
  9. import com.shiers.pojo.OrderStatus;
  10. import com.shiers.pojo.Orders;
  11. import com.shiers.pojo.vo.MyOrdersVO;
  12. import com.shiers.service.center.MyOrdersService;
  13. import com.shiers.utils.PagedGridResult;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Propagation;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import tk.mybatis.mapper.entity.Example;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * Demo class
  25. *
  26. * @author shierS
  27. * @date 2021/6/6
  28. */
  29. @Service
  30. public class MyOrdersServiceImpl implements MyOrdersService {
  31. @Autowired
  32. private OrdersMapperCustom ordersMapperCustom;
  33. @Autowired
  34. private OrderStatusMapper orderStatusMapper;
  35. @Autowired
  36. private OrdersMapper ordersMapper;
  37. @Transactional(propagation = Propagation.SUPPORTS)
  38. @Override
  39. public PagedGridResult queryMyOrders(String userId, Integer orderStatus, Integer page, Integer pageSize) {
  40. Map<String, Object> map = new HashMap<>();
  41. map.put("userId", userId);
  42. if(orderStatus != null){
  43. map.put("orderStatus",orderStatus);
  44. }
  45. PageHelper.startPage(page,pageSize);
  46. List<MyOrdersVO> list = ordersMapperCustom.queryMyOrders(map);
  47. return setterPagedGrid(list,page);
  48. }
  49. private PagedGridResult setterPagedGrid(List<?> list,Integer page){
  50. PageInfo<?> pageList = new PageInfo<>(list);
  51. PagedGridResult grid = new PagedGridResult();
  52. grid.setPage(page);
  53. grid.setRows(list);
  54. grid.setTotal(pageList.getPages());
  55. grid.setRecords(pageList.getTotal());
  56. return grid;
  57. }
  58. @Transactional(propagation=Propagation.REQUIRED)
  59. @Override
  60. public void updateDeliverOrderStatus(String orderId) {
  61. OrderStatus updateOrder = new OrderStatus();
  62. updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
  63. updateOrder.setDeliverTime(new Date());
  64. Example example = new Example(OrderStatus.class);
  65. Example.Criteria criteria = example.createCriteria();
  66. criteria.andEqualTo("orderId", orderId);
  67. criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
  68. System.out.println(orderId);
  69. orderStatusMapper.updateByExampleSelective(updateOrder, example);
  70. }
  71. @Transactional(propagation=Propagation.SUPPORTS)
  72. @Override
  73. public Orders queryMyOrder(String userId, String orderId) {
  74. Orders orders = new Orders();
  75. orders.setUserId(userId);
  76. orders.setId(orderId);
  77. orders.setIsDelete(YesOrNo.NO.type);
  78. return ordersMapper.selectOne(orders);
  79. }
  80. @Transactional(propagation=Propagation.REQUIRED)
  81. @Override
  82. public boolean updateReceiveOrderStatus(String orderId) {
  83. OrderStatus updateOrder = new OrderStatus();
  84. updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
  85. updateOrder.setSuccessTime(new Date());
  86. Example example = new Example(OrderStatus.class);
  87. Example.Criteria criteria = example.createCriteria();
  88. criteria.andEqualTo("orderId", orderId);
  89. criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
  90. int result = orderStatusMapper.updateByExampleSelective(updateOrder, example);
  91. return result == 1 ? true : false;
  92. }
  93. @Transactional(propagation=Propagation.REQUIRED)
  94. @Override
  95. public boolean deleteOrder(String userId, String orderId) {
  96. Orders updateOrder = new Orders();
  97. updateOrder.setIsDelete(YesOrNo.YES.type);
  98. updateOrder.setUpdatedTime(new Date());
  99. Example example = new Example(Orders.class);
  100. Example.Criteria criteria = example.createCriteria();
  101. criteria.andEqualTo("id", orderId);
  102. criteria.andEqualTo("userId", userId);
  103. int result = ordersMapper.updateByExampleSelective(updateOrder, example);
  104. return result == 1 ? true : false;
  105. }
  106. }

补充:

由于没有商城后台管理,商家发货功能只是开发了接口,使用GET方法,可在浏览器直接调用,或者使用Swagger2进行调试更改订单状态