JPA多条件组合分页查询,精准查询,模糊查询,时间段查询

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

效果
JPA多条件组合分页查询,精准查询,模糊查询,时间段查询

上一篇:通过curl测试断言predicate


下一篇:时序数据库Influx-IOx源码学习十二(物理计划的执行)