Java 如何在 Spring Data REST 中从页面获取列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38809580/
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
How to get List from Page in Spring Data REST
提问by Soumitri Pattnaik
I am using JPARespository
for all my CRUD operation.
Recently I wanted to implement sorting, so I went ahead with Pagable
.
我正在使用JPARespository
我的所有 CRUD 操作。最近想实现排序,所以继续用Pagable
.
The problem is, I want the repository methods to return List
objects, I use them in my service layer.
问题是,我希望存储库方法返回List
对象,我在我的服务层中使用它们。
How can I achieve this, is there a way to convert these Page
objects to List
?
我怎样才能做到这一点,有没有办法将这些Page
对象转换为List
?
采纳答案by Sohlowmawn
If you use pageable in a jpa repository method, spring will always return a Page not a List. I suggest you have a service method that calls the repository method and extracts the contents of the Page result into a list.
如果在 jpa 存储库方法中使用 pageable,spring 将始终返回 Page 而不是 List。建议你有一个service方法,调用repository方法,将Page结果的内容提取到一个列表中。
So if your repository method is thus:
因此,如果您的存储库方法是这样的:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RecordRepository extends JpaRepository<Record, Integer>{
Page<Record> findAll(Pageable pageable);
}
then you can have a service class which has a method that calls the repository method
那么你可以有一个服务类,它有一个调用存储库方法的方法
@Service
public class RecordService{
@Autowired
RecordRepository recordRepository;
public List<Record> findAll(PageRequest pageRequest){
Page<Record> recordsPage = recordRepository.findAll(pageRequest);
return recordsPage.getContent();
}
}
so in your calling class instead of calling the repository directly you can just use the service. thus:
所以在你的调用类中,而不是直接调用存储库,你可以只使用该服务。因此:
public class MyRecordImpl{
@Autowired
RecordService recordService;
public void doSomething(){
int page = 0; int pageSize = 5;
List<Record> recordList = recordService.findAll(new PageRequest(page, pageSize, new Sort(Sort.Direction.DESC, "recordId")));
//do other implementations here
}
}
回答by Noki
I know I am late, but, this is the first option it pops in google and people may need more answers when all the information of all pages needs to be converted into a list...
我知道我迟到了,但是,这是它在 google 中弹出的第一个选项,当所有页面的所有信息都需要转换为列表时,人们可能需要更多答案......
One aproximation can be:
一种近似可以是:
xRepository.findAll(new PageRequest(0, Integer.MAX_VALUE)).getContent();
The problem here is that spring sets the maximum size to 1000 so you are going to get a list of maximum 1000 elements.
这里的问题是 spring 将最大大小设置为 1000,因此您将获得最多 1000 个元素的列表。
An other way of doing it can be multiple find with diferent page indexes and then adding the results into a list with getContent():
另一种方法是使用不同的页面索引进行多次查找,然后使用 getContent() 将结果添加到列表中:
Page<T> pageData = xRepository.findAll(new PageRequest(0, 20));
List<T> finalList = pageData.getContent();
while(!pageData.isLast()){
pageData = xRepository.findAll(pageData.nextPageable());
List<T> listData = pageData.getContent();
//append listData into finalList
}
回答by Chaithu Narayana
The simplest way to fetch all your results at once is to use Pageable.unpaged(), like below:
一次获取所有结果的最简单方法是使用Pageable.unpaged(),如下所示:
Page<Employee> allEmployees = employeeRepository.findAll(Pageable.unpaged());
allEmployees.getContent();
But if you are concerned about retrieving too much data at once and instead prefer to do the retrieval in small chunks, you can use a service or repository method similar to below:
但是,如果您担心一次检索太多数据而更喜欢以小块进行检索,则可以使用类似于以下的服务或存储库方法:
private List<Employee> findAll() {
List<Employee> allEmployees = new ArrayList<>();
// First page of employees -- 5000 results per page
PageRequest pageRequest = PageRequest.of(0, 5000);
Page<Employee> employeePage = employeeRepository.findAll(pageRequest);
allEmployees.addAll(employeePage.getContent());
// All the remaining employees
while (employeePage.hasNext()) {
Page<Employee> nextPageOfEmployees = employeeRepository.findAll(employeePage.nextPageable());
allEmployees.addAll(nextPageOfEmployees.getContent());
// update the page reference to the current page
employeePage = nextPageOfEmployees;
}
return allEmployees;
}
Check this pagefor advantages and disadvantages on fetching the data in chunks.
检查此页面以了解分块获取数据的优缺点。