// 1、继承 JpaSpecificationExecutor 接口
extends JpaSpecificationExecutor<TestEntity>
// 2、编写条件
Specification<TestEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
Predicate pre1 = criteriaBuilder.like(root.get("field1"), "%" + keyword + "%");
Predicate pre2 = criteriaBuilder.equal(root.get("field2"), keyword]);
// where field1 like '%keyword%' or field2 = 'keyword'
return criteriaBuilder.or(pre1, pre2);
}
// 3、执行查询
TestRepository.findAll(specification);
Spring Data Jpa:分页、Specification、Criteria
网友评论