本文介绍了如何在Spring Data JPA中将分页与条件查询相结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PagingAndSortingRepository和findAll(Pageable pageable)方法来分页我的数据。我认为没有办法提供任何条件。例如,有时我想选择和分页地址city = NY。有没有办法同时提供条件和分页?

I use PagingAndSortingRepository and findAll(Pageable pageable) method to paging my data. I think that there is no way to provide any condition. For example sometime I want select and paging addresses where city=NY. Is there any way to provide condition and paging simultaneously?

推荐答案

PagingAndSortingRepository 只需在分页模式中添加非常基本的CRUD方法。正如所述,只需在您定义的任何查询方法中添加 Pageable 参数即可实现此查询的分页。

The PagingAndSortingRepository just adds the very basic CRUD methods in pagination mode. As the reference documentation describes, you can simply add a Pageable parameter to any query method you define to achieve pagination of this query.

interface CustomerRepository implements Repository<Customer, Long> {

  Page<Customer> findByLastname(String lastname, Pageable pageable);

  List<Customer> findByFirstname(String firstname, Pageable pageable);
}

第一种方法将返回 Page 包含页面元数据,如可用的总元素等。要计算此数据,它将触发派生查询的附加计数查询。第二个查询方法返回纯切片结果集,而不执行计数查询。

The first method will return a Page containing the page metadata like total elements available etc. To calculate this data it will trigger an additional count query for the query derived. The second query method returns the plain sliced result set without the count query being executed.

这篇关于如何在Spring Data JPA中将分页与条件查询相结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:56