春天的分页 - 数据

时间:2017-03-24 08:15:11

标签: java mysql spring-data

我在使用分页查询数据库时有一个要求。 在分页时,我将为查询提供页面大小和页面索引,如下所示,

select distinct tp.* from kat_task_property tp inner join kat_task_to_workstream ttw on ttw.ttw_frn_task_id = tp.tp_frn_task_id and ttw.ttw_frn_workstream_id= :workStreamId and ttw.ttw_ended_by is null and tp.tp_ended_by is null and tp.tp_is_active=true and ttw.ttw_is_active=true left join kat_user_to_task_order kto on ttw.ttw_id = kto.uto_frn_task_to_workstream_id and kto.uto_frn_user_id = :userId order by tp.tp_completed_at ,kto.uto_order limit :index, :size

样本结果将是,

tp_id      tp_completed_at
 1          2017-02-27 06:47:52
 2          null
 3          null
 4           2017-03-14 12:59:24
 5          null
 6          null
 7          null

我的要求是当查询中的索引为0时,我应该得到tp_completed_at为null的所有数据,而不管查询中的值大小。我的意思是,当index为零时不应该应用分页,我应该获得tp_completed_at的所有条目为null。并且,当索引具有除0以外的值时,应该应用分页。请帮忙

2 个答案:

答案 0 :(得分:0)

你可以使用Pageable来做到这一点。 PageRequest(页面,大小)

Pageable page = new PageRequest(0, 10);

例如:

Page<Incident> findByProblemId(Long problemId, Pageable page);

在你的情况下, countQuery =&#34; .... count(distinct tp。*)...无限制&#34;

@Query(value = "your existing Query without limit ", countQuery = "countQuery", nativeQuery = true)
getData(@Param("workStreamId") String workStreamId, @Param("userId") String userId, Pageable page);

答案 1 :(得分:0)

您可以通过基于索引值执行不同的查询来实现此目的。

编辑:尝试此hack,如果结果集小于Integer.MAX_VALUE

,则运行
@Query("select distinct tp.* from kat_task_property tp inner join kat_task_to_workstream ttw on ttw.ttw_frn_task_id = tp.tp_frn_task_id and ttw.ttw_frn_workstream_id= :workStreamId and ttw.ttw_ended_by is null and tp.tp_ended_by is null and tp.tp_is_active=true and ttw.ttw_is_active=true left join kat_user_to_task_order kto on ttw.ttw_id = kto.uto_frn_task_to_workstream_id and kto.uto_frn_user_id = :userId and order by tp.tp_completed_at, kto.uto_order")
Page<T> findTp(Pageable pageable);

PagingAndSortingRepository<YourObject, Long> repository = // … get access to a bean
if(index == 0)
    Page<YourObject> yourObject= repository.findTp(new PageRequest(index, Integer.MAX_VALUE));
else
    Page<YourObject> yourObject= repository.findTp(new PageRequest(index, size));
相关问题