类名驼峰式对应表名下划线
1、创建ProductCategory
@Entitypublic class ProductCategory { /** 类目id. */ @Id @GeneratedValue private Integer categoryId; /** 类目名字. */ private String categoryName; /* 类目编号. */ private Integer categoryType;} +get set方法
2、建立ProductCategoryRepository
public interface ProductCategoryRepository extends JpaRepository{ List findByCategoryTypeIn(List categoryTypeList);}
3、对 ProductCategoryRepository 接口测试
@RunWith(SpringRunner.class)@SpringBootTestpublic class ProductCategoryRepositoryTest { @Autowired private ProductCategoryRepository repository; @Test public void findOneTest() { ProductCategory productCategory = repository.findById(1).get(); System.out.println(productCategory.toString()); } @Test @Transactional //不会污染数据库 public void saveTest(){ ProductCategory productCategory = new ProductCategory("男生最爱",4); ProductCategory result = repository.save(productCategory); Assert.assertNotNull(result); //Assert.assertNotEquals(null,result); } @Test public void findByCategoryTypeInTest(){ Listlist = Arrays.asList(2,3,4); List result = repository.findByCategoryTypeIn(list); Assert.assertNotEquals(0,result.size()); }}
4、service层
建立CategoryService:
public interface CategoryService { ProductCategory findOne(Integer categoryId); ListfindAll(); List findByCategoryTypeIn(List categoryTypeList); ProductCategory save(ProductCategory productCategory);}
建立CategoryServiceImpl:
@Servicepublic class CategoryServiceImpl implements CategoryService { @Autowired private ProductCategoryRepository repository; @Override public ProductCategory findOne(Integer categoryId) { return repository.findById(categoryId).get(); } @Override public ListfindAll() { List productCategoryList = repository.findAll(); return repository.findAll(); } @Override public List findByCategoryTypeIn(List categoryTypeList) { return repository.findByCategoryTypeIn(categoryTypeList); } @Override public ProductCategory save(ProductCategory productCategory) { return repository.save(productCategory); }}
5、对Service测试
@RunWith(SpringRunner.class)@SpringBootTestpublic class CategoryServiceImplTest { @Autowired private CategoryServiceImpl categoryService; @Test public void findOne() { ProductCategory productCategory = categoryService.findOne(7); Assert.assertEquals(new Integer(7),productCategory.getCategoryId()); } @Test public void findAll() { ListproductCategoryList = categoryService.findAll(); Assert.assertNotEquals(0,productCategoryList.size()); } @Test public void findByCategoryTypeIn() { List productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(4,5,6)); Assert.assertNotEquals(0,productCategoryList.size()); } @Test public void save() { ProductCategory productCategory = new ProductCategory("男生专享的",12); ProductCategory result = categoryService.save(productCategory); Assert.assertNotNull(result); }}