【问题标题】:spring-data-jpa pagination returing null values on third pagespring-data-jpa 分页在第三页上返回空值
【发布时间】:2015-12-05 21:06:32
【问题描述】:

在我的 Spring MVC 应用程序中,我将 spring-data-jpa 与 MySql 数据库一起使用。 对于试图实现分页的数据表。

像这样

表上有191条记录,'id'是主键——从10、11、12开始的值----199、200。

我的每个页面包含假设 10 条记录。

以下是我用来从表中仅获取 10 条记录的代码。

Pageable pageable = new PageRequest(firstResult, maxResults);   
Page<User> findAll2 = repository.findAll(pageable);

对于第 1 页 -> firstResult = 1 和 maxResults = 10 Pageable pageable = new PageRequest(1, 10);
这里我得到 10 条记录,id 20 到 29 但我期待从 10 到 19

对于第 2 页 -> firstResult = 11 和 maxResults = 10 Pageable pageable = new PageRequest(11, 10);
这里我得到 10 条记录,id 120 到 129 其他记录呢。

现在 对于第 3 页 ->firstResult = 21 和 maxResults = 10 可分页可分页 = new PageRequest(21, 10);
在这里我没有得到任何记录。

          All values are null.Not sure what I am missing.please help me on this.

【问题讨论】:

    标签: mysql spring jpa


    【解决方案1】:

    第一个数字是page 而不是firstResult。所以你应该从

    Pageable pageable = new PageRequest(0, 10);   
    

    然后:

    Pageable pageable = new PageRequest(1, 10);
    

    等等。

    当你说

    Pageable pageable = new PageRequest(21, 10);   
    

    你要的是第 21 页,而你没有,所以它返回 null。

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2021-02-24
      • 2018-01-27
      • 2020-07-01
      • 2017-04-04
      • 1970-01-01
      • 2016-02-01
      • 2014-12-11
      • 2012-05-25
      相关资源
      最近更新 更多