第1关:订单查询
任务描述
本关任务:实现订单查询功能。
相关知识
为了完成本关任务,你需要掌握:
- 订单查询;
- 功能实现。
订单查询
在一个购物网站中,用户可以对自己的订单进行查询,如下图:
功能实现
-
dao 层查询数据库: OrderDao 查询订单编号及订单内容:
//根据用户 id 查询订单编号,distinct 消除重复的行内容
@Select("select distinct order_id from orders where user_id=#{user_id} order by order_id desc")
List<Long> getOrderIdByUser(int user_id);
//根据订单编号查询订单内容
@Select("select * from orders where order_id=#{order_id}")
List<Order> getOrders(Long order_id);
ProductDao 查询商品信息:
//根据商品 id 查询商品内容
@Select("select * from product where product_id=#{product_id}")
Product getProductByID(int product_id);
-
service 层: OrderService 定义接口:
//定义 service 查询订单编号接口
List<Long> getOrderIdByUser(int user_id);
//定义 service 查询订单接口
List<Order> getOrders(Long order_id);
ProductService 定义接口:
//定义根据商品 id 查询具体商品信息 service 接口
Product getProductByID(int product_id);
-
impl 实现类: OrderServiceImpl:
@Override
public List<Long> getOrderIdByUser(int user_id) {
//调用 dao 层方法,根据用户 id 查询该用户的所有订单编号并返回结果集
return orderDao.getOrderIdByUser(user_id);
}
@Override
public List<Order> getOrders(Long order_id) {
//调用 dao 层方法,根据订单 id 查询该订单的所有内容
return orderDao.getOrders(order_id);
}
ProductServiceImpl:
@Override
public Product getProductByID(int product_id) {
//先从 redis 中取商品集合的 map 集合
Map<Object, Object> productMap = redisTemplate.opsForHash().entries("product");
if(productMap.isEmpty()){
//redis 中无 map,productMap 存入 redis
Product product = productDao.getProductByID(product_id);
productMap.put(product_id+"",product);
redisTemplate.opsForHash().putAll("product",productMap);
}else if(productMap.get(product_id)==null){
//redis中的 map 无该商品,去数据库中查询商品 id 为 product_id 的商品并存入 map 集合和 redis 缓存
productMap.put(product_id+"",productDao.getProductByID(product_id));
redisTemplate.opsForHash().putAll("product",productMap);
}
//取出 productMap 中商品 id 为 product_id 的商品
return (Product) productMap.get(product_id+"");
}
-
controller:
@RequestMapping(value = "/getOrder")
public Map getOrder(@RequestBody Map<String,Object> paramMap){
//获取用户 id
Integer user_id = Integer.parseInt(String.valueOf(paramMap.get("user_id")));
//初始化 map 集合
HashMap<String, Object> map = new HashMap();
//获取用户有的所有订单 id
List<Long> Order_Ids = orderService.getOrderIdByUser(user_id);
if(Order_Ids==null){
return RespBeanEnum.FIND_ORDER_ERROR.getMap();
}else{
List<List<Order>> ordersList = new ArrayList<>(); //需要返回前端的list
//循环所有订单 id
for(Long order_id:Order_Ids){
List<Order> tempList = new ArrayList<>();
//查询订单所有订单
List<Order> orders = orderService.getOrders(order_id); //同一笔订单的商品
//循环所有订单
for(Order order:orders){
//查询订单里的商品
Product product = productService.getProductByID(order.getProduct_id());
//该订单商品的数量
order.setProduct_name(product.getProduct_name());
//该订单商品图片
order.setProduct_picture(product.getProduct_picture());
//该订单的商品信息存入订单集合
tempList.add(order);
}
ordersList.add(tempList);
}
map.put("orders",ordersList);
map.put("code","001");
}
return map;
}
-
数据库结构: 订单表:
字段名称 类型 备注 id
int(11) NOT NULL AUTO_INCREMENT '购物车id' user_id
int(11) NOT NULL '用户id' product_id
int(11) NOT NULL '商品id' product_num
int(11) NOT NULL '商品数量' product_price
decimal(10,0) NOT NULL '商品价格' order_time
datetime NOT NULL '订单创建时间' order_id
bigint(20) NOT NULL '订单编号' pay_time
datetime NOT NULL '付款时间' pay_status
int(2) NOT NULL '0为未付款,1为已付款' status
int(11) NOT NULL '0为失效,1为生效' 商品表:
字段名称 类型 备注 product_id
int(11) NOT NULL AUTO_INCREMENT '商品id' product_name
char(100) NOT NULL '商品名' category_id
int(11) NOT NULL '类型id' product_title
char(30) NOT NULL '商品标题' product_intro
text NOT NULL '商品描述' product_picture
char(200) DEFAULT NULL '商品图片' product_price
decimal(10,2) NOT NULL '商品价格' product_selling_price
decimal(10,2) NOT NULL '商品售价' product_num
int(11) NOT NULL '商品存量' product_sales
int(11) NOT NULL '商品销售数量' status
int(11) DEFAULT NULL '商品状态'
编程要求
在 OrderDao.java、ProductDao.java、OrderService.java、ProductService.java、OrderServiceImpl.java、ProductServiceImpl.java、OrderController.java 文件里 Begin-End 中根据提示补充代码。
测试说明
平台会对你编写的代码进行测试:发送请求检验结果集是否正确。
测试输入:
{
"user_id":"1018"
}
预期输出:
{"code":"001","orders":[[{"id":1,"order_id":1631456416541,"product_num":2,"product_id":1,"product_price":35,"order_time":"2022-05-24T02:55:55.000+00:00","pay_time":"2022-05-03T02:56:20.000+00:00","status":0,"product_name":"Redmi K30","product_picture":"public/imgs/phone/Redmi-k30.png"},{"id":2,"order_id":1631456416541,"product_num":3,"product_id":10,"product_price":44,"order_time":"2022-05-24T03:08:10.000+00:00","pay_time":"2022-05-24T03:08:15.000+00:00","status":0,"product_name":"小米全面屏电视E55A","product_picture":"public/imgs/appliance/MiTv-E55A.png"},{"id":3,"order_id":1631456416541,"product_num":10,"product_id":15,"product_price":77,"order_time":"2022-05-24T03:08:38.000+00:00","pay_time":"2022-05-24T03:08:43.000+00:00","status":0,"product_name":"小米壁画电视 65英寸","product_picture":"public/imgs/appliance/MiTv-ArtTv-65.png"}],[{"id":4,"order_id":1631456416489,"product_num":15,"product_id":20,"product_price":130,"order_time":"2022-05-24T03:09:42.000+00:00","pay_time":"2022-05-24T03:09:58.000+00:00","status":1,"product_name":"小米9 ARE U OK保护壳","product_picture":"public/imgs/accessory/protectingShell-Mi-9.png"}]]}
开始你的任务吧,祝你成功!
OrderDao.java
package com.www.dao;
import com.www.entity.Order;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface OrderDao {
//根据用户 id 查询订单编号,distinct 消除重复的行内容
/********************Begin********************/
@Select("select distinct order_id from orders where user_id=#{user_id} order by order_id desc")
List<Long> getOrderIdByUser(int user_id);
/********************End********************/
//根据订单编号查询订单内容
/********************Begin********************/
@Select("select * from orders where order_id=#{order_id}")
List<Order> getOrders(Long order_id);
/********************End********************/
}
ProductDao.java
package com.www.dao;
import com.www.entity.Product;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductDao {
//根据商品 id 查询商品内容
/********************Begin********************/
@Select("select * from product where product_id=#{product_id}")
Product getProductByID(int product_id);
/********************End********************/
}
OrderService.java
package com.www.service;
import com.www.entity.Order;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface OrderService {
//定义 service 查询订单编号接口
/********************Begin********************/
List<Long> getOrderIdByUser(int user_id);
/********************End********************/
//定义 service 查询订单接口
/********************Begin********************/
List<Order> getOrders(Long order_id);
/********************End********************/
}
ProductService.java
package com.www.service;
import com.www.entity.Product;
import org.springframework.stereotype.Component;
@Component
public interface ProductService {
//定义根据商品 id 查询具体商品信息 service 接口
/********************Begin********************/
Product getProductByID(int product_id);
/********************End********************/
}
OrderServiceImpl.java
package com.www.service.impl;
import com.www.dao.OrderDao;
import com.www.entity.Order;
import com.www.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("orderService")
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public List<Long> getOrderIdByUser(int user_id) {
//调用 dao 层方法,根据用户 id 查询该用户的所有订单编号并返回结果集
/********************Begin********************/
return orderDao.getOrderIdByUser(user_id);
/********************End********************/
}
@Override
public List<Order> getOrders(Long order_id) {
//调用 dao 层方法,根据订单 id 查询该订单的所有内容
/********************Begin********************/
return orderDao.getOrders(order_id);
/********************End********************/
}
}
ProductServiceImpl.java
package com.www.service.impl;
import com.www.dao.ProductDao;
import com.www.entity.Product;
import com.www.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Override
public Product getProductByID(int product_id) {
/********************Begin********************/
//先从 redis 中取商品集合的 map 集合
Map<Object, Object> productMap = redisTemplate.opsForHash().entries("product");
if(productMap.isEmpty()){
//redis 中无 map,productMap 存入 redis
Product product = productDao.getProductByID(product_id);
productMap.put(product_id+"",product);
redisTemplate.opsForHash().putAll("product",productMap);
}else if(productMap.get(product_id)==null){
//redis中的 map 无该商品,去数据库中查询商品 id 为 product_id 的商品并存入 map 集合和 redis 缓存
productMap.put(product_id+"",productDao.getProductByID(product_id));
redisTemplate.opsForHash().putAll("product",productMap);
}
//取出 productMap 中商品 id 为 product_id 的商品
return (Product) productMap.get(product_id+"");
/********************End********************/
}
}
OrderController.java
package com.www.controller;
import com.www.entity.Order;
import com.www.entity.Product;
import com.www.service.OrderService;
import com.www.service.ProductService;
import com.www.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/order")
public class OrderController {
@Autowired
private ProductService productService;
@Autowired
private OrderService orderService;
/**
* 查询订单
* @param paramMap
* @return
*/
@RequestMapping(value = "/getOrder")
public Map getOrder(@RequestBody Map<String,Object> paramMap){
/********************Begin********************/
//获取用户 id
Integer user_id = Integer.parseInt(String.valueOf(paramMap.get("user_id")));
//初始化 map 集合
HashMap<String, Object> map = new HashMap();
//获取用户有的所有订单 id
List<Long> Order_Ids = orderService.getOrderIdByUser(user_id);
if(Order_Ids==null){
return RespBeanEnum.FIND_ORDER_ERROR.getMap();
}else{
List<List<Order>> ordersList = new ArrayList<>(); //需要返回前端的list
//循环所有订单 id
for(Long order_id:Order_Ids){
List<Order> tempList = new ArrayList<>();
//查询订单所有订单
List<Order> orders = orderService.getOrders(order_id); //同一笔订单的商品
//循环所有订单
for(Order order:orders){
//查询订单里的商品
Product product = productService.getProductByID(order.getProduct_id());
//该订单商品的数量
order.setProduct_name(product.getProduct_name());
//该订单商品图片
order.setProduct_picture(product.getProduct_picture());
//该订单的商品信息存入订单集合
tempList.add(order);
}
ordersList.add(tempList);
}
map.put("orders",ordersList);
map.put("code","001");
}
return map;
/********************End********************/
}
}
Order.java
package com.www.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class Order implements Serializable {
private Long id;
private Long order_id;
private int product_num;
private int product_id;
private BigDecimal product_price;
private Date order_time;
private Date pay_time;
private int status; //0,失效;1,生效
private String product_name;
private String product_picture;
}
Product.java
package com.www.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class Product implements Serializable {
private int product_id;
private String product_name;
private int category_id;
private String product_title;
private String product_intro;
private String product_picture;
private BigDecimal product_price; //原价
private BigDecimal product_selling_price; //售价
private int product_num;
private int product_sales;
private int status;
}
加油哦,同学们!