JPA常用
@Override public Page<SapOrder> findPage(SapOrderModel sapOrderModel, int currentPage, int pageSize) { Specification<SapOrder> specification = new Specification<SapOrder>() { @Override public Predicate toPredicate(Root<SapOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> list = new ArrayList<>(); Predicate deleteMark = cb.equal(root.get("deleteMark").as(Boolean.class), false); list.add(deleteMark); if(!CommonUtil.isEmpty(sapOrderModel.getSapSaler())){ List<Long> salerIds = sapSearchMapper.getSalerIds(sapOrderModel.getSapSaler().trim()); if(salerIds != null && salerIds.size() > 0) { CriteriaBuilder.In<Long> in = cb.in(root.get("sapSaler")); for (Long id : salerIds) { in.value(id); } list.add(in); } } if(!CommonUtil.isEmpty(sapOrderModel.getStartTime()) && !CommonUtil.isEmpty(sapOrderModel.getEndTime())){ //起始日期 Predicate startTime = cb.greaterThanOrEqualTo(root.get("docDate").as(String.class), sapOrderModel.getStartTime()); list.add(startTime); //结束日期 Predicate endTime = cb.lessThanOrEqualTo(root.get("docDate").as(String.class), sapOrderModel.getEndTime()); list.add(endTime); } if (!CommonUtil.isEmpty(sapOrderModel.getuXsxz())) { Predicate uXsxz = cb.like(root.get("uXsxz"), "%" + sapOrderModel.getuXsxz() + "%"); list.add(uXsxz); } if (!CommonUtil.isEmpty(sapOrderModel.getSapCustom())) { Predicate sapCustom = cb.like(root.get("sapCustom"), "%" + sapOrderModel.getSapCustom() + "%"); list.add(sapCustom); } Predicate[] predicates = new Predicate[list.size()]; return cb.and(list.toArray(predicates)); } }; //排序 List<Sort.Order> orders = new ArrayList<>(); orders.add(new Sort.Order(Sort.Direction.ASC, "createDate")); Sort sort = Sort.by(orders); Pageable pageable = PageRequest.of(currentPage - 1, pageSize, sort); return sapOrderRepository.findAll(specification, pageable); }