【问题标题】:Load More functionality using Spring JPA使用 Spring JPA 加载更多功能
【发布时间】:2019-05-26 06:40:54
【问题描述】:

我想在我的网站中启用“加载更多”功能。 由于我使用 Spring JPA 从远程数据库获取数据,我想知道如何在 Spring 中实现这样的功能?

基本上,我想做的是在第一次 REST 调用时只加载 100 条记录。如果用户点击“加载更多”,那么我会从我停止的最后一个索引(意味着记录 101-200)中再调用 100 条记录,依此类推。

如果需要,我可以通过 HTTP POST 发送开始和结束索引。这是我的存储库:

@Repository
public interface IStatusRepository extends CrudRepository<StatusDAO,String> {

    List<StatusDAO> getStatusByIdAndFqhnIn(String id, List<String> filteredHostList);

}

你有什么建议吗?

【问题讨论】:

    标签: java mysql spring-boot spring-data-jpa


    【解决方案1】:

    您可以使用Pageable 或PagingAndSortingRepository&lt;T, ID&gt; 来满足您的要求。 通过扩展PagingAndSortingRepository,,我们得到了用于分页和排序的findAll(Pageable pageable) 和findAll(Sort sort) 方法。

    一旦我们的存储库从 PagingAndSortingRepository 扩展而来,我们只需要:

    1. 创建或获取一个PageRequest对象,它是一个实现 可分页界面
    2. 将 PageRequest 对象作为参数传递给存储库方法 我们打算使用

    例子:

    public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
    
        List<Product> findAllByPrice(double price, Pageable pageable);
    }
    

    Pageable firstPageWithTwoElements = PageRequest.of(0, 2);
    
    Pageable secondPageWithFiveElements = PageRequest.of(1, 5);
    

    Page<Product> allProducts = productRepository.findAll(firstPageWithTwoElements);
    
    List<Product> allTenDollarProducts = 
      productRepository.findAllByPrice(10, secondPageWithFiveElements);
    

    Paging and Sorting DocRefer 1Refer 2

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 2011-12-25
      • 2019-06-02
      • 2020-05-12
      • 2021-09-08
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多