分页在Java Spring的列表中

huangapple go评论91阅读模式
英文:

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&lt;Tasks&gt; getAllTasks() {
   	List&lt;Tasks&gt; task = (List&lt;Tasks&gt;) 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&lt;Tasks&gt; getAllTasks(Long page, Long size){
       List&lt;Tasks&gt; tsk = (List&lt;Tasks&gt;) taskRepository.findAll();
     
   	int start =  new PageRequest(page, size).getOffset();
       int end = (start + new PageRequest(page, size).getPageSize()) &gt; tsk.size() ? tsk.size() : (start + new PageRequest(page, size).getPageSize());

       return new PageImpl&lt;Tasks&gt;(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&lt;Tasks&gt; 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&lt;Tasks&gt;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.

huangapple
  • 本文由 发表于 2020年10月8日 21:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64263107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定