目录

 

 

基本概念

代码与实例


 

基本概念

这里有一个思路,后端只发送Json数据,前端vue去解析。这样的话,就可以做到前后端分离,耦合性就很低了。

 

代码与实例

程序运行截图如下:

得到后,使用vue去解析,然后页面显示。

这里可以使用nginx做个代理,就看不出来了!

结构如下:

这里vo就是存的Json格式

controller就是发送请求的

数据库结构如下:

product_category

product_info

关键源码如下:

ProductInfoVo.java

package selldemo.demo.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class ProductInfoVO {

    @JsonProperty("id")
    private String productId;

    @JsonProperty("name")
    private String productName;

    @JsonProperty("price")
    private BigDecimal productPrice;

    @JsonProperty("description")
    private String productDescription;

    @JsonProperty("icon")
    private String productIcon;
}

ProductVO.java

package selldemo.demo.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

@Data
public class ProductVO {

    @JsonProperty("name")
    private String categoryName;

    @JsonProperty("type")
    private Integer categoryType;

    @JsonProperty("foods")
    private List<ProductInfoVO> productInfoVOList;
}

ResultVO.java

package selldemo.demo.vo;

import lombok.Data;

@Data
public class ResultVO<T> {

    // 错误码
    private Integer code;

    // 提示信息
    private String msg;

    // 具体内容
    private T data;
}

BuyerProductController.java

package selldemo.demo.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import selldemo.demo.dataobject.ProductCategory;
import selldemo.demo.dataobject.ProductInfo;
import selldemo.demo.service.CategoryService;
import selldemo.demo.service.ProductService;
import selldemo.demo.vo.ProductInfoVO;
import selldemo.demo.vo.ProductVO;
import selldemo.demo.vo.ResultVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list(){

        //测试用例
//        ResultVO resultVO = new ResultVO();
//        ProductVO productVO = new ProductVO();
//        ProductInfoVO productInfoVO = new ProductInfoVO();
//
//        productVO.setProductInfoVOList(Arrays.asList(productInfoVO));
//        resultVO.setData(productVO);
//
//        resultVO.setCode(0);
//        resultVO.setMsg("成功");
//
//        return resultVO;

        // 1. 查询所有上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        // 2. 查询类目(一次性查询)

        /*
        //传统方法
        List<Integer> categoryTypeList = new ArrayList<>();
        categoryService.findByCategoryTypeIn(categoryTypeList);
        for(ProductInfo productInfo : productInfoList){

            categoryTypeList.add(productInfo.getCategoryType());
        }
        */

        //精简方法java8 lambda
        List<Integer> categoryTypeList = productInfoList.stream().map(e->e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        // 3. 数据拼接
        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory : productCategoryList){

            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo : productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){

                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        ResultVO resultVO = new ResultVO();

        resultVO.setData(productVOList);
        resultVO.setCode(0);
        resultVO.setMsg("成功");

        return resultVO;
    }
}

 

Logo

前往低代码交流专区

更多推荐