5. 买家商品(productInfo)
5.1、 流程一览
写productInfo商品相关的实体类,dao,service,test类
- 实体类
- ProductInfo.java
@Entity //将数据库映射成对象的标签
@DynamicUpdate //动态更新标签,
@Data//自动get set标签!
@Id //主键标签
@GeneratedValue //自增标签
- dao接口
- ProductinfoRepository.java
继承JpaRepository< ProductInfo,String> String是主键类型
写上一些其它需要使用的数据库方法,命名是有规定的
- 测试接口,是否能连接上数据库,能否操作增删改查
- service接口和service实体类,service写具体需要的方法
- 配置application.yml
server:
context-path: /sell
-
ProductService.java
-
ProductServiceImpl.java
继承ProductService接口
@Service //—service端的注解
@Autowired//------注入标签 ,产生dao对象,可以用jpa上操作数据库的方法
- ProductStatusEnum //----Enum枚举状态
- 测试service实体类
- API的VO,先确定需求,根据API文档写
- ResultVO.java
- ProductVO.java
- ProductInfoOV.java
@Data
@JsonProperty // 代码中的参数名和返回到jsp页面中需要的参数名不同时使用
- control层,拼接VO,
- BuyerProductController
@RestController //返回的jsp,所以用controller的标签
@RequestMapping("/buyer/product") //url前缀
@Autowired
- ResultVOUtil // 工具类,生成拼接数据结果
- 测试用网页打开http://localhost:8080/sell/buyer/product/list查看是否有数据库数据
5.1.1 实体类和dao类
注意dao中的方法命名 ↓ findByProductStatus
public interface ProductinfoRepository extends JpaRepository<ProductInfo,String>{
/** 查询上架的商品 */
List<ProductInfo> findByProductStatus(Integer productStatus); //需要实体类无参构造函数
}
5.1.2 service接口和service impl实例类
注意: 查询所有带有的是分页信息,而不是普通的List
public interface ProductService {
ProductInfo findOne(String productId);
/** 查询所有在架商品列表*/
List<ProductInfo> findUpAll();
//分页查询所有页
Page<ProductInfo> findAll(Pageable pageable);
ProductInfo save(ProductInfo productinfo);
// 加库存
// 减库存
}
5.1.3 写service impl的测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductServiceImplTest {
@Autowired
private ProductServiceImpl productService;
@Test
public void findAll() throws Exception {
PageRequest request = new PageRequest(0,2);
Page<ProductInfo> productInfoPage = productService.findAll(request);
System.out.println(productInfoPage.getTotalElements());
}
}
5.1.4 API编写,OV类
确定API文档,写各层的OV类
重点注解:
@JsonProperty // 代码中的参数名和返回到jsp页面中需要的参数名不同时使用
@Data
public class ProductInfoOV {
@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;
}
5.1.5 API编写,拼接control类拼接信息
@RestController //返回的jsp,所以用controller的标签
@RequestMapping("/buyer/product") //url前缀
public class BuyerProductController {
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
public ResultVO list() {
// 1.查询所有上架商品(此处无翻页)
List<ProductInfo> productInfoList = productService.findUpAll();
// 2.查询类目(注意一次性查询,不要十个商品,循环查10次那样)
// List<Integer> categoryTypeList = new ArrayList<>();
// 传统方法
// 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<ProductInfoOV> productInfoOVList = new ArrayList<>();
// 商品详情
for(ProductInfo productInfo : productInfoList) {
if(productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
ProductInfoOV productInfoOV = new ProductInfoOV();
BeanUtils.copyProperties(productInfo, productInfoOV);
productInfoOVList.add(productInfoOV);
}
}
productVO.setProductInfoOVList(productInfoOVList);
productVOList.add(productVO);
}
return ResultVOUtil.success(productVOList);
}
}
工具类的代码:
public class ResultVOUtil {
public static ResultVO success(Object object) {
ResultVO resultVO = new ResultVO();
resultVO.setData(object);
resultVO.setCode(0);
resultVO.setMsg("成功");
return resultVO;
}
public static ResultVO success() {
return success(null);
}
public static ResultVO error(Integer code, String msg) {
ResultVO resultVO = new ResultVO();
resultVO.setCode(code);
resultVO.setCode(code);
resultVO.setMsg(msg);
return resultVO;
}
}
5.2、enum枚举类的使用
比如下面,经常有判断0,1,但是久了就不知道0和1是什么了
public class ProductServiceImpl implements ProductService {
@Autowired
ProductinfoRepository repository ;
@Override
public List<ProductInfo> findUpAll() {
return repository.findByProductStatus(0);
//使用enum后改为
//return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
}
}
枚举类,注意@Getter标签
@Getter
public enum ProductStatusEnum {
UP(0,"在架"),
DOWN(1,"下架")
;
private Integer code;
private String message;
ProductStatusEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}