英文:
Getting wrong number of pages in Spring Boot
问题
我正在使用Spring Boot进行分页。我的列表中有32条记录。我尝试以每页5条的页面大小来获取这些列表。当我从0到3检查页面时,每页的元素数是正确的(每页5个元素)。但是当我检查最后一页时,列表中的元素少了一个(最后一页上有1个元素,但应返回2个),而且元素的总数减少了一个。
@Query(value = "SELECT DISTINCT s FROM Students s JOIN FETCH s.Teachers WHERE s.Teachers IN (:Teachers)",
countQuery = "SELECT count(DISTINCT s) FROM Students s" +
" WHERE s.Teachers IN (:Teachers)")
Page<Students> findByStd(@Param("Teachers") List<Teacher> Teacher, Pageable pageable);
英文:
I am doing paging in spring boot. I am having 32 records on a list. I am trying to get these lists in pages with a page size of 5. When I check for pages from 0 to 3, I am getting correct total elements(5 on each page). But when I check for the last page, it is giving me one less element in the list(1 element on the last page, but it should return 2) and also, the total count of elements is reduced by one.
@Query(value = "SELECT DISTINCT s FROM Students s JOIN FETCH s.Teachers WHERE s.Teachers IN (:Teachers)", countQuery = "SELECT count(DISTINCT s) FROM Students s" +
" WHERE s.Teachers IN (:Teachers)")
Page<Students> findByStd(@Param("Teachers") List<Teacher> Teacher, Pageable pageable);
答案1
得分: 1
SELECT DISTINCT s
FROM Students s JOIN FETCH s.Teachers
WHERE s.Teachers IN (:Teachers)
SELECT count(DISTINCT s)
FROM Students s
WHERE s.Teachers IN (:Teachers)
让我们看一下:
FROM Students s JOIN Teachers
与
FROM Students s WHERE s.Teachers IN (:Teachers)
是不同的,因此 pageNumber 是错误的。
英文:
SELECT DISTINCT s
FROM Students s JOIN FETCH s.Teachers
WHERE s.Teachers IN (:Teachers)
SELECT count(DISTINCT s)
FROM Students s
WHERE s.Teachers IN (:Teachers)
Let's see
FROM Students s JOIN Teachers
is different with
FROM Students s WHERE s.Teachers IN (:Teachers)
thereforce, pageNumber is wrong.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论