spring boot+ jpa使用specification 查询分页

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

                String ifChecked = jsonObject.getString("ifChecked"); //是否审核
                if (StringUtils.isNotBlank(ifChecked)) {
                    Path<Integer> 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<Boolean> 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<String> 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<String> 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<Date> 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<Boolean>  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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值