微信点餐项目记录-买家端商品类目Service层的实现与测试

1.Service层的实现,针对商品类目表分别实现四个功能
a.根据主键商品类目id对特定的某类商品进行详细信息的查询。
b.直接查询商品类目中所有的类目的信息。
c.根据买家商品类目编号列表查询多个买家的类目信息。
d.添加买家商品类目。

1.Service接口
首先建立一个CategoryService接口,该接口定义四种方法,针对上面提供的四种功能。

    public interface CategoryService {
    ProductCategory findOne(Integer categoryId);//搞一个方法。存一个主键id,返回这个商品目录这个类对象

    List<ProductCategory> findAll();//建立一个列表,把所有的商品目录放进去。这个是针对卖家端的

    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);//这个是针对买家端的

    ProductCategory save(ProductCategory productCategory);//更新
    }

2.建立Service层的实现类,针对1中建立的接口,然后去实现上面提到的四种方法。其实这个实现的方法的书写和在DAO层写完继承JPA接口之后进行测试是一样的操作,每写好一层,测试的目的一方面是看这层的功能是否能达到预期,另外一方面就是为了下一层的实现类服务。这四个方法仍然是借助JPA的特性去实现,其中查询列表的商品详情信息,是自己在JPA里自己设计的方法。另外三个都是JPA自带的方法,直接使用就可以了。

@Service//service层要加一个注解
public class CategoryServiceImpl implements CategoryService {

    //要通过resposity.这个包里的接口来去创建一个引用,针对ProductCategrory这个类里面的对象,好在这个类里面进行调用
    @Autowired
    private ProductCategoryRepository repository;//有了这个东西,就相当于获取到了ProductCategrory的所有信息。


    @Override
    public ProductCategory findOne(Integer categoryId) {
        return repository.findById(categoryId).orElse(null);//查不到返回null,get抛异常
    }

    @Override
    public List<ProductCategory> findAll() {
        return repository.findAll();
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return repository.findByCategoryTypeIn(categoryTypeList);
    }

    @Override
    public ProductCategory save(ProductCategory productCategory) {
        return repository.save(productCategory);
    }
}

3.针对建立好的Service层,进行测试,建立一个测试类CategoryServiceImplTest。

有些注解上篇已经讲过,这篇就不再重复介绍。这里面是四个方法的实现本质上还是利用的JPA的方法,只是多穿了一件衣服而已。

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

    @Autowired
    private CategoryServiceImpl categoryService;//实现类,之所以categoryService拥有ProductCategory所有信息,是因为CategoryServiceImpl这个类里面放了,ProductCategoryRepository这个类的对象,这个就是JPA的魅力所在。

    @Test
    public void findOne() throws Exception{
        ProductCategory productCategory=categoryService.findOne(1);
        Assert.assertEquals(new Integer(1),productCategory.getCategoryId());//测试id是否相等,前面1是我们期待的一个值,后面是实际得到的值,如果是Object类,那就用new Integer 把1包住
    }

    @Test
    public void findAll() throws Exception{
        List<ProductCategory> productCategories=categoryService.findAll();//搞一个list列表,把商品类目全都存进去。
        Assert.assertNotEquals(0,productCategories.size());//如果这个列表中啥也没有,那就报错
    }

    @Test
    public void findByCategoryTypeIn() throws Exception{
        List<ProductCategory>  productCategories= categoryService.findByCategoryTypeIn(Arrays.asList(2,3,4));
        Assert.assertNotEquals(0,productCategories.size());//如果这个列表中啥也没有,那就报错

    }

    @Test
    public void save() throws Exception{
        ProductCategory productCategory=new ProductCategory("男生专享",10);//这句话就是新建一个行,里面包含了一条新的数据
        ProductCategory  result=categoryService.save(productCategory);
        Assert.assertNotNull(result);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42740023/article/details/107638459