首页 > 解决方案 > 我真的需要在原生 @Query 中使用 countQuery 进行分页吗?

问题描述

根据参考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-jpapaginationspring-data

解决方案


推荐阅读