获取Spring Boot中页面数量错误

huangapple go评论66阅读模式
英文:

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 = &quot;SELECT DISTINCT s FROM Students s JOIN FETCH s.Teachers WHERE s.Teachers IN (:Teachers)&quot;, countQuery = &quot;SELECT count(DISTINCT s) FROM Students s&quot; +
             &quot; WHERE s.Teachers IN (:Teachers)&quot;)
    Page&lt;Students&gt; findByStd(@Param(&quot;Teachers&quot;) List&lt;Teacher&gt; 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.

huangapple
  • 本文由 发表于 2020年10月1日 17:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64152938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定