文章目录
0. 简介
主要实现买家商品Service层的功能:
1、根据productId查询某个商品
2、查询所有在架商品
3、分页查询所有的商品
4、添加商品
1. ProductService接口
package com.hh.service;
import com.hh.dataobject.ProductInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
/**
* @author hh
* 商品Service层接口
*/
public interface ProductService {
/**
* 根据productId查询某个商品
* @param productId
* @return
*/
ProductInfo findOne(String productId);
/**
* 查询所有在架商品
* @return
*/
List<ProductInfo> findUpAll();
/**
* 分页查询所有的商品
* @param pageable
* @return
*/
Page<ProductInfo> findAll(Pageable pageable);
/**
* 添加商品
* @param productInfo
* @return
*/
ProductInfo save(ProductInfo productInfo);
//加库存
//减库存
}
2. ProductServiceImpl 实现类
package com.hh.service.impl;
import com.hh.dataobject.ProductInfo;
import com.hh.enums.ProductStatusEnum;
import com.hh.repository.ProductInfoRepository;
import com.hh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductInfoRepository repository;
@Override
public ProductInfo findOne(String productId) {
return repository.getOne(productId) ;
}
@Override
public List<ProductInfo> findUpAll() {
return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
}
@Override
public Page<ProductInfo> findAll(Pageable pageable) {
return repository.findAll(pageable);
}
@Override
public ProductInfo save(ProductInfo productInfo) {
return repository.save(productInfo);
}
}
3. ProductStatusEnum枚举类
这个枚举类的作用就是增加程序的可读性,用来描述商品上下架的状态
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author hh
*
* 定义枚举类型增加程序的可读性
* 用来描述商品的状态
*/
@Getter
@AllArgsConstructor
public enum ProductStatusEnum {
UP(0,"在架"),
DOWN(1,"下架");
//code表示状态值
private Integer code;
//message表示状态值对应状态
private String message;
}
4. ProductServiceImplTest测试类
package com.hh.service.impl;
import com.hh.dataobject.ProductInfo;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest
class ProductServiceImplTest {
@Autowired
private ProductServiceImpl productService;
@Test
void findOne() {
ProductInfo productInfo = productService.findOne("123456");
Assert.assertEquals("123456",productInfo.getProductId());
}
@Test
void findUpAll() {
List<ProductInfo> productInfoList = productService.findUpAll();
Assert.assertNotEquals(0,productInfoList.size());
}
/**
*new PageRequest()已经过时
*/
@Test
void findAll() {
PageRequest request = PageRequest.of(0,2);
Page<ProductInfo> productInfoPage = productService.findAll(request);
System.out.println(productInfoPage.getTotalElements());
}
@Test
void save() {
ProductInfo productInfo = new ProductInfo();
//设置商品Id
productInfo.setProductId("123456");
productInfo.setProductName("皮蛋粥");
productInfo.setProductPrice(new BigDecimal(3.2));
productInfo.setProductStock(100);
productInfo.setProductDescription("很好喝的哦");
productInfo.setProductIcon("http://pidan.jpg");
productInfo.setProductStatus(0);
//注意:这个商品类目编号一定要在数据库中存在才可以
productInfo.setCategoryType(2);
ProductInfo result = productService.save(productInfo);
Assert.assertNotNull(result);
}
}
补充知识:
new PageRequest的方法创建Pageable对象这个方法已经过时了,替代的方法是不要new PageRequest,而是直接用 PageRequest.of这个方法 根据你的需求选择入参;
@Override
public Page<People> getPage(Integer pageNum, Integer pageLimit) {
Pageable pageable =new PageRequest(pageNum - 1,pageLimit);
return emr.findAll(pageable);
}
@Override
public Page<People> getPage(Integer pageNum, Integer pageLimit) {
Pageable pageable =PageRequest.of(pageNum - 1,pageLimit);
return emr.findAll(pageable);