点击首页推荐的商品、轮播图商品广告、商品列表页面点击商品,就会进入到商品的详情页面。
一、流程分析
点击推荐商品 ———商品ID———> 商品详情 ————商品ID————> 商品详情
<———商品详情———— 接口
轮播图广告 ————商品ID———> ————商品ID————> 商品详情
<———商品参数———— 参数接口
<———商品评论———— 评论接口
1.获取商品ID
2.根据id查询商品详情(商品基本信息、商品套餐、商品图片、商品参数、商品评价)
3.将商品详情返回给前端
二、接口实现
1.商品详情接口
商品基本信息、商品套餐、商品图片
SQL:
- #根据商品id查询商品基本信息
- SELECT * FROM product WHERE product_id=1;
-
- SELECT * FROM product p
- INNER JOIN product_img i
- INNER JOIN product_sku s
- ON p.product_id=i.item_id
- AND p.product_id=s.product_id
- WHERE p.product_id=3;
-
- SELECT * FROM product p
- INNER JOIN product_img i
- ON p.product_id=i.item_id
- WHERE p.product_id=3;
-
-
- #子查询:根据商品id查询当前商品的套餐
- SELECT * FROM product WHERE product_id=3;
- #根据商品id查询当前商品的图片
- SELECT * FROM product_img WHERE item_id=3;
- #根据商品id查询当前商品的套餐
- SELECT * FROM product_sku WHERE product_id=3;
因为上述子查询的三个查询都是单表查询,所以可以通过tkMapper完成。
三、业务层实现
ProductService
- public interface ProductService {
- public ResultVO listRecommendProducts();
-
- public ResultVO getProductBasicInfo(String productId);
-
- }
ProductServiceImpl
- @Service
- public class ProductServiceImpl implements ProductService {
-
- @Autowired
- private ProductMapper productMapper;
- @Autowired
- private ProductImgMapper productImgMapper;
- @Autowired
- private ProductSkuMapper productSkuMapper;
-
- public ResultVO listRecommendProducts() {
- List
productVOS = productMapper.selectRecommendProducts(); - ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productVOS);
- return resultVO;
- }
-
-
- @Transactional(propagation = Propagation.SUPPORTS)
- public ResultVO getProductBasicInfo(String productId) {
- //1.商品信息
- Example example = new Example(Product.class);
- Example.Criteria criteria = example.createCriteria();
- criteria.andEqualTo("productId",productId);
- criteria.andEqualTo("productStatus",1);//状态为1表示上架商品
- List
products = productMapper.selectByExample(example); - if (products.size()>0){
- //2,商品图片
- Example example1 = new Example(ProductImg.class);
- Example.Criteria criteria1 = example1.createCriteria();
- criteria1.andEqualTo("itemId",productId);
- List
productImgs = productImgMapper.selectByExample(example1); -
- //3.商品套餐
- Example example2 = new Example(ProductSku.class);
- Example.Criteria criteria2 = example2.createCriteria();
- criteria2.andEqualTo("productId",productId);
- criteria2.andEqualTo("status",1);
- List
productSkus = productSkuMapper.selectByExample(example2); -
- HashMap
basicInfo = new HashMap<>(); - basicInfo.put("product",products.get(0));
- basicInfo.put("productImgs",productImgs);
- basicInfo.put("productSkus",productSkus);
-
- return new ResultVO(ResStatus.OK,"success",basicInfo);
- }else {
- return new ResultVO(ResStatus.NO,"查询的商品不存在",null);
- }
-
- }
- }
四、控制层实现
ProductController
- @RestController
- @CrossOrigin
- @RequestMapping("/product")
- @Api(value = "提供商品信息相关的接口",tags = "商品管理")
- public class ProductController {
-
- @Autowired
- private ProductService productService;
-
- @ApiOperation("商品基本信息查询接口")
- @GetMapping("/detail-info/{pid}")
- public ResultVO getProductBasicInfo(@PathVariable("pid") String pid){
- return productService.getProductBasicInfo(pid);
- }
- }
测试: