Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring数据中的分页_Java_Mysql_Spring Data - Fatal编程技术网

Java spring数据中的分页

Java spring数据中的分页,java,mysql,spring-data,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 an

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

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来完成此操作。页面请求(页面,大小)

例如:

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

可以通过基于索引值执行不同的查询来实现这一点

编辑:试试这个技巧,如果你的结果集小于Integer.MAX\u值就可以了

@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));
@Query("从kat_task_属性tp internal join kat_task_to_workstream ttw on ttw.ttw_frn_task id=tp.tp_frn_task id和ttw.ttw_frn_workstream id=:workStreamId和ttw.ttw_ended_by为null,tp.tp_ended_by为null,tp.tp_为true,ttw_为true,ttw_为true;ttw_为true左连接kat_用户_to_to任务id,ttu为truek_to_工作流程_id和kto.uto_frn_user_id=:用户id和订单由tp.tp_完成(kto.uto_订单)
PageFindTP(可分页可分页);
PagingAndSortingRepository存储库=/…获取对bean的访问权限
如果(索引==0)
Page yourObject=repository.findTp(新页面请求(索引,Integer.MAX_值));
其他的
Page yourObject=repository.findTp(新页面请求(索引、大小));

我可以将可分页对象与本机一起使用吗query@Sandesha是的,您可以,但您必须像这样定义计数查询@query(value=“SELECT*FROM USERS wherelastname=?1”,countQuery=“SELECT count(*)FROM USERS wherelastname=?1”,nativeQuery=true)我从未使用过Pageable。如何在我的查询中使用?请帮助我希望这有帮助:)我可以在一个查询符号中使用两个单独的查询吗@lucidi在第一个查询中为tp_completed_添加了一个is null条件,因为你只需要返回那些行,但我不允许使用两个单独的查询。最多我可以按tp_completed_a排序t获取相邻的所有空值
@Query(value = "your existing Query without limit ", countQuery = "countQuery", nativeQuery = true)
getData(@Param("workStreamId") String workStreamId, @Param("userId") String userId, Pageable page);
@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));