设置JPA Pageable对象的默认页面大小 [英] Set default page size for JPA Pageable Object
问题描述
我有一个PagingandSorting存储库,该存储库具有接受可分页对象的方法. 我还有一个控制器,可以通过URL接受可分页的对象.
I have a PagingandSorting Repository which has a method that accecpts a pageable object. I also have a controller that accepts a pageable object through the URL.
我的用例是,如果用户在URL中指定页面大小参数,则我必须将该值用作可分页对象.如果他不提及,则将默认值设为50.
My use case is that, if a user specifies a page size parameter in the URL i must take that value for the pageable object. If he does not mention take a default value of 50.
但是可分页对象现在默认为20.
But the pageable object defaults to 20 right now.
任何建议都会帮助
推荐答案
如果您正在谈论Spring Data PagingAndSortingRepository,则可以通过在Controller方法上使用@PageableDefault
来设置默认页面大小,如下所示:
If you are talking about a Spring Data PagingAndSortingRepository you can set the default page size by using the @PageableDefault
on a Controller method as follows:
public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { "surname",
"forename", "address.town" }, value = 50) Pageable pageable) {
}
或者您可以在Spring配置中使用以下配置全局默认值,如下所示在XML和Java配置中.
Or you can configure a global default using the following in your Spring config as shown below in both XML and Java config.
请注意,Spring Data的较新版本使用基于零的页面索引,而较旧的版本将1用于第一页.如果您的UI分页库期望首页为1,则可以将oneIndexedParameters
属性设置为true
:
Note that newer versions of Spring Data use zero based page indexing while older versions used 1 for the first page. If your UI paging library expects 1 as first page then you can set the oneIndexedParameters
property to true
: