Java 如何在 Spring Mvc Jpa 中将实体对象列表转换为页面对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37136679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:03:20  来源:igfitidea点击:

How to convert a List of enity Object to Page Object in Spring Mvc Jpa?

javaspring-mvcpaginationspring-data

提问by briantaurostack7

I have a Listof entities. How do I convert it to PageObject using Spring MVC 4 and Spring Data JPA?

我有一个List实体。如何Page使用 Spring MVC 4 和 Spring Data JPA将其转换为Object?

采纳答案by briantaurostack7

There is a Pageimplementationfor that:

有一个Page实现

final Page<Something> page = new PageImpl<>(theListOfSomething);

回答by Mehraj Malik

There is one more Constructor:

还有一个构造函数

Page<FOO> page = new PageImpl<>(listOfsomething, pageable, listOfsomething.size());

回答by smndiaye

I think you will need to fetch the correct page content as well.

我认为您还需要获取正确的页面内容。


PageRequest pageRequest = PageRequest.of(offset, limit);

List<Product> products = getProducts();

int total = products.size();
int start = toIntExact(pageRequest.getOffset());
int end = Math.min((start + pageRequest.getPageSize()), total);

List<Product> output = new ArrayList<>();

if (start <= end) {
    output = products.subList(start, end);
}

return new PageImpl<>(
    output,
    pageRequest,
    total
);