JPA Example 基本使用使用實例
阿新 • • 發佈:2018-05-06
font start done 12px ID pri all 多條件 -s
返回單一對象精準匹配:
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryId(111);
//將匹配對象封裝成Example對象
Example<ProductCategory> example =Example.of(productCategory);
//根據id:111精準匹配對象,id必須是唯一主鍵,查出2條會報錯
Optional<ProductCategory> one = repository.findOne(example);
多條件,返回集合:
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("喜歡");
//創建匹配器,即如何使用查詢條件
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("categoryName",,ExampleMatcher.GenericPropertyMatchers.endsWith())//endsWith是categoryName 結尾為喜歡的數據
.withMatcher("categoryName",ExampleMatcher.GenericPropertyMatchers.startsWith()) //
.withIgnorePaths("isFace");//isFace字段不參與匹配
//創建實例
Example<ProductCategory> example =Example.of(productCategory,exampleMatcher);
//查詢
List<ProductCategory> one = repository.findAll(example);
System.out.println(one);
JPA Example 基本使用使用實例