新建数据表映射过来的对象
main--dataobject--ProductCategory.class
@Entity
@DynamicUpdate//实时更新
@Data//省略 get set toString等方法
public class ProductCategory {
// 类目ID
@Id//主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增
private Integer categoryId;
// 类目名称
private String categoryName;
// 类目编号
private Integer categoryType;
public ProductCategory() {
}
public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
DAO层 -
main--repository--ProductCategoryRepository.class
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
测试
选中方法名 -- 右键 -- go to -- test
test--repository--ProductCategoryRepositoryTest.class
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
@Autowired
private ProductCategoryRepository repository;
@Test
public void findOneTest(){
Optional<ProductCategory> result = repository.findById(1);
if (result.isPresent()) {
System.out.print(result.get().toString());
} else {
// handle not found, return null or throw
}
System.out.print("123");
}
@Test
@Transactional//在测试中是完全回滚 测试完自动回滚
public void saveTest() {
ProductCategory productCategory = new ProductCategory("蛋糕", 35);
ProductCategory result = repository.save(productCategory);
Assert.assertNotNull(result);
}
@Test
public void findByCategoryTypeInTest(){
List<Integer> list = Arrays.asList(2,3,4);
List<ProductCategory> result = repository.findByCategoryTypeIn(list);
Assert.assertNotEquals(0,result.size());
}
}
网友评论