【问题标题】:Spring batch - Using mybatis pagination on union operationSpring Batch - 在 union 操作上使用 mybatis 分页
【发布时间】:2018-09-20 10:04:27
【问题描述】:

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

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

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

例如-

<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>

【问题讨论】:

  • 请显示std表的定义以及查询到Student(我假设)类的mybatis映射。
  • 感谢您的快速响应,我无法从我的应用程序中获取相同的 sql,请理解问题,如果您有任何问题,请询问我。
  • 你不需要展示真实的表格和真实的映射。重现该问题的综合示例甚至更好(请参阅stackoverflow.com/help/mcve)。没有这些信息,问题就很难定义。

标签: spring oracle spring-batch mybatis


【解决方案1】:

查询结果的每条记录都应代表一个项目(在您的情况下为 Student)。您的项目阅读器应该能够返回一个完整的学生项目及其所有子记录(显然是来自您的查询的 Credit 记录)。否则分页将不会返回正确的结果,原因很明显。

您需要的是Driving Query Pattern:您的读者只能读取学生(没有子记录),然后处理器将完成每个学生的子记录(基本上是当前项目的联合查询的结果) .使用这种方法,分页只会对学生起作用,而不管每个项目有多少子记录。

希望这会有所帮助。

【讨论】:

  • 感谢 Hassine 的回复。实际上,问题在于我们的数据库调用非常昂贵。执行需要很多时间。仅选择一次 ID 并在处理器中获取结果将非常耗时。请指教。
猜你喜欢
  • 2019-08-17
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
相关资源
最近更新 更多