一、业务分析
需要统计的是相同订单id的商品种类、商品数量和以及商品金额。
sql语句:
SELECT order_id,user_id,order_time,COUNT(product_id) product_num,SUM(product_num) order_num,SUM(product_price) order_price FROM orders GROUP BY order_id LIMIT 0,6;
LIMIT 0,6
表示显示1-6条数据
同:
LIMIT 6
上边那条sql执行有问题正常
1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'store_order.orders.user_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
执行下边的
SELECT order_id,user_id,order_time,COUNT(product_id) product_num,SUM(product_num) order_num,SUM(product_price) order_price
FROM orders GROUP BY order_id,user_id,order_time,product_num LIMIT 0,6;
二、定义这个OrderVo实体类做返回的集合数据里的订单实体类
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class AdminOrderVo {
@JsonProperty("order_id")
private Long orderId;
@JsonProperty("user_id")
private Integer userId;
@JsonProperty("product_num")
private Integer productNum; //商品种类
@JsonProperty("order_num")
private Integer orderNum; //订单中商品数量
@JsonProperty("order_price")
private Double orderPrice; //订单金额
@JsonProperty("order_time")
private Long orderTime; //订单时间
}
三、具体实现
常规套路:Admin服务调用order服务的订单展示接口,order先实现订单展示接口。
这里用自己封装的mapper方法。在resource目录下新建OrderMapper文件:
public interface OrderMapper extends BaseMapper<Order> {
/**
* 分页查询数据,返回order封装vo
* @param offset
* @param number
* @return
*/
List<AdminOrderVo> selectAdminOrders(@Param("offset") int offset, @Param("number")int number);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.order.mapper.OrderMapper">
<select id="selectAdminOrders" resultType="com.atguigu.vo.AdminOrderVo">
SELECT order_id,user_id,order_time,COUNT(product_id) product_num,SUM(product_num) order_num,SUM(product_price) order_price FROM orders GROUP BY order_id,user_id,order_time,product_num LIMIT #{offset} , #{number};
</select>
</mapper>
实现类:
@Override
public R adminList(PageParam pageParam) {
//分页数据计算
int offset=(pageParam.getCurrentPage()-1)*pageParam.getPageSize();
int pageSize=pageParam.getPageSize();
List<AdminOrderVo> adminOrderVoList=orderMapper.selectAdminOrder(offset,pageSize);
long total=adminOrderVoList.size();
return R.ok("订单数据查询成功",adminOrderVoList,total);
}
}
将该接口声明在client中,然后admin服务调用OrderClient中定义的接口即可。