获取Spring存储库中的Pageable自定义查询的总计数行数

时间:2018-01-30 07:58:05

标签: java pagination spring-data-jpa spring-repositories

我实现了这样的分页:

List<Products> products = productRepository.findAllProducts(productsRequest.getInitiatorType(), "ACTIVE",
      new PageRequest(page, 100, Sort.Direction.DESC, "insertDate"));

但是如何获得此查询的总大小?只复制这样的查询?

  @Query("SELECT t FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  List<OpcClProducts> findAllOpcClProducts(String senderId, String status, Pageable pageable);


  @Query("SELECT COUNT(t) FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  long getTotalCount(String senderId, String status);

并调用2个查询:for totalCount和data?

1 个答案:

答案 0 :(得分:10)

尝试将对象更改为

Page<Products> p .. 

p应该包含您要查找的信息。 :) (因为他们想让我发布在这里) ^^&#39;