开始

E:\学习中\Java架构师体系课:还原千万级项目从0到100全过程,技术&思维双提升-wjw\阶段一:单体电商项目架构,开发与上线(1~5周)\04.用户中心 ,订单\评价管理开发\第3章 用户中心-订单管理功能开发

注意事项。订单表和订单状态表是一对一的,订单表和订单商品信息是一对多的(一个订单可能包含多个商品信息)
image.png

创建自定义mapper

所以mapper里面就包含了嵌套的list。
复制CategoryMapperCustom改名OrdersMapperCustom.xml
image.png
创建接口
image.png

image.png
传入一个map类型
image.png

  1. public interface OrdersMapperCustom {
  2. public List queryMyOrders(@Param("paramMap") Map<String, Object> map);
  3. }

完善sql

mapper的命名空间
image.png

把sql粘过来
image.png

增加根据订单的状态的查询
image.png
image.png

<select id="queryMyOrders" resultMap="myCategoryVO" parameterType="int">
    select
    od.id as orderId,
    od.created_time as createdTime,
    od.pay_method as payMethod,
    od.real_pay_amount as realPayAmout,
    od.post_amount as postAmount,
    os.order_status as orderStatus,
    oi.item_id as itemId,
    oi.item_name as itemName,
    oi.item_img as itemImg,
    oi.item_spec_id as itemSepcId,
    oi.item_spec_name as itemSpecName,
    oi.buy_counts as buyCounts,
    oi.price as price
    from
    orders od
    left join
        order_status os
    on od.id=os.order_id
    left join
        order_items oi
    on od.id=oi.order_id

    where
        od.user_id=#{paramMap.userId}
    and
        od.is_delete=0
        <if test="paramMap.orderStatus!=null">
            and os.order_status=#{paramMap.orderStatus}
        </if>
    order by
    od.updated_time ASC
  </select>

image.png
image.png

<select id="queryMyOrders" resultMap="myOrderVO" parameterType="map">

创建好的vo对象
image.png
在MyOrdersVO里面 添加
image.png

public class MyOrderVO {
    private String orderId;
    private Date createdTime;
    private Integer payMethod;
    private Integer realPayAmount;
    private Integer postAmount;
    private Integer isComment;
    private Integer orderStatus;
    private List<MySubOrderItemVO> subOrderItemList;
}

嵌套的VO ,MySubOrderItemVO
image.png

public class MySubOrderItemVO {
    private String itemId;
    private String itemImg;
    private String itemName;
    private String itemSpecName;
    private Integer buyCounts;
    private Integer price;

}

上面就是我们订单的一些信息
image.png

子对象
image.png

column是查询的sql语句内对应的。property是VO对象内对应的。
image.png

<mapper namespace="com.imooc.mapper.OrdersMapperCustom" >
  <resultMap id="myOrderVO" type="com.imooc.pojo.vo.MyOrderVO">
    <id column="orderId" property="orderId"></id>
    <result column="createdTime" property="createdTime"></result>
    <result column="payMethod" property="payMethod"></result>
    <result column="realPayAmount" property="realPayAmount"></result>
    <result column="postAmount" property="postAmount"></result>
    <result column="isComment" property="isComment"></result>
    <result column="orderStatus" property="orderStatus"></result>
    <collection property="subOrderItemList" ofType="com.imooc.pojo.vo.MySubOrderItemVO">
      <id column="itemId" property="itemId"></id>
      <result column="itemImg" property="itemImg"></result>
      <result column="itemName" property="itemName"></result>
      <result column="itemSpecName" property="itemSpecName"></result>
      <result column="buyCounts" property="buyCounts"></result>
      <result column="price" property="price"></result>
    </collection>
  </resultMap>

对应的类型
image.png

public List<MyOrderVO> queryMyOrders(@Param("paramMap") Map<String, Object> map);

service

image.png
image.png
实现接口
image.png
设计到分页的,所以返回的结果用PagedGridResult
image.png

image.png

public interface MyOrderService {

    /**
     * 查询我的订单列表
     * @param userId
     * @param orderStatus
     * @param page
     * @param pageSize
     * @return
     */
    public PagedGridResult queryMyOrders(String userId,Integer orderStatus,Integer page,Integer pageSize);

}

接口实现

订单状态不为空,才加到map里面
image.png

image.png

image.png
在这里偷懒,没有把些一个BaseService把这个settterPagedGrid放进去。
image.png

public class MyOrderServiceImpl implements MyOrderService {

    @Autowired
    private OrdersMapperCustom ordersMapperCustom;
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryMyOrders(String userId, Integer orderStatus, Integer page, Integer pageSize) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        if(orderStatus!=null) {
            map.put("orderStatus",orderStatus);
        }
        PageHelper.startPage(page,pageSize);
        List<MyOrderVO> list = ordersMapperCustom.queryMyOrders(map);

        return setterPagedGrid(list,page);
    }
    private PagedGridResult setterPagedGrid(List<?> list,Integer page){
        PageInfo<?> pageList=new PageInfo<>(list);
        PagedGridResult grid=new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}

结束