美文网首页
Spring Data Jpa 分页带参数排序查询

Spring Data Jpa 分页带参数排序查询

作者: 忘记_3a6a | 来源:发表于2020-03-27 13:56 被阅读0次
       //查看公告列表
        public PagingQueryUtil findAllInfo(Integer pageSize,Integer currentPage){
            Long accountId=LocalAccountHelper.getAccountId();
            //按照创建时间倒叙
            Sort sort=new Sort(Sort.Direction.DESC, "createTime");
            //分页
            PageRequest pageRequest=new PageRequest(currentPage,pageSize,sort);
            //传参
            Specification<RecruitSubaccountBulletin>  specification=new Specification<RecruitSubaccountBulletin>() {
                @Override
                public Predicate toPredicate(Root<RecruitSubaccountBulletin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Predicate id=cb.equal(root.get("id").as(Long.class),accountId);
                    Predicate isDelete=cb.equal(root.get("isDelete").as(Short.class),CommonValueUtil.defaultValue);
                    Predicate predicate= cb.and(id,isDelete);
                    return predicate;
                }
            };
            //查询
            Page<RecruitSubaccountBulletin> page=recruitSubaccountBulletinRepository.findAll(specification,pageRequest);
            //转换查询结果
            List<RecruitInformationView>  recruitInformationViews=this.getInformationViews(page.getContent());
            return PagingQueryUtil.getPageInfo(page,recruitInformationViews);
        }
    
    
        public static PagingQueryUtil getPageInfo(Page<?> page){
            PagingQueryUtil pagingQueryUtil=new PagingQueryUtil();
            pagingQueryUtil.setPages(page.getTotalPages());
            pagingQueryUtil.setPageNum(page.getNumber()+1);
            pagingQueryUtil.setTotal((int) page.getTotalElements());
            pagingQueryUtil.setPageSize(page.getSize());
            pagingQueryUtil.setData(page.getContent());
            return pagingQueryUtil;
        }
    

    相关文章

      网友评论

          本文标题:Spring Data Jpa 分页带参数排序查询

          本文链接:https://www.haomeiwen.com/subject/bwyluhtx.html