当前位置 : 主页 > 编程语言 > java >

JPA带时间范围的条件查询实现

来源:互联网 收集:自由互联 发布时间:2021-06-28
JPA带时间范围的条件查询实现 @Autowiredprivate OrderInvoiceNewRepository orderInvoiceNewRepository;@Overridepublic List findSearch(OrderInvoiceEntity model, String orderNo, Date invoiceCreateStart, Date invoiceCreateEnd) {Assert.
JPA带时间范围的条件查询实现
@Autowired
	private OrderInvoiceNewRepository orderInvoiceNewRepository;

	@Override
	public List
 
   findSearch(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; }
         
        
       
      
     
    
   
  
 
网友评论