美文网首页
买家类目DAO层

买家类目DAO层

作者: 谢谢水果 | 来源:发表于2018-11-11 13:47 被阅读0次

新建数据表映射过来的对象

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());
    }

}

相关文章

网友评论

      本文标题:买家类目DAO层

      本文链接:https://www.haomeiwen.com/subject/xhpxfqtx.html