英文:
Pagination in List Java Spring
问题
我正在进行一个Spring项目,在这个项目中我有Tasks(id,name)和Assignments(task_id,name,...)。当我获取任务时,我同时也获取了分配的任务,但是我无法实现分页系统。
这是我目前的代码:
public List<Tasks> getAllTasks() {
List<Tasks> task = (List<Tasks>) taskRepository.findAll();
return task;
}
这是我目前的代码。我想在每个分页中显示10个项目。
我尝试了类似这样的代码:
public Page<Tasks> getAllTasks(Long page, Long size) {
List<Tasks> tsk = (List<Tasks>) taskRepository.findAll();
int start = new PageRequest(page.intValue(), size.intValue()).getOffset();
int end = (start + new PageRequest(page.intValue(), size.intValue()).getPageSize()) > tsk.size() ? tsk.size() : (start + new PageRequest(page.intValue(), size.intValue()).getPageSize());
return new PageImpl<Tasks>(tsk.subList(start, end), new PageRequest(page.intValue(), size.intValue()), tsk.size());
}
但是它没有起作用。
我的当前JSON响应如下:
{
id: 1,
name: 第一个任务
assignments: [
{
id: 1,
assignment_name: 第一个分配
},
{
id: 2,
assignment_name: 第二个分配
}]
}
我还看到不一定需要使用List。我对任何想法都持开放态度。
英文:
I'm working on a Spring project where I have Tasks (id, name) and Assignments (task_id, name, ...). When I get the tasks I also get assigned task, but I was not able to implement a pagination system.
public List<Tasks> getAllTasks() {
List<Tasks> task = (List<Tasks>) taskRepository.findAll();
return task;
}
That's my current code. I want to display 10 items in each paginated side.
I tried something like that:
public Page<Tasks> getAllTasks(Long page, Long size){
List<Tasks> tsk = (List<Tasks>) taskRepository.findAll();
int start = new PageRequest(page, size).getOffset();
int end = (start + new PageRequest(page, size).getPageSize()) > tsk.size() ? tsk.size() : (start + new PageRequest(page, size).getPageSize());
return new PageImpl<Tasks>(tsk.subList(start, end), new PageRequest(page, size), tsk.size());
}
but it's not working.
My current JSON response is as follows:
{
id: 1,
name: First Task
assignments: [
{
id: 1,
assignment_name: firstassignment
},
{
id: 2,
assignment_name: secondassignment
}]
}
I also read that it doesn't need to be a List. I'm open for any ideas.
答案1
得分: 7
你可以在findAll
的参数中创建并发送pageable
以获取数据。
public Page<Tasks> getAllTasks(int page, int size){
Pageable pageable = PageRequest.of(page, size);
return taskRepository.findAll(pageable);
}
关于Spring Data JPA中分页的优质文章在这里。
英文:
You can create and send pageable
as a parameter in findAll
to get the data
public Page<Tasks> getAllTasks(int page, int size){
Pageable pageable = PageRequest.of(page, size);
return taskRepository.findAll(pageable);
}
A good article about Pagination in Spring Data JPA here
答案2
得分: 0
public List<Tasks> getAllTasks(Long page, Long size) {
Pageable taskPageable = new PageRequest.of(page, size);
return taskRepository.findAll(taskPageable);
}
We can also directly get `List` object instead of `Page` object.
英文:
public List<Tasks>getAllTasks(Long page, Long size) {
Pageable taskPageable = new PageRequest.of(page, size);
return taskRepository.findAll(pageable);
}
We can also directly get List
object instead of Page
object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论