如何从 Spring 数据 REST 中的页面获取列表
How to get List from Page in Spring Data REST
我正在使用 JPARespository
进行所有 CRUD 操作。
最近想实现排序,于是直接Pagable
.
问题是,我想要 return List
对象的存储库方法,我在我的服务层中使用它们。
我该如何实现,有没有办法将这些 Page
对象转换为 List
?
如果您在 jpa 存储库方法中使用 pageable,spring 将始终 return 页面而不是列表。我建议你有一个调用存储库方法并将页面结果的内容提取到列表中的服务方法。
因此,如果您的存储库方法是这样的:
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);
}
然后你可以有一个服务class,它有一个调用存储库方法的方法
@Service
public class RecordService{
@Autowired
RecordRepository recordRepository;
public List<Record> findAll(PageRequest pageRequest){
Page<Record> recordsPage = recordRepository.findAll(pageRequest);
return recordsPage.getContent();
}
}
所以在调用 class 时,您可以直接使用该服务,而不是直接调用存储库。因此:
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
}
}
我知道我来晚了,但是,这是它在 google 中弹出的第一个选项,当所有页面的所有信息都需要转换为列表时,人们可能需要更多答案...
一个近似可以是:
xRepository.findAll(new PageRequest(0, Integer.MAX_VALUE)).getContent();
这里的问题是 spring 将最大大小设置为 1000,因此您将获得最多包含 1000 个元素的列表。
另一种方法是使用不同的页面索引进行多次查找,然后使用 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
}
一次获取所有结果的最简单方法是使用 Pageable.unpaged(),如下所示:
Page<Employee> allEmployees = employeeRepository.findAll(Pageable.unpaged());
allEmployees.getContent();
但是如果您担心一次检索太多数据而更喜欢以小块的形式进行检索,您可以使用类似于以下的服务或存储库方法:
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;
}
查看此 page 以了解分块获取数据的优点和缺点。
我正在使用 JPARespository
进行所有 CRUD 操作。
最近想实现排序,于是直接Pagable
.
问题是,我想要 return List
对象的存储库方法,我在我的服务层中使用它们。
我该如何实现,有没有办法将这些 Page
对象转换为 List
?
如果您在 jpa 存储库方法中使用 pageable,spring 将始终 return 页面而不是列表。我建议你有一个调用存储库方法并将页面结果的内容提取到列表中的服务方法。
因此,如果您的存储库方法是这样的:
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);
}
然后你可以有一个服务class,它有一个调用存储库方法的方法
@Service
public class RecordService{
@Autowired
RecordRepository recordRepository;
public List<Record> findAll(PageRequest pageRequest){
Page<Record> recordsPage = recordRepository.findAll(pageRequest);
return recordsPage.getContent();
}
}
所以在调用 class 时,您可以直接使用该服务,而不是直接调用存储库。因此:
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
}
}
我知道我来晚了,但是,这是它在 google 中弹出的第一个选项,当所有页面的所有信息都需要转换为列表时,人们可能需要更多答案...
一个近似可以是:
xRepository.findAll(new PageRequest(0, Integer.MAX_VALUE)).getContent();
这里的问题是 spring 将最大大小设置为 1000,因此您将获得最多包含 1000 个元素的列表。
另一种方法是使用不同的页面索引进行多次查找,然后使用 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
}
一次获取所有结果的最简单方法是使用 Pageable.unpaged(),如下所示:
Page<Employee> allEmployees = employeeRepository.findAll(Pageable.unpaged());
allEmployees.getContent();
但是如果您担心一次检索太多数据而更喜欢以小块的形式进行检索,您可以使用类似于以下的服务或存储库方法:
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;
}
查看此 page 以了解分块获取数据的优点和缺点。