Spring batch - 在 union 操作上使用 mybatis 分页

Spring batch - Using mybatis pagination on union operation

在使用联合查询时,有没有更好的方法来处理 mybatis 分页? 不要认为这是一个强大的用例,而只是代表我的实际问题。

我需要合并才能从 2 个不同的表中获取记录。

这里的问题是,如果我将pageSize设置为100,例如,如果10个学生每人有20条记录,那么即使有200条记录,我也只能得到100条记录。在下面的示例中 class,当我打印每个学生拥有的记录数时,我不会看到所有记录。

例如-

<code>
with student AS (
select * from std (
 select studentId, name, class, ROW_NUMBER() over (order by studentId) as paginationRank from Student
)std
where paginationRank > #{ _skipRows} and paginationRank <= ( #{_pageSize} * 
(#{_page}+1))
)

select student.studentId, attendanceRegfields......Creditsfields....
from student left outer join 
( select ..... from attendanceReg
   union all 
  select  .... from Credits ) all_records
on all_records.studentId = student.studentId
</code>

在我的项目编写器中,如果我得到所有学生记录

<code>
  class MyItemWriter extends ItemWriter<Student>
    {

        write(List<student> studentRecords){
             Map<String, List<Student>> studentRecordsMap = 
        studentRecords.stream().collect(groupby(e-> e.getStudentId()));
      studentRecordsMap .forEach((key, studentRecords) -> process(stuedntRecords);

    }

process(List<Student> studentRecords){
// here I am processing all records
}
    }
</code>

查询结果的每条记录都应代表一个项目(在您的例子中是 Student)。您的项目 reader 应该能够 return 一个完整的学生项目及其所有 child 条记录(显然是您查询中的 Credit 条记录)。否则,由于显而易见的原因,分页将不会 return 正确的结果。

你需要的是 Driving Query Pattern:你 reader 只能读取学生(没有 child 记录),然后处理器将完成每个学生 child 记录(基本上是对当前项的联合查询的结果)。使用这种方法,无论每个项目有多少 child 条记录,分页都将只对学生有效。

希望对您有所帮助。