springdata jpa

    public Page refreshList(AliScdnIn aliScdnIn) {
        if("all".equals(aliScdnIn.getObjectType())){
            aliScdnIn.setObjectType(null);
        }
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List predicates = new ArrayList<>();
                //条件查询匹配对象
                if(StringUtils.isNoneBlank(aliScdnIn.getObjectType())){
                    Predicate predicate = criteriaBuilder.equal(root.get("objectType").as(String.class),aliScdnIn.getObjectType());
                    predicates.add(predicate);
                }
                //条件查询,模糊查询匹配对象
                if(StringUtils.isNoneBlank(aliScdnIn.getUrl())){
                    Predicate predicate = criteriaBuilder.like(root.get("objectPath").as(String.class),"%"+aliScdnIn.getUrl()+"%");
                    predicates.add(predicate);
                }
                //条件查询,区间匹配,开始时间到结束时间
                if(aliScdnIn.getStartTime()!=null&&aliScdnIn.getEndTime()!=null){
                    Predicate less = criteriaBuilder.lessThanOrEqualTo(root.get("creationTime").as(Date.class),aliScdnIn.getEndTime());
                    predicates.add(less);
                    Predicate greater = criteriaBuilder.greaterThanOrEqualTo(root.get("creationTime").as(Date.class),aliScdnIn.getStartTime());
                    predicates.add(greater);
                }
              //将集合转换为数组,放入构建对象中
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        //传入当前页与每页数
        Pageable pageable = PageRequest.of(aliScdnIn.getPage()-1,aliScdnIn.getLimit());
        //将构建对象与当前页参数放入findAll中查询,直接实现,就会再dao层生成接口
        return aliScdnTaskDao.findAll(specification,pageable);
    }

你可能感兴趣的:(springdata jpa)