JPA Example 基本使用使用实例

时间:2018-05-06 22:25:52   收藏:0   阅读:7203

返回单一对象精准匹配:
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);
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!