springdata jpa
2019-07-07 本文已影响0人
過眼云烟
public Page<AliScdnTask> refreshList(AliScdnIn aliScdnIn) {
if("all".equals(aliScdnIn.getObjectType())){
aliScdnIn.setObjectType(null);
}
Specification<AliScdnTask> specification = new Specification<AliScdnTask>() {
@Override
public Predicate toPredicate(Root<AliScdnTask> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> 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);
}