Spring Boot点餐系统实战(5)–买家商品API(下)

avatar 2017年11月12日20:54:50 7 3519 views
博主分享免费Java教学视频,B站账号:Java刘哥
在上一篇《Spring Boot点餐系统实战(4)–买家商品API(上)》中已经为买家前台商品页面所需要的JSON数据进行了封装,最后还差 Controller 了中从数据库中查询数据进行数据拼接,再返回给前台了。  

本文新关键词

lambda 表达式 冗余度  

一、基本准备

1、数据库内容 product_category 表 product_info 表   2、最终的文件结构  

二、Controller 层的修改

BuyerProductController.java
  1. package com.liuyanzhao.sell.controller;
  2. import com.liuyanzhao.sell.VO.ProductInfoVO;
  3. import com.liuyanzhao.sell.VO.ProductVO;
  4. import com.liuyanzhao.sell.VO.ResultVO;
  5. import com.liuyanzhao.sell.entity.ProductCategory;
  6. import com.liuyanzhao.sell.entity.ProductInfo;
  7. import com.liuyanzhao.sell.service.CategoryService;
  8. import com.liuyanzhao.sell.service.ProductService;
  9. import com.liuyanzhao.sell.utils.ResultVOUtil;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.stream.Collectors;
  18. /**
  19.  * @Author: 言曌
  20.  * @Date: 2017/11/12
  21.  * @Time: 下午12:13
  22.  */
  23. @RestController
  24. @RequestMapping("/buyer/product")
  25. public class BuyerProductController {
  26.     @Autowired
  27.     private ProductService productService;
  28.     @Autowired
  29.     private CategoryService categoryService;
  30.     @GetMapping("/list")
  31.     public ResultVO list() {
  32.         //1、查询所有的上架商品
  33.         List<ProductInfo> productInfoList = productService.findUpAll();
  34.         //2、查询类目(一次性查询)
  35.         //传统方法
  36. //        List<Integer> categoryTypeList = new ArrayList<>();
  37. //        for(ProductInfo productInfo:productInfoList) {
  38. //            categoryTypeList.add(productInfo.getCategoryType());
  39. //        }
  40.         //精简做法(java8,lambda)
  41.         List<Integer> categoryTypeList = productInfoList.stream()
  42.                 .map(e -> e.getCategoryType())
  43.                 .collect(Collectors.toList());
  44.         List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
  45.         //3、数据拼接
  46.         List<ProductVO> productVOList = new ArrayList<>();
  47.         for(ProductCategory productCategory:productCategoryList) {
  48.             ProductVO productVO = new ProductVO();
  49.             productVO.setCategoryName(productCategory.getCategoryName());
  50.             productVO.setCategoryType(productCategory.getCategoryType());
  51.             List<ProductInfoVO> productInfoVOList = new ArrayList<>();
  52.             for(ProductInfo productInfo:productInfoList) {
  53.                 if(productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
  54.                     //传统方法
  55. //                    ProductInfoVO productInfoVO = new ProductInfoVO();
  56. //                    productInfoVO.setProductId(productInfo.getProductId());
  57. //                    productInfoVO.setProductName(productInfo.getProductName());
  58. //                    productInfoVO.setProductPrice(productInfo.getProductPrice());
  59. //                    productInfoVO.setProductDescription(productInfo.getProductDescription());
  60. //                    productInfoVO.setProductIcon(productInfo.getProductIcon());
  61.                     //精简方法
  62.                     ProductInfoVO productInfoVO = new ProductInfoVO();
  63.                     BeanUtils.copyProperties(productInfo,productInfoVO);//从前者拷到后者中
  64.                     productInfoVOList.add(productInfoVO);
  65.                 }
  66.             }
  67.             productVO.setProductInfoVOList(productInfoVOList);
  68.             productVOList.add(productVO);
  69.         }
  70. //        ResultVO resultVO = new ResultVO();
  71. //        resultVO.setCode(0);
  72. //        resultVO.setMsg("成功");
  73. //        resultVO.setData(productVOList);
  74. //        return resultVO;
  75.         return ResultVOUtil.success(productVOList);
  76.     }
  77. }
注意: 1、我们必须先把数据从数据库中全部查出来,然后再用 for 循环(或者其他的方式)进行赋值拼接,而不要查一部分拼接一部分,因为数据库查询开销远远大于java虚拟机执行。 2、第 50-53 行使用 lambda 表达式( java 8推出的)来合成 list,跟 for 循环没什么区别。 3、第 74 行使用了是 Spring 框架中的一个方法,用来拷贝属性。 4、第 88 行新建了一个 ResultVOUtil 类,下面会写,从而减少了上面几行代码,减少冗余度  

三、新建一个 ResultVOUtil 降低前面代码的冗余度

我们通常看到重复的代码就烦,一定要想办法把它建个类,让大家都可以调用它。一方面少些代码,另一方面也便于维护。 ResultVOUtil.java
  1. package com.liuyanzhao.sell.utils;
  2. import com.liuyanzhao.sell.VO.ResultVO;
  3. /**
  4.  * @Author: 言曌
  5.  * @Date: 2017/11/12
  6.  * @Time: 下午6:32
  7.  */
  8. public class ResultVOUtil {
  9.     public static ResultVO success(Object object) {
  10.         ResultVO resultVo = new ResultVO();
  11.         resultVo.setCode(0);
  12.         resultVo.setMsg("成功");
  13.         resultVo.setData(object);
  14.         return resultVo;
  15.     }
  16.     public static ResultVO success() {
  17.         return success(null);
  18.     }
  19.     public static ResultVO error(Integer code,String msg) {
  20.         ResultVO resultVO = new ResultVO();
  21.         resultVO.setCode(code);
  22.         resultVO.setMsg(msg);
  23.         return resultVO;
  24.     }
  25. }
    最终,访问 http://localhost:8080/sell/buyer/product/list 由于,目前还没有建视图层,所以先返回 JSON 数据进行测试   本文链接:https://liuyanzhao.com/6669.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:1   待审核评论数:0
  1. avatar 阳光岛主

    喜欢技术深入的文章,赞博主