congxiuzi1880
少年不惧岁月长_
2019-03-24 11:09

java springboot +jpa 动态查询分页问题

  • spring
  • java
  • hibernate
  • java-ee

service 层

 PageRequest pageRequest = new PageRequest(currentPage-0,pageSize);

        List<AuditInfo> auditInfoList =  auditInfoDAO.searchByCondition(date1,date2,new Specification<AuditInfo>() {
                @Override
                public Predicate toPredicate(Root<AuditInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    List<Predicate> list = new ArrayList<>();

                    if(!StringUtils.isEmpty(username)){
                        list.add(cb.equal(root.get("userName").as(String.class),username));
                    }

                    if(!StringUtils.isEmpty(condition)){
                        list.add(cb.equal(root.get("parentType").as(String.class),condition));
                    }
                    query.where(cb.and(list.toArray(new Predicate[list.size()])));
                    return query.getRestriction();
                }
        },pageRequest);

dao:

 @Query(value="select audit from AuditInfo audit where audit.createTime>=?1 and audit.createTime<= ?2 ")
    List<AuditInfo> searchByCondition(Date start,Date end,Specification<AuditInfo> specification,Pageable pageable);

查询得到的一直是全部的结果,筛选条件 无效

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐