2017-03-24 65 views
0

我在使用分頁查詢數據庫時有要求。 雖然分頁,我給頁面大小和頁索引到查詢如下所示,春季分頁數據

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爲空的所有數據。我的意思是,當索引爲零時,不應該使用分頁,我應該使用tp_completed_at獲取所有條目爲空。而且,如果指數具有0以外的值,則應該應用分頁。請幫助

回答

0

您可以使用Pageable來做到這一點。 PageRequest(頁面大小)

Pageable page = new PageRequest(0, 10); 

例如:

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

在你的情況, countQuery = 「... ...數沒有限制(不同TP *)。」

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

我可以使用可分頁對象與本地查詢 – Virat

+0

@Sandesha當然可以,但你必須定義計數這樣的查詢@Query(value =「SELECT * FROM USERS WHERE LASTNAME =?1」, countQuery =「SELECT count(*)FROM USERS WHERE LASTNAME =?1」, nativeQuery = true) – lucid

+0

我從來沒有使用過Pageable。如何在我的查詢中使用?請幫助 – Virat

0

您可以通過執行基於索引值的不同查詢來實現此目的。

編輯:嘗試這個技巧,作品,如果你的結果集小於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)); 
+0

我在第一個查詢中添加了tp_completed_at爲空的條件,因爲您只需要返回那些行 – Anurag

+0

但我不允許使用兩個單獨的查詢。至多我可以通過tp_completed_at命令來獲取彼此相鄰的所有空值 – Virat