紧接着上篇的继续写。
ProductInfo:
@Entity
@DynamicUpdate
@Data
public class ProductInfo {
/**
* 商品id
*/
@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;
public ProductInfo() {
}
/**
* Title:
* Description:
* @param productId
* @param productName
* @param productPrice
* @param productStock
* @param productDecription
* @param productIcon
* @param productStatus
* @param categoryType
* @param createTime
* @param updateTime
*/
public ProductInfo(String productId, String productName, BigDecimal productPrice, int productStock,
String productDescription, String productIcon, int productStatus, int categoryType, Date createTime,
Date updateTime) {
super();
this.productId = productId;
this.productName = productName;
this.productPrice = productPrice;
this.productStock = productStock;
this.productDescription = productDescription;
this.productIcon = productIcon;
this.productStatus = productStatus;
this.categoryType = categoryType;
this.createTime = createTime;
this.updateTime = updateTime;
}
}
OrderDetail:
@Entity
@DynamicUpdate
@Data
public class OrderDetail {
/**订单明细详情id.*/
@Id
private String detailId;
/**订单id.*/
private String orderId;
/**商品id.*/
private String productId;
/**商品名称.*/
private String productName;
/**当前价格,单位分.*/
private BigDecimal productPrice;
/**数量.*/
private Integer productQuantity;
/**小图.*/
private String productIcon;
/**创建时间.*/
private Date createTime;
/**修改时间.*/
private Date updateTime;
public OrderDetail() {
}
}
OrderMaster:
@Entity
@DynamicUpdate
@Data
public class OrderMaster {
/** 订单id. */
@Id
// @GeneratedValue(strategy=GenerationType.IDENTITY)
private String orderId;
/** 买家名字. */
private String buyerName;
/** 买家电话. */
private String buyerPhone;
/** 买家地址. */
private String buyerAddress;
/** 买家微信openid. */
private String buyerOpenid;
/** 订单总金额. */
// private double orderAmount;
private BigDecimal orderAmount;
/** 订单状态,默认为新下单. */
private Integer orderStatus;
/** 支付状态,默认未支付. */
private Integer payStatus;
/** 创建时间. */
private Date createTime;
/** 修改时间. */
private Date updateTime;
/**订单明细列表.*/
// @Transient
// private List orderDetailList;
}
注:1.@Data注解是lombok的注解,目的在于简化getset方法,等价于注解@Getter和@Setter两者组合
2.对于属性字段有时间的,如updatetime,想每次操作更改数据表的时候更改update字段,其中@DynamicUpdate注解可以实现这个功能。
接下来是pom依赖:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
true
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-data-jpa
org.projectlombok
lombok
注:这个项目是springboot项目,在创建的时候记得是maven构建的springboot项目,然后添加进去这些依赖;
接下来新建dao层:ProductCategoryRepository继承JpaRepository
public interface ProductCategoryRepository extends JpaRepository{
List findByCategoryTypeIn(List categoryTypeList);
// void UpdateCategoryAndFlush(ProductCategory productCategory);
}
新建service层:
public interface CategoryService {
ProductCategory findOne(Integer categoryId);
List findAll();
List findByCategoryTypeIn(List list);
}
新建service的impl层:
@Service
public class CategoryServiceImpl implements CategoryService {
public CategoryServiceImpl() {
}
@Autowired
private ProductCategoryRepository repository;
@Override
public ProductCategory findOne(Integer categoryId) {
return repository.getOne(categoryId);
}
@Override
public List findAll(){
return repository.findAll();
}
@Override
public List findByCategoryTypeIn(List categoryTypeList) {
return repository.findByCategoryTypeIn(categoryTypeList);
}
}
创建controller层:
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
@Autowired
private ProductInfoService productService;
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
public ResultVO list(){
//1.查询所有上架商品
List productInfoUpList = productService.findUpAll();
//2.查询类目--一次性查出来
List categoryTypeList = productInfoUpList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
List productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
//3.数据的拼装
List productVOList=new ArrayList();
for (ProductCategory productCategory : productCategoryList) {
ProductVO productVO=new ProductVO();
productVO.setCategoryType(productCategory.getCategoryType());
productVO.setCategoryName(productCategory.getCategoryName());
List productInfoVOList= new ArrayList();
for (ProductInfo productInfo : productInfoUpList) {
if(productInfo.getCategoryType()==(productCategory.getCategoryType())){
ProductInfoVO productInfoVO=new ProductInfoVO();
BeanUtils.copyProperties(productInfo,productInfoVO);
productInfoVO.setProductPrice(productInfo.getProductPrice());
productInfoVOList.add(productInfoVO);
}
}
productVO.setProductInfoVOList(productInfoVOList);
productVOList.add(productVO);
}
ResultVO resultVo =new ResultVO();
// ProductVO productVO=new ProductVO();
// ProductInfoVO productinfovo=new ProductInfoVO();
// productinfovo.setProductId("1234234");
// productinfovo.setProductName("皮蛋瘦肉粥");
// productinfovo.setProductPrice(3.4);
// productinfovo.setProductIcon("HTTP://QWEQWE.JPG");
// productinfovo.setProductDescription("好吃不上火");
// productVO.setCategoryType(1);
// productVO.setCategoryName("热榜");//皮蛋瘦肉粥
// productVO.setProductInfoVOList(Arrays.asList(productinfovo));
resultVo.setData(productVOList);
resultVo.setCode(0);
resultVo.setMsg("成功");
return resultVo;
}
}
访问路径:http://localhost:8080/sell/buyer/product/list
结果如下: