Created
December 8, 2021 07:42
-
-
Save guozi/6cefae5eb9d83c72103ccfffbed9d9ca to your computer and use it in GitHub Desktop.
使用Pageable将查询结果list转page分页
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListPageUtils { | |
public static void main(String[] args) { | |
List<String> strings = new ArrayList<String>(); | |
for (int i=0;i<20;i++){ | |
strings.add("第"+i+"数据"); | |
} | |
Pageable pageRequest = PageRequest.of(1, 10); | |
Page<String> strings1 = listConvertToPage(strings, pageRequest); | |
System.out.println(strings1); | |
} | |
private static <T> Page<T> listConvertToPage(List<T> list, Pageable pageable) { | |
final int start = (int) pageable.getOffset(); | |
final int end = Math.min((start + pageable.getPageSize()), list.size()); | |
return new PageImpl<T>(list.subList(start, end), pageable, list.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment