创建产品服务工程

一、创建工程Product

 

下一步,选择Cloud Discovery-> Eureka Discovery

配置同Client工程,工程名称改为product

 然后启动product。

打开Eureka localhost:8761,可以看到Product已经注册进来了。

二、数据访问使用Spring Data JPA

1、pom文件中引入pring Data JPA和Mysql

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

 

2. 修改配置文件

show-sql:方便调试

 

三、创建dataobject包

增加依赖

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

增加lombk插件,我这里已经安装

这样在类中不用自己写Get和Set方法。 只需要在类前面加入@Data注解



增加ProductInfo类
//类名对应表名,如果不一致,需要加注解
//@Table(name = "T_proxxx")

@Data
@Entity //和数据库的表对应
public class ProductInfo {

    @Id //主键
    private String productId;

    /** 名字. */
    private String productName;

    /** 单价. */
    private BigDecimal productPrice;

    /** 库存. */
    private Integer productStock;

    /** 描述. */
    private String productDescription;

    /** 小图. */
    private String productIcon;

    /** 状态, 0正常1下架. */
    private Integer productStatus;

    /** 类目编号. */
    private Integer categoryType;

    private Date createTime;

    private Date updateTime;

}

  

四、创建包repository

创建类ProductInfoRepository类, 定义List<ProductInfo> findByProductStatus(Integer productStatus)方法。

然后在类ProductInfoRepository右键-> Go 

.....

 然后勾选

findByProductStatus方法

这样,就生成了ProductInfoRepositoryTest类,增加RunWith和SpringBooTest注解。

然后完善findByProductStatus方法,运行单元测试方法

测试结果:

六、产品类目

1、创建产品目录实体类

package com.example.product.dataobject;

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;


@Data
@Entity
public class ProductCategory {

    @Id //主键
    @GeneratedValue //自增
    private Integer categoryId;
    //类目名称
    private String categoryName;
    //类目编号
    private Integer  categoryType;
    private Date createTime;
    private Date updateTime;
}

 

2、创建Dao层的ProductCategoryRepository 接口

3、 创建单元测试

七、创建服务

1、创建接口ProductService

2. 创建服务

 产品状态Enum

3、单元测试

其中ProductApplicationTests类

 同理,创建类目服务

八、创建Controller

1、创建ProductController类

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO<ProductVO> list(){
        //查询所有在架商品
        List<ProductInfo>  productInfoList = productService.findUpAll();
        //获取类目的type列表
       List<Integer> categoryTypeList =   productInfoList.stream()
               .map(ProductInfo::getCategoryType)
               .collect(Collectors.toList());

       //从数据库查询类目
       List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

       //构造数据
        List<ProductVO> productVOList = new ArrayList<>();
       for(ProductCategory productCategory: categoryList){
           ProductVO productVO = new ProductVO();
           productVO.setCategoryName(productCategory.getCategoryName());
           productVO.setCategoryType(productCategory.getCategoryType());
           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);
       }

      return ResultVoUtil.success(productVOList);

    }
}

  

其中ResultVO类

@Data
public class ResultVO<T>
{
    private Integer code;
    private String msg;
    //具体内容
    private T data;
}

  

ProductInfoVO类

@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类

@Data
public class ProductVO {

    @JsonProperty("name")
    private String categoryName;

    @JsonProperty("type")
    private Integer categoryType;

    @JsonProperty("foods")
    List<ProductInfoVO> productINfoVOList;

}

  

ResultVoUtil 类

public class ResultVoUtil {
    public static ResultVO success(Object object){
        ResultVO resultVO = new ResultVO();
        resultVO.setData(object);
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        return  resultVO;
    }
}

  

最后启动工程测试

原文地址:https://www.cnblogs.com/linlf03/p/9965745.html