我真的需要在原生 @Query 中使用 countQuery 来进行分页吗?

问题描述 投票:0回答:1

根据参考,本机查询分页需要

countQuery
(至少有一个示例)。但根据我的测试(Spring Boot 2.4.5),对于此类查询,@Query 分页无需
countQuery
即可工作:

public interface ARepository extends JpaRepository<A, Long>{
  @Query(value = "select * from a", nativeQuery = true)
  Page<A> findAllANative(Pageable pageable);

  @Query(value = "select count(a.id) as aCount, category.name as 
        categoryName " +
        "from a " +
        "join category on a.category_id=category.id " +
        "group by category.name",
        nativeQuery = true)
  List<CountView> findACountByCategoryNative(Pageable pageable);
}

如您所见,没有

countQuery
并且它们可以工作。

分页仅适用于这些特定查询,还是我可以在所有情况下省略

countQuery

spring-data-jpa pagination spring-data
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.