JPA条件查询,分页

public Page<VWProcessDetails> list(Integer pageSize, Integer pageNumber, String topicCode, String sourceCode, String countyCode, String villageCode, String qStatusCode, Timestamp startTime, Timestamp endTime, String executiver) {
    Specification<VWProcessDetails> specification = new Specification<VWProcessDetails>() {
        @Override
        public Predicate toPredicate(Root<VWProcessDetails> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
            ArrayList<Predicate> predicates = new ArrayList<>();
            if (!topicCode.isEmpty()) {
                predicates.add(cb.equal(root.get("topicCode").as(String.class), topicCode));
            }
            if (!sourceCode.isEmpty()) {
                predicates.add(cb.equal(root.get("sourceCode").as(String.class), sourceCode));
            }
            if (!countyCode.isEmpty()) {
                predicates.add(cb.equal(root.get("countyCode").as(String.class), countyCode));
            }
            if (!villageCode.isEmpty()) {
                predicates.add(cb.equal(root.get("villageCode").as(String.class), villageCode));
            }
            if (!qStatusCode.isEmpty()) {
                predicates.add(cb.equal(root.get("qStatusCode").as(String.class), qStatusCode));
            }
            if (startTime != null && endTime != null) {
                predicates.add(cb.between(root.get("entryTime"), startTime, endTime));
            }
          //or判断
            ArrayList<Predicate> or = new ArrayList<>();
            if (executiver != null && !executiver.isEmpty()) {
                or.add(cb.equal(root.get("executiver").as(String.class), executiver));
            }
            if (executiver != null && !executiver.isEmpty()) {
                or.add(cb.equal(root.get("entrant").as(String.class), executiver));
            }
            predicates.add(cb.or(or.toArray(new Predicate[or.size()])));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
    Page<VWProcessDetails> page = getPage(specification, pageSize, pageNumber);
    return page;
}

 

public List<VwBaseInfo> provinceWaterAreaByYears() {
    Sort.Order order = Sort.Order.asc("year");
    Sort sort = Sort.by(order);
    List<VwBaseInfo> list = baseInfoDao.findByAdLevel(PROVINCEADMINDIVLEVEL, sort);
    return list;
}
//另一种or判断
ArrayList<Predicate> or = new ArrayList<>();
if (!userCode.isEmpty()) {
    or.add(cb.equal(root.get("entrant").as(String.class), userCode));
    or.add(cb.equal(root.get("executiver").as(String.class), userCode));
}
ArrayList<Predicate> list = new ArrayList<>();
list.add(cb.and(predicates.toArray(new Predicate[predicates.size()])));
list.add(cb.or(or.toArray(new Predicate[or.size()])));
return cb.or(list.toArray(new Predicate[predicates.size()]));

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值