springboot第二章-----打造企业级微信点餐系统(3)--买家商品

第一步:实体类:ProductInfo.java

package com.fjz.vxsell.bean;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;

/**
 * @author 冯师兄
 * @date 2020-04-28 16:20
 */
@Entity
@Data
public class ProductInfo {
    @Id
    private String productId;//id
    private String productName;//商品名称
    private BigDecimal productPrice;//商品单价
    private Integer productStock;//商品库存
    private String productDescription;//商品描述
    private Integer productStatus;//商品状态
    private String productIcon;//商品图片路径
    private Integer categoryType;//类目编号



}

第二步:repository:ProductInfoRepository.java

package com.fjz.vxsell.repository;

import com.fjz.vxsell.bean.ProductInfo;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author 冯师兄
 * @date 2020-04-28 16:34
 */
public interface ProductInfoRepository extends JpaRepository {

    //根据商品状态查询
    List findByProductStatus(Integer productStatus);
}

第三步:单元测试:ProductInfoRepositoryTest.java

package com.fjz.vxsell.repository;

import com.fjz.vxsell.bean.ProductInfo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoRepositoryTest {

    @Autowired
    private ProductInfoRepository repository;

    @Test
    public void saveTest(){
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId("2");
        productInfo.setProductName("oppoS15");
        productInfo.setProductPrice(new BigDecimal(3522.92));
        productInfo.setProductDescription("很好用");
        productInfo.setProductStatus(0);
        productInfo.setProductIcon("http://D:/Picture/oppoS15.jpg");
        productInfo.setProductStock(50);
        productInfo.setCategoryType(1);
        ProductInfo result = repository.save(productInfo);
        Assert.assertNotNull(result);
    }

    @Test
    public void findByProductStatus() {
        List list = repository.findByProductStatus(0);
        Assert.assertNotEquals(0, list.size());
    }
}

第四步:service:ProductInfoService.java

package com.fjz.vxsell.service;

import com.fjz.vxsell.bean.ProductInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * @author 冯师兄
 * @date 2020-04-28 17:17
 */
public interface ProductInfoService {

    ProductInfo findById(String productId);

    //分页查询所有商品列表
    Page findAll(Pageable pageable);

    //查询上架商品
    List findUpAll();

    //新增
    ProductInfo save(ProductInfo productInfo);

    //
}

第五步:serviceImpl:ProductInfoServiceImpl.java

package com.fjz.vxsell.service.impl;

import com.fjz.vxsell.bean.ProductInfo;
import com.fjz.vxsell.enums.ProductInfoEnums;
import com.fjz.vxsell.repository.ProductInfoRepository;
import com.fjz.vxsell.service.ProductInfoService;
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;

/**
 * @author 冯师兄
 * @date 2020-04-28 17:22
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository repository;

    @Override
    public ProductInfo findById(String productId) {
        return repository.findById(productId).orElse(null);
    }

    /**
     * 这里productStatus,用枚举的形式表现出来,当然也可以不用
     * @param productStatus
     * @return
     */
    @Override
    public List findUpAll() {
        return repository.findByProductStatus(ProductInfoEnums.UP.getCode());
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    /**
     * 分页查询,因为pageable返回的是page对象,而不是List
     * @param pageable
     * @return
     */
    @Override
    public Page findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }
}

第六步:单元测试:ProductInfoServiceImplTest.java

package com.fjz.vxsell.service.impl;

import com.fjz.vxsell.bean.ProductInfo;
import com.fjz.vxsell.enums.ProductInfoEnums;
import org.junit.Assert;
import org.junit.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.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoServiceImplTest {

    @Autowired
    private ProductInfoServiceImpl productInfoService;

    @Test
    public void findById() {
        ProductInfo productInfo = productInfoService.findById("1");
        //期望值为1,实际值为:productInfo.getProductId()
        Assert.assertEquals("1", productInfo.getProductId());
    }

    @Test
    public void findUpAll() {
        List list = productInfoService.findUpAll();
        Assert.assertNotEquals(0, list.size());
    }

    @Test
    public void save() {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId("3");
        productInfo.setProductName("iphoneS85");
        productInfo.setProductPrice(new BigDecimal(7529));
        productInfo.setProductDescription("很好用");
        productInfo.setProductStatus(ProductInfoEnums.DOWN.getCode());
        productInfo.setProductIcon("http://D:/Picture/iphoneS85.jpg");
        productInfo.setProductStock(50);
        productInfo.setCategoryType(1);
        ProductInfo result = productInfoService.save(productInfo);
        Assert.assertNotNull(result);
    }

    @Test
    public void findAll() {
        PageRequest request = new PageRequest(0, 2);
        Page page = productInfoService.findAll(request);
       // System.out.println(page.getTotalElements());
        //实际值不为0
        Assert.assertNotEquals(0, page.getTotalElements());
    }
}

 

 

你可能感兴趣的:(springboot)