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); }
spring boot+ jpa使用specification 查询分页
最新推荐文章于 2022-12-28 14:22:11 发布