其他分享
首页 > 其他分享> > JPA语法大全

JPA语法大全

作者:互联网

 

 

复杂查询

    @Override
    public JSONResult<PageResult<OrdOfflineOrderVO>> findOfflineOrder(OrdOfflineOrderSearchVO vo) {

        Page<OrdOfflineOrderPO> page= ordOfflineOrderRepository.findAll(new Specification<OrdOfflineOrderPO>() {

            @Override
            public Predicate toPredicate(Root<OrdOfflineOrderPO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtils.isNotBlank(vo.getProductName())){
                    list.add(criteriaBuilder.like(root.get("productName").as(String.class), "%"+vo.getProductName()+"%"));
                }
                if (vo.getStartTime()!=null) {
                    //大于或等于传入时间
                    list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTimestamp").as(Date.class), vo.getStartTime()));
                }
                if (vo.getEndTime()!=null) {
                    //小于或等于传入时间
                    list.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTimestamp").as(Date.class), vo.getEndTime()));
                }


                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        },vo.toPageRequest());
        List<OrdOfflineOrderVO> list=new ArrayList<>();
         page.getContent().forEach(po->{
             OrdOfflineOrderVO orderVO=new OrdOfflineOrderVO();
             BeanUtils.copyProperties(po,orderVO);
             list.add(orderVO);
         });
        PageResult<OrdOfflineOrderVO> result = PageResult.of(page.getNumber(), page.getTotalPages(), page.getTotalElements(), list);
        return new JSONResult(result);
    }

 

标签:JPA,list,vo,语法,add,criteriaBuilder,new,page,大全
来源: https://www.cnblogs.com/xing-nb/p/15714685.html