JPA带时间范围的条件查询实现 @Autowiredprivate OrderInvoiceNewRepository orderInvoiceNewRepository;@Overridepublic List findSearch(OrderInvoiceEntity model, String orderNo, Date invoiceCreateStart, Date invoiceCreateEnd) {Assert.
@Autowired private OrderInvoiceNewRepository orderInvoiceNewRepository; @Override public ListfindSearch(OrderInvoiceEntity model, String orderNo, Date invoiceCreateStart, Date invoiceCreateEnd) { Assert.notNull(model); List orderEntityList = orderInvoiceNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { List predicateList = this.commonPredicates(root, criteriaBuilder, model, orderNo, invoiceCreateStart, invoiceCreateEnd); Predicate[] predicates = new Predicate[predicateList.size()]; return criteriaBuilder.and(predicateList.toArray(predicates)); }); return orderEntityList; } @Override public Page pagingSearch(Pageable pageable, OrderInvoiceEntity model, String orderNo, Date invoiceCreateStart, Date invoiceCreateEnd) { Assert.notNull(model); Page pageDataOrder = orderInvoiceNewRepository.findAll((root, query, criteriaBuilder) -> { List predicateList = this.commonPredicates(root, criteriaBuilder, model, orderNo, invoiceCreateStart, invoiceCreateEnd); Predicate[] predicates = new Predicate[predicateList.size()]; return criteriaBuilder.and(predicateList.toArray(predicates)); }, pageable); return pageDataOrder; } private List commonPredicates(Root root, CriteriaBuilder criteriaBuilder, OrderInvoiceEntity model, String orderNo, Date invoiceCreateStart, Date invoiceCreateEnd) { List predicateList = Lists.newLinkedList(); if (StringUtils.isNotEmpty(model.getInvoiceType())) { predicateList.add(criteriaBuilder.equal(root.get("invoiceType").as(String.class), model.getInvoiceType())); } if (StringUtils.isNotEmpty(orderNo)) { predicateList.add(criteriaBuilder.equal(root.get("order.orderNo").as(String.class), orderNo)); } if (invoiceCreateStart != null) { predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("firstInsert").as(Date.class), invoiceCreateStart)); } if (invoiceCreateEnd != null) { predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("firstInsert").as(Date.class), invoiceCreateEnd)); } return predicateList; }