JPA条件照会時間区間用LocalDateTimeの問題

3860 ワード

@Override
    public Page findAll(String outTradeNo, String tradeNo, String mchAppid, String status, String payType,
            String startData, String endData, Pageable pageable) {
        Page pageList = orderDao.findAll((root,query,cb) -> {
            List list = new ArrayList();
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotEmpty(startData) && StringUtils.isNotEmpty(endData)) {
                try {
                    list.add(cb.between(root.get("payTime"),
                            LocalDateTime.parse(sdfmat.format(sdf.parse(startData)),df),
                            LocalDateTime.parse(sdfmat.format(sdf.parse(endData).getTime()+86400000), df)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if(outTradeNo!=null && outTradeNo!="") {
                list.add(cb.like(root.get("outTradeNo"), "%"+outTradeNo+"%"));
            }
            if(tradeNo!=null && tradeNo!="") {
                list.add(cb.like(root.get("tradeNo"), "%"+tradeNo+"%"));
            }
            if(mchAppid!=null && mchAppid!="") {
                list.add(cb.like(root.get("mchAppid"), "%"+mchAppid+"%"));
            }
            if(status!=null && status!="") {
                list.add(cb.equal(root.get("state"), status));
            }
            if(payType!=null && payType!="") {
                list.add(cb.equal(root.get("payType"), payType));
            }
            Predicate[] array = new Predicate[list.size()];
            return cb.and(list.toArray(array));
        }, pageable);
        
        return pageList;
    }

 
転載先:https://www.cnblogs.com/yunqing/p/10551492.html