[英]Pagination in 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的所有数据。 我的意思是,当索引为零时不应应用分页,并且我应该获得tp_completed_at为null的所有条目。 并且,当索引的值不是0时,应使用分页。 请帮忙
您可以使用Pageable来做到这一点。 PageRequest(页面大小)
Pageable page = new PageRequest(0, 10);
例如 :
Page<Incident> findByProblemId(Long problemId, Pageable page);
在您的情况下,countQuery =“ .... count(distinct tp。*)...没有限制”
@Query(value = "your existing Query without limit ", countQuery = "countQuery", nativeQuery = true)
getData(@Param("workStreamId") String workStreamId, @Param("userId") String userId, Pageable page);
您可以通过基于索引值执行不同的查询来实现此目的。
编辑:尝试此技巧,如果您的结果集小于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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.