spring boot+ jpa使用specification 查询分页

public Page  listShowDProgramme(int pageNumber, int pageSize, JSONObject jsonObject) {
    Specification  librarySpecification = new Specification() {
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
            List list = new ArrayList();
            if(jsonObject != null) {

                String ifChecked = jsonObject.getString("ifChecked"); //是否审核
                if (StringUtils.isNotBlank(ifChecked)) {
                    Path ifCheckedPath = root.join("dProgrammeExtend").get("ifChecked");
                    list.add(cb.equal(ifCheckedPath.as(Integer.class), Integer.valueOf(ifChecked)));
                }

                String ifShow  = jsonObject.getString("ifShow"); //是否显示
                if(StringUtils.isNotBlank(ifShow)) {
                    Path ifShowPath = root.join("dProgrammeExtend").get("ifShow");
                    list.add(cb.equal(ifShowPath.as(Boolean.class),Boolean.valueOf(ifShow)));
                }

                String name = jsonObject.getString("query"); //方案名称
                if(StringUtils.isNotBlank(name)) {
                    Path namePath = root.get("name");
                    list.add(cb.like(namePath.as(String.class), "%" + name.toLowerCase() + "%"));
                }

                Predicate predptype = null;
                String ptype = jsonObject.getString("ptype");  //方案类型
                if(StringUtils.isNotBlank(ptype)) {
                    Path ptypePath = root.get("pType");
                    predptype = cb.like(ptypePath.as(String.class),"%" + ptype + "%");
                    list.add(cb.like(ptypePath.as(String.class),"%" + ptype + "%"));
                }

                Predicate predtime = null;
                String createtime = jsonObject.getString("time");
                if(StringUtils.isNotBlank(createtime)) {
                    Path createTimePath = root.get("createTime");
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Calendar c = Calendar.getInstance();
                    Date start = c.getTime();
                    int time = Integer.parseInt(createtime);
                    if(3 == time) {
                        c.add(Calendar.DATE,-time);
                        Date end3 = c.getTime();
                        predtime = cb.between(createTimePath.as(Date.class),end3,start);
                        list.add(cb.between(createTimePath.as(Date.class),end3,start));
                    }else if(7 == time) {
                        c.add(Calendar.DATE,-time);
                        Date endWeek = c.getTime();
                        predtime = cb.between(createTimePath.as(Date.class),endWeek,start);
                        list.add(cb.between(createTimePath.as(Date.class),endWeek,start));
                    }else if(30 == time) {
                        c.add(Calendar.MONTH,-1);
                        Date endMonth = c.getTime();
                        predtime = cb.between(createTimePath.as(Date.class),endMonth,start);
                        list.add(cb.between(createTimePath.as(Date.class),endMonth,start));
                    }else if(180 == time) {
                        c.add(Calendar.MONTH,-6);
                        Date end180 = c.getTime();
                        predtime = cb.between(createTimePath.as(Date.class),end180,start);
                        list.add(cb.between(createTimePath.as(Date.class),end180,start));
                    }else {
                        c.add(Calendar.YEAR,-1);
                        Date endYear= c.getTime();
                        predtime = cb.between(createTimePath.as(Date.class),endYear,start);
                        list.add(cb.between(createTimePath.as(Date.class),endYear,start));
                    }
                }

                if(StringUtils.isNotBlank(ptype) && StringUtils.isNotBlank(createtime)) {
                    list.add(cb.and(predptype,predtime));   //相当于where predptype and predtime;
                }

                String isRecommend = jsonObject.getString("isRecommend"); //是否推荐
                if(StringUtils.isNotBlank(isRecommend)) {
                    Path  isRecommendPath = root.join("dProgrammeExtend").get("isRecommend");  //自动内连
                    list.add(cb.equal(isRecommendPath.as(Boolean.class),Boolean.valueOf(isRecommend)));
                }
            }

            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }
    };
    Sort sort = new Sort(Sort.Direction.DESC, "createTime");
    Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);
    return dProgrammeLibraryDao.findAll(librarySpecification,pageable);
}

你可能感兴趣的:(spring boot+ jpa使用specification 查询分页)