【问题标题】:Get total count rows for Pageable custom query in Spring repository获取 Spring 存储库中可分页自定义查询的总行数
【发布时间】:2018-07-09 01:11:36
【问题描述】:

我这样实现分页:

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 查询:对于 totalCount 和对于数据?

【问题讨论】:

  • 尝试将您的对象更改为 Page
  • 当您使用它时,将实体重命名为Product。一个实例就是一个产品。字符串不命名为字符串,因为每个实例都是一个字符串。
  • @andrewTobilko 谢谢你提醒我!我不是 100% 确定 ^^'
  • @ip696 看看 Noixes 提供的答案。它解决了问题吗?
  • 所以有一个大脑和我的想法一样。

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


【解决方案1】:

尝试将您的对象更改为

Page<Products> p .. 

p 应该有您正在寻找的信息。 :) (在这里发布是因为他们希望我这样做) ^^'

【讨论】:

  • 我没有检查过,但我相信这是正确的答案。 +1
  • page.getTotalElements() 将为您提供查询 page.getTotalPages() 返回的行数与页面大小相关的页面数(您配置的每页项目)
  • 在我的搜索结果中排在首位的绝对有用的信息...
  • 使用spring boot data jdbc,如果你的接口扩展PagingAndSortingRepository并且查询返回多个结果,你得到org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 50
猜你喜欢
  • 2015-10-29
  • 2016-08-29
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 2015-02-02
  • 2020-01-07
  • 2021-04-01
相关资源
最近更新 更多