Java 如何使用typedQuery在条件api上添加分页?

Java 如何使用typedQuery在条件api上添加分页?,java,spring,spring-boot,criteria-api,Java,Spring,Spring Boot,Criteria Api,我使用带pageable的Criteria API返回带pageable的页面,但是当我插入setFirstResult和setMaxResults时,查询总是返回10个元素 如果我从TypedQuery中删除setFirstResult和setMaxResults,我的TypedQuery.getResultList将返回所有元素,但显然没有分页 我有一个服务,它调用我的条件,为主函数peopleRepository.filter发送我的可分页内容 公共页筛选器ParcialString名称、

我使用带pageable的Criteria API返回带pageable的页面,但是当我插入setFirstResult和setMaxResults时,查询总是返回10个元素

如果我从TypedQuery中删除setFirstResult和setMaxResults,我的TypedQuery.getResultList将返回所有元素,但显然没有分页

我有一个服务,它调用我的条件,为主函数peopleRepository.filter发送我的可分页内容

公共页筛选器ParcialString名称、字符串rg、字符串mom、字符串cpf、字符串昵称、整数页码、整数页面大小、列表排序{ 列表顺序=新的ArrayList; PageRequest PageRequest=PageRequest.OfPagenNumber、pageSize、Sort.byorders; Page listPeople=peopleRepository.filtername、rg、mom、cpf、昵称、pageRequest; 返回列表人员; } 我的存储库实现

@服务 公共类PeopleRepositoryImpl实现PeopleRepositoryQueries{ @持久上下文 私人实体经理; @TransactionalreadOnly=true 公共页筛选器字符串名称、字符串rg、字符串mom、字符串cpf、字符串昵称、可分页{ CriteriaBuilder CriteriaBuilder=manager.getCriteriaBuilder; CriteriaQuery查询=criteriaBuilder.CreateQueryOple.class; Root=query.fromPeople.class; Path nomePath=root.getname; 列表谓词=新的ArrayList; 如果!nome.equals{ 谓词nomeIgual=criteriaBuilder.likeomepath,%+name.toUpperCase+%; 谓词addNomegual; } query.wherePredicate[]predicates.toArraynew谓词[0]; int paginaAtual=pageable.getPageNumber; int totalRegistrosPorPagina=pageable.getPageSize; int primeiroRegistro=paginaAtual*总注册量; TypedQuery TypedQuery=manager.createQueryquery; //typedQuery.SetFirstResultPrimereRegistro; //typedQuery.setMaxResultstotalRegistrosPorPagina; 整数totalRows=typedQuery.getResultList.size; Long total=totalRows.longValue; 返回新的PageImpltypedQuery.getResultList,可分页,总计; } 例如,如果我搜索一个名为marcos的人,typedQuery.getResultList只返回10个元素,在我的数据库中,按页面显示的元素数相同,共有180个。如果我删除此项

typedQuery.SetFirstResultPrimereRegistro; typedQuery.setMaxResultstotalRegistrosPorPagina;
然后,typedQuery.getResultList返回180个元素,但有分页,并且所有180个元素都在单个页面中,没有分页

请尝试以下配置

CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
Root<People> entity_ = countQuery.from(query.getResultType());
entity_.alias("entitySub"); //use the same alias in order to match the restrictions part and the selection part
countQuery.select(criteriaBuilder.count(entity_));
Predicate restriction = query.getRestriction();
if (restriction != null) {
    countQuery.where(restriction); // Copy restrictions
}
Long totalCount=entityManager.createQuery(countQuery).getSingleResult();


query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
List<People> result = query.getResultList();
return PageableExecutionUtils.getPage(result,pageable, () -> totalCount);

如果设置FirstResult和MaxResults,则查询应返回案例10中的最大MaxResults元素数。第0页->元素0-9,第1页->元素10-19…第17页->元素170->179。FirstResult是偏移量0,10…170,MaxResults是每页的最大元素数。是的,按页显示最大元素数,但仅显示此项。我在数据库中有120个元素,如果我尝试访问,在json中只返回10个元素totalelements而不返回其他页面。page=1不存在。不确定它是否在原始回答时存在,但PageableExecutions现在已被弃用。据我所知,这将从数据库中提取所有结果,以便计算c而不是执行count*类型的查询。