在Spring Boot JPA中使用嵌套循环进行分页。

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

Pagination in Spring Book JPA with Nested loops

问题

我正在尝试实现分页功能。我有两个表,一个是教师表,另一个是学生表。对于每个教师,都有多个学生。现在我有一个教师姓名的列表,对于每个教师,我都必须找出其所有的学生,并将它们添加到列表中,最后将其返回。我还会得到页码和大小作为输入。

List<StudentDTO> studentDTOList = new ArrayList<>();

List<TeacherEntity> teachers = TeacherRepo.findByName(TeacherNames);
teachers.forEach(teacher -> {
    Pageable pageable = PageRequest.of(pageNumber, pageSize); // 使用页码和大小创建分页请求
    Page<Student> studentPage = studentRepo.findByTeacherName(teacher, pageable);
    List<Student> students = studentPage.getContent(); // 获取当前分页的学生列表
    studentDTOList.addAll(students);
});

return studentDTOList;

我知道如果只查询教师库的话如何实现分页功能。但是在存在嵌套查询的情况下,我不太清楚如何实现。

英文:

I am trying to implement Pagination. I am having two tables, a teacher and a student. For each teacher, there are multiple students. Now I got a list of TeacherNames and for each teacher, I have to find all its students and add them to the list, and finally return it. I am getting page number and size also as input.




List&lt;TeacherEntity&gt; teacher = TeacherRepo.findByName(TeacherNames);
Teachers.forEach(teacher - &gt; {
    List&lt;Student&gt; students = studentRepo.findByTeacherName(teacher);
    studentDTO.addAll(students);
});

return StudentDTO;

I know how to implement pagination if I had to query only in teachersRepo. But I am not able to get how will I do it when there is nesting.

答案1

得分: 1

我假设在学生和教师之间存在@ManyToMany关系。如果没有,请在继续之前创建这种关系。以下是创建关系的示例代码:

public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long teacherId;
    
    //一些字段
    
    @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(name = "teacher_student", joinColumns = {
            @JoinColumn(name = "teacherId", referencedColumnName = "teacherId") }, inverseJoinColumns = {
                    @JoinColumn(name = "studentId", referencedColumnName = "studentId") })

    @BatchSize(size = 20)
    private Set<Student> students = new HashSet<>();
    //获取和设置方法
}

现在,在教师存储库中,添加一个方法来在学生和教师之间建立连接:

@Repository
public interface TeacherRepo extends JpaRepository<Teacher, Long> {
    
    @Query(value="SELECT t.teacherName,s.studentName FROM Teacher t left join t.students s")
    public ArrayList<User> findByStudent(Pageable paging);
}

在你的服务类中调用这个存储库方法:

public ArrayList<Teacher> findByStudent() 
{
    // TODO Auto-generated method stub
    Pageable paging = PageRequest.of(0, 4);
    return teacherRepo.findByStudent(paging);
}

这样你的分页查询将返回4条学生记录。

注意:你可以根据需要添加where条件、分页和排序详细信息。

英文:

I am assuming that you have @ManyToMany relationship between student and teacher. If not, you have to create one before you proceed. Sample code to create relation

public class Teacher
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long teacherId;
    
    //some fields
    
    @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(name = &quot;teacher_student&quot;, joinColumns = {
            @JoinColumn(name = &quot;teacherId&quot;, referencedColumnName = &quot;techerId&quot;) }, inverseJoinColumns = {
                    @JoinColumn(name = &quot;studentId&quot;, referencedColumnName = &quot;studentId&quot;) })

    @BatchSize(size = 20)
    private Set&lt;Stident&gt; students = new HashSet&lt;&gt;();
//Get and set
}

Now, in teacher repo, add a method having join between student and teacher

@Repository
public interface TeacherRepo extends JpaRepository&lt;Teacher, Long&gt;
{   
    @Query(value=&quot;SELECT t.teacherName,s.studentName FROM Teacher t left join t.students s&quot;)
    public ArrayList&lt;User&gt; findByStudent(Pageable paging);
}

Call this repo method in your sevice class

public ArrayList&lt;Teacher&gt; findByStudent() 
    {
        // TODO Auto-generated method stub
        Pageable paging = PageRequest.of(0, 4);
        return teacherRepo.findByStudent(paging);
    }

This way you pagination will return 4 students records.

Note - You can add where conditions, paginations, sorting details according to your requirement

huangapple
  • 本文由 发表于 2020年9月26日 00:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068236.html
匿名

发表评论

匿名网友

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

确定