public Page<Project> listProject(Pageable pageable, ProjectVO project) {
return projectRepository.findAll(new Specification<Project>() {
@Override
public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
if (!"".equals(project.getTitle()) && project.getTitle() != null) {
predicate.getExpressions().add(cb.like(root.get("title"),"%"+project.getTitle()+"%"));
}
if (project.getYear() != null) {
String yearBeginTime = getYearBeginTime(project.getYear());
String yearEndTime = getYearEndTime(project.getYear());
predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("endTime").as(String.class),yearBeginTime));
predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("endTime").as(String.class),yearEndTime));
}
if (project.getYear() != null && project.getMonth() != null) {
String monthBeginTime = getMonthBeginTime(project.getYear(),project.getMonth());
String monthEndTime = getMonthEndTime(project.getYear(),project.getMonth());
predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("endTime").as(String.class),monthBeginTime));
predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("endTime").as(String.class),monthEndTime));
}
if (project.getTypeId() != null) {
predicate.getExpressions().add(cb.equal(root.<Type>get("type").get("id"),project.getTypeId()));
}
if (!"".equals(project.getStatus()) && project.getStatus() != null) {
predicate.getExpressions().add(cb.equal(root.get("status").as(String.class),project.getStatus()));
}
return predicate;
}
},pageable);
}
效果