第1关:查询收藏
任务描述
本关任务:实现查询用户收藏的商品功能。
相关知识
为了完成本关任务,你需要掌握:
- 查询用户收藏的商品;
- 功能实现。
查询用户收藏的商品
如下图是收藏页面的展示:
一般是前端页面通过 ajax 请求服务器,服务器查询数据库后得到 json 数据通过 js 渲染在网页上。
功能实现
- dao 层: CollectDao:查询用户收藏。
//根据用户 id 查询用户的收藏
@Select("select * from collect where user_id=#{user_id}")
List<Collect> getCollect(int user_id);
//根据商品 id 查询商品信息
@Select("select * from product where product_id=#{product_id}")
Product getProductByID(int product_id);
- service 层定义接口: CollectService:
//定义查询收藏的 service 接口
List<Collect> getCollect(int user_id);
//定义根据 Id 查询商品信息的 service 接口
Product getProductByID(int product_id);
- service 层 impl 类: CollectServiceImpl :
@Override
public List<Collect> getCollect(int user_id) {
//调用 dao 层方法,查询用户的收藏
return collectDao.getCollect(user_id);
}
@Override
public Product getProductByID(int product_id) {
//从 redis 中取商品集合 map
Map<Object, Object> productMap = redisTemplate.opsForHash().entries("product");
if(productMap.isEmpty()){
//redis 无 map,从 dao 层查询商品信息存入 map ,map 存入 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无该商品,从 dao 层查询商品信息存入 map ,map 存入 redis
productMap.put(product_id+"",productDao.getProductByID(product_id));
redisTemplate.opsForHash().putAll("product",productMap);
}
//返回 map 集合
return (Product) productMap.get(product_id+"");
}
- controller 层:接收参数,判断用户是否有收藏商品,如果有就返回商品信息。
@RestController
@RequestMapping(value = "/collect")
public class CollectController {
@Autowired
private CollectService collectService;
@Autowired
private ProductService productService;
/**
* 查询收藏
* @param paramMap
* @return
*/
@RequestMapping(value = "/getCollect")
public Map getCollect(@RequestBody Map<String,Integer> paramMap){
//接收参数
Integer user_id = paramMap.get("user_id");
HashMap<String, Object> map = new HashMap<>();
//参数的非空判断
if(user_id==null){
map.put("code","004");
map.put("msg","用户未登录");
}else{
//调用 service 层方法,查询用户的收藏,用户没有收藏返回 "code","002" "msg","该用户没有收藏的商品"
List<Collect> collectList = collectService.getCollect(user_id);
if(collectList==null){
map.put("code","002");
map.put("msg","该用户没有收藏的商品");
}else{
//否则就循环根据收藏的商品 id 查询商品的具体信息
for(Collect collect:collectList){
Product product = productService.getProductByID(collect.getProduct_id());
collect.setProduct_name(product.getProduct_name());
collect.setProduct_picture(product.getProduct_picture());
collect.setProduct_price(product.getProduct_price());
collect.setProduct_selling_price(product.getProduct_selling_price());
collect.setProduct_title(product.getProduct_title());
}
//返回 map 结果集 "code","001" "collectList",collectList
map.put("collectList",collectList);
map.put("code","001");
}
}
return map;
}
- 数据库如下:、 商品表:
收藏表:字段名称 类型 备注 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 '商品状态' 字段名称 类型 备注 id
int(11) NOT NULL AUTO_INCREMENT '收藏表主键id' user_id
int(11) NOT NULL '用户id' product_id
int(11) NOT NULL '商品id' collect_time
datetime NOT NULL '收藏时间'
编程要求
在右侧 CollectDao.java、ProductDao.java、CollectService.java、ProductService.java、CollectServiceImpl.ava、ProductServiceImpl.java、CollectController.java 文件里 Begin-End 处根据提示补充代码。
测试说明
平台会对你编写的代码进行测试:发送请求检验结果集是否正确。
测试输入:
{
"user_id":"1018"
}
预期输出:
{"code":"001","collectList":[{"id":1,"user_id":1018,"product_id":1,"collect_time":"2022-05-25 14:08:29","product_picture":"public/imgs/phone/Redmi-k30.png","product_name":"Redmi K30","product_title":"120Hz流速屏,全速热爱","product_selling_price":1499.00,"product_price":2000.00},{"id":2,"user_id":1018,"product_id":2,"collect_time":"2022-05-25 14:08:39","product_picture":"public/imgs/phone/Redmi-k30-5G.png","product_name":"Redmi K30 5G","product_title":"双模5G,120Hz流速屏","product_selling_price":2599.00,"product_price":2599.00},{"id":3,"user_id":1018,"product_id":3,"collect_time":"2022-05-25 14:08:49","product_picture":"public/imgs/phone/Mi-CC9.png","product_name":"小米CC9 Pro","product_title":"1亿像素,五摄四闪","product_selling_price":2599.00,"product_price":2799.00}]}
开始你的任务吧,祝你成功!
CollectDao.java
package com.www.dao;
import com.www.entity.Collect;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CollectDao {
//根据用户 id 查询用户的收藏
/**********************Begin**********************/
@Select("select * from collect where user_id=#{user_id}")
List<Collect> getCollect(int user_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 {
//根据商品信息查询商品信息
/**********************Begin**********************/
@Select("select * from product where product_id=#{product_id}")
Product getProductByID(int product_id);
/**********************End**********************/
}
CollectService.java
package com.www.service;
import com.www.entity.Collect;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface CollectService {
//定义查询收藏的 service 接口
/**********************Begin**********************/
List<Collect> getCollect(int user_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**********************/
}
CollectServiceImpl.java
package com.www.service.impl;
import com.www.dao.CollectDao;
import com.www.entity.Collect;
import com.www.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("collectService")
public class CollectServiceImpl implements CollectService {
@Autowired
private CollectDao collectDao;
@Override
public List<Collect> getCollect(int user_id) {
//调用 dao 层方法,查询用户的收藏
/**********************Begin**********************/
return collectDao.getCollect(user_id);
/**********************End**********************/
}
}
ProductServiceImpl.java
package com.www.service.impl;
import com.www.dao.ProductDao;
import com.www.entity.Category;
import com.www.entity.Product;
import com.www.entity.Product_picture;
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.List;
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,从 dao 层查询商品信息存入 map ,map 存入 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无该商品,从 dao 层查询商品信息存入 map ,map 存入 redis
productMap.put(product_id+"",productDao.getProductByID(product_id));
redisTemplate.opsForHash().putAll("product",productMap);
}
//返回 map 集合
return (Product) productMap.get(product_id+"");
/**********************End**********************/
}
}
CollectController.java
package com.www.controller;
import com.www.entity.Collect;
import com.www.entity.Product;
import com.www.service.CollectService;
import com.www.service.ProductService;
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.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/collect")
public class CollectController {
@Autowired
private CollectService collectService;
@Autowired
private ProductService productService;
/**
* 查询收藏
* @param paramMap
* @return
*/
@RequestMapping(value = "/getCollect")
public Map getCollect(@RequestBody Map<String,Integer> paramMap){
/**********************Begin**********************/
//接收参数
Integer user_id = paramMap.get("user_id");
HashMap<String, Object> map = new HashMap<>();
//参数的非空判断
if(user_id==null){
map.put("code","004");
map.put("msg","用户未登录");
}else{
//调用 service 层方法,查询用户的收藏,用户没有收藏返回 "code","002" "msg","该用户没有收藏的商品"
List<Collect> collectList = collectService.getCollect(user_id);
if(collectList==null){
map.put("code","002");
map.put("msg","该用户没有收藏的商品");
}else{
//否则就循环根据收藏的商品 id 查询商品的具体信息
for(Collect collect:collectList){
Product product = productService.getProductByID(collect.getProduct_id());
collect.setProduct_name(product.getProduct_name());
collect.setProduct_picture(product.getProduct_picture());
collect.setProduct_price(product.getProduct_price());
collect.setProduct_selling_price(product.getProduct_selling_price());
collect.setProduct_title(product.getProduct_title());
}
//返回 map 结果集 "code","001" "collectList",collectList
map.put("collectList",collectList);
map.put("code","001");
}
}
return map;
/**********************End**********************/
}
}
Collect.java
package com.www.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class Collect implements Serializable {
private int id;
private int user_id;
private int product_id;
private String collect_time;
private String product_picture;
private String product_name;
private String product_title;
private BigDecimal product_selling_price;
private BigDecimal product_price;
}
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;
}
加油哦,同学们!