Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

java - spring data jpa limit pagesize, how to set to maxSize

I have one requirement is to search by pageable and non-pageable,

and in my Java code, I use spring data jpa Pageable class,

 Pageable pageable = new PageRequest(
                    queryForm.getPageNumber()- 1, queryForm.getPageSize(),Sort.Direction.ASC,"id");  
Page page = repository.fullTextSearch(queryForm.getText(), pageable);

And I don't want to change the return structure,

So when non-pageable situation (search all), how to set the pageSize value to MAX ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In conjunction with Spring MVC you can use PageableDefaults annotation with value = Integer.MAX_VALUE like

public String showUsers(Model model, 
  @PageableDefaults(pageNumber = 0, value = Integer.MAX_VALUE) Pageable pageable) { … }

see PageableDefaults annotation Javadoc.

In any other client code you can set second constructor parameter to Integer.MAX_VALUE:

new PageRequest(
    queryForm.getPageNumber()- 1, 
    queryForm.getPageSize() == null ? Integer.MAX_VALUE : queryForm.getPageSize(),
    Sort.Direction.ASC,"id"); 

see PageRequest constructor. I assume that queryForm.getPageSize() is a wrapper type not a primitive. Otherwise you get a zero if pageSize wasn't set by the user (intentionally for a "search all" request).

UPDATE:

Since Spring Data Commons 1.6 you should use PageableDefault instead of PageableDefaults

public String showUsers(Model model, 
    @PageableDefault(page= 2 ,value = Integer.MAX_VALUE)

See PageableDefault annotation Javadoc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...