其他分享
首页 > 其他分享> > SpringBoot JPA使用Specification多条件查询

SpringBoot JPA使用Specification多条件查询

作者:互联网

public Specification<SwindleAccountEntity> exportSwindleAccountSpecification(ExportSwindleAccountRequest request) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();

            // 一级分行
            String firstBranch = request.getFirstBranch();
            if (StringUtils.isNotBlank(firstBranch)) {
                predicateList.add(criteriaBuilder.equal(root.get("firstBranch"), firstBranch));
            }
            // 开户网点
            String openAccountSite = request.getOpenAccountSite();
            if (StringUtils.isNotBlank(openAccountSite)) {
                predicateList.add(criteriaBuilder.equal(root.get("openAccountSite"), openAccountSite));
            }
            // 状态
            SwindleAccountStatus swindleAccountStatus = request.getSwindleAccountStatus();
            if (swindleAccountStatus != null) {
                predicateList.add(criteriaBuilder.equal(root.get("swindleAccountStatus"), swindleAccountStatus));
            }
            // 录入时间起始日期
            ZonedDateTime zonedDateTime = request.getStartInputTime();
            if (zonedDateTime != null) {
                LocalDateTime startInputTime = zonedDateTime.toLocalDateTime();
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createDatetime").as(LocalDateTime.class), startInputTime));
            }
            // 录入时间结束日期
            ZonedDateTime zonedDateEndTime = request.getEndInputTime();
            if (zonedDateEndTime != null) {
                LocalDateTime endInputTime = zonedDateEndTime.toLocalDateTime();
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createDatetime").as(LocalDateTime.class), endInputTime));
            }
            // 网点开户起始日期
            LocalDate startOpenAccountDate = request.getStartOpenAccountDate();
            if (startOpenAccountDate != null) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("openAccountDate").as(LocalDate.class), startOpenAccountDate));
            }
            // 网点开户结束日期
            LocalDate endOpenAccountDate = request.getEndOpenAccountDate();
            if (endOpenAccountDate != null) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("openAccountDate").as(LocalDate.class), endOpenAccountDate));
            }
            
            /** * order By */
            Order createDateOrder = criteriaBuilder.desc(root.get("createDatetime"));
            return criteriaQuery.orderBy(createDateOrder).where(predicateList.toArray(new Predicate[predicateList.size()])).getRestriction();

        };
    }

 

public Specification<SwindleAccountEntity> getSwindleAccountSpecification(QuerySwindleAccountRequest request) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            // 增加筛选条件
            Predicate predicate = criteriaBuilder.conjunction();
            // 一级分行
            String firstBranch = request.getFirstBranch();
            if (StringUtils.isNotBlank(firstBranch)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("firstBranch"), firstBranch));
            }
            // 开户网点
            String openAccountSite = request.getOpenAccountSite();
            if (StringUtils.isNotBlank(openAccountSite)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("openAccountSite"), openAccountSite));
            }
            // 状态
            SwindleAccountStatus swindleAccountStatus = request.getSwindleAccountStatus();
            if (swindleAccountStatus != null) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("swindleAccountStatus"), swindleAccountStatus));
            }
            // 录入时间起始日期
            ZonedDateTime zonedDateTime = request.getStartInputTime();
            if (zonedDateTime != null) {
                LocalDateTime startInputTime = zonedDateTime.toLocalDateTime();
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.greaterThanOrEqualTo(root.get("createDatetime").as(LocalDateTime.class), startInputTime));
            }
            // 录入时间结束日期
            ZonedDateTime zonedDateEndTime = request.getEndInputTime();
            if (zonedDateEndTime != null) {
                LocalDateTime endInputTime = zonedDateEndTime.toLocalDateTime();
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.lessThanOrEqualTo(root.get("createDatetime").as(LocalDateTime.class), endInputTime));
            }
            // 网点开户起始日期
            LocalDate startOpenAccountDate = request.getStartOpenAccountDate();
            if (startOpenAccountDate != null) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.greaterThanOrEqualTo(root.get("openAccountDate").as(LocalDate.class), startOpenAccountDate));
            }
            // 网点开户结束日期
            LocalDate endOpenAccountDate = request.getEndOpenAccountDate();
            if (endOpenAccountDate != null) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.lessThanOrEqualTo(root.get("openAccountDate").as(LocalDate.class), endOpenAccountDate));
            }
            return predicate;
        };
    }

 

标签:predicate,SpringBoot,get,JPA,Specification,request,predicateList,criteriaBuilder
来源: https://blog.csdn.net/qq_33371766/article/details/114228251