0. 简介
对买家类目Service层的实现:主要有四个方法
1、根据买家类目Id查询单个买家商品类目
2、查询所有的买家商品类目
3、根据买家商品类目编号列表查询多个买家类目
4、添加买家商品类目
1. CategoryService接口
import com.hh.dataobject.ProductCategory;
import java.util.List;
/**
* @author hh
* 商品类目Service层接口
*/
public interface CategoryService {
/**
* 根据categoryId查询单个商品类目
* @param categoryId
* @return
*/
ProductCategory findOne(Integer categoryId);
/**
* 查询所有商品类目
* @return
*/
List<ProductCategory> findAll();
/**
* 根据categoryTypeList查询商品类目
* @param categoryTypeList
* @return
*/
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
/**
* 添加商品类目
* @param productCategory
* @return
*/
ProductCategory save(ProductCategory productCategory);
}
2. CategoryServiceImpl实现类
import com.hh.dataobject.ProductCategory;
import com.hh.dataobject.service.CategoryService;
import com.hh.dataobject.repository.ProductCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author hh
* 商品类目Service层的实现
*/
@Service
public class CategoryServiceImpl implements CategoryService {
//引入dao层接口实现类
@Autowired
private ProductCategoryRepository repository;
@Override
public ProductCategory findOne(Integer categoryId) {
return repository.getOne(categoryId);
}
@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);
}
}
右键go to —>Test—>就会在相应的test包下面自动创建测试类:
3. CategoryServiceImplTest测试类
import com.hh.dataobject.ProductCategory;
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.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class CategoryServiceImplTest {
@Autowired
private CategoryServiceImpl categoryService;
@Test
void findOne() {
ProductCategory productCategory = categoryService.findOne(1);
Assert.assertEquals(new Integer(1),productCategory.getCategoryId());
}
@Test
void findAll() {
List<ProductCategory> productCategoryList = categoryService.findAll();
Assert.assertNotEquals(0,productCategoryList.size());
}
@Test
void findByCategoryTypeIn() {
List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(1,2,3,4,5));
Assert.assertNotEquals(0,productCategoryList.size());
}
@Test
void save() {
ProductCategory productCategory = new ProductCategory("男生专享",4);
ProductCategory result = categoryService.save(productCategory);
Assert.assertNotNull(result);
}
}
补充一个Idea快捷键:ctrl+shift+enter可以跳到代码的下一行的行首