微信点餐之升级2.1.1(15)
修改pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修改application.yml文件
driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动 2.1.1
server:
port: 80 #端口
servlet:
context-path: /sell #项目名称路径 2.1.1
修改所有的jpa调用的findOne方法
com\imooc\service\imp\ProductCategoryServiceImp.java
/*查询单条记录--卖家端*/
Override
public ProductCategory findOne(Integer categoryId) {
/*spring boot 1.5.3
return productCategoryDao.findOne(categoryId);*/
/*spring boot 2.1.1*/
return productCategoryDao.findById(categoryId).orElse(null);
}
com\imooc\service\imp\OrderServiceImp.java
@Override
public OrderDTO findOne(String orderId) {
/*查询该订单是否存在*/
/*spring boot 1.5.3
OrderMaster orderMaster = orderMasterDao.findOne(orderId);
*/
/*spring boot 2.1.1*/
OrderMaster orderMaster = orderMasterDao.findById(orderId).orElse(null);
if(orderMaster == null){
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
/*根据订单id查询订单详细*/
List<OrderDetail> orderDetailList = orderDetailDao.findByOrderId(orderId);
if(CollectionUtils.isEmpty(orderDetailList)){
throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
}
OrderDTO orderDTO = new OrderDTO();
BeanUtils.copyProperties(orderMaster,orderDTO);
orderDTO.setOrderDetailList(orderDetailList);
return orderDTO;
}
com\imooc\service\imp\ProductInfoServiceImp.java
/*查询单个产品*/
@Override
public ProductInfo findOne(String ProductId) {
/*spring boot 1.5.3
return productInfoDao.findOne(ProductId);
*/
/*spring boot 2.1.1*/
return productInfoDao.findById(ProductId).orElse(null);
}
@Override
@Transactional//事务
public void increaseStock(List<CartDTO> cartDTOList) {
for(CartDTO cartDTO : cartDTOList){
/*spring boot 1.5.3
ProductInfo productInfo = productInfoDao.findOne(cartDTO.getProductId());
*/
/*spring boot 2.1.1*/
ProductInfo productInfo = productInfoDao.findById(cartDTO.getProductId()).orElse(null);
if(productInfo == null){
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
Integer result = productInfo.getProductStock()+cartDTO.getProductQuantity();
productInfo.setProductStock(result);
productInfoDao.save(productInfo);
}
}
@Transactional/*事务*/
public void decreaseStock(List<CartDTO> cartDTOList) {
for(CartDTO cartDTO : cartDTOList){
//根据传递过来的产品id查询商品
/*spring boot 1.5.3
ProductInfo productInfo = productInfoDao.findOne(cartDTO.getProductId());
*/
/*spring boot 2.1.1*/
ProductInfo productInfo = productInfoDao.findById(cartDTO.getProductId()).orElse(null);
if(productInfo == null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
//用查询出的库存减去传递过来的购买数量
Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
if(result < 0){
throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
}
//更新数据库
productInfo.setProductStock(result);
productInfoDao.save(productInfo);
}
}
@Override
public ProductInfo on_sale(String ProductId) {
/*spring boot 1.5.3
ProductInfo productInfo = productInfoDao.findOne(ProductId);
*/
/*spring boot 2.1.1*/
ProductInfo productInfo = productInfoDao.findById(ProductId).orElse(null);
if(productInfo == null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
productInfo.setProductStatus(0);/*更新上架状态*/
return productInfoDao.save(productInfo);
}
/*下架*/
@Override
public ProductInfo off_sale(String ProductId) {
/*spring boot 1.5.3
ProductInfo productInfo = productInfoDao.findOne(ProductId);
*/
/*spring boot 2.1.1*/
ProductInfo productInfo = productInfoDao.findById(ProductId).orElse(null);
if(productInfo == null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
productInfo.setProductStatus(1);/*更新上架状态*/
return productInfoDao.save(productInfo);
}
消除警告信息:
Build---Rebuild Project
1:@NotEmpty注解过时,需要删除原来的import 重新引入即可
2:new PageRequest过时,改为PageRequest.of即可