Java Spring JPA分页如果超过一页,则无法正常工作
技术问答
219 人阅读
|
0 人回复
|
2023-09-12
|
我正在尝试将军jpa分页与自定义查询一起使用。
+ C% {' y5 E* I' W/ o如果两种情况相匹配,则分页有效。: q, Q {# U. W3 P6 S6 ?, U8 _
情况1 :( r' _8 a+ t0 @$ X! s
如果可用数据少于10个,请求页码为0如果没有,请求不起作用。- [1 n9 I% l+ R- V
和
" m3 j$ s g _' W情况2:
& O2 H4 M0 U$ F: y3 e# r若可用数据大于10,请求页码为1没有分页是行不通的。: m$ p F m1 a( S
我不知道问题所在。有帮助吗?+ d- i, E- x( m) `
提前致谢。, V) C9 ] O; Y8 W- N
@Repositorypublic interface SmsLogRepository extends JpaRepository {@Query(value = "select * from sms_log s where s.phone_number = (:ph) \n#pageable\n",nativeQuery = true)Page findByPhoneNumber(@Param("ph") String phoneNumber,Pageable pageable);@Query(value = "select * from sms_log s where s.phone_number = (:ph) AND s.message_timestamp between (:fromdate) and (:todate) \n#pageable\n",nativeQuery = true)Page findByPhoneNumberAndDate(@Param("ph") String phoneNumber,@Param("fromdate") String fromDate,@Param("todate") String todate,Pageable pageable);@Query(value = "select * from sms_log s where s.message_timestamp between (:fromdate) and (:todate) \n#pageable\n",nativeQuery = true)Page findByDate(@Param("fromdate") String fromDate,@Param("todate") String todate,Pageable pageable);}要求 :
. a! }2 L+ L7 |; ?: d3 h/ |PageRequest pageRequest = new PageRequest(page,10,new Sort(new Sort.Order(Sort.Direction.DESC,"message_timestamp"))); return logRepository.findByPhoneNumber(ph,pageRequest);
6 h2 S6 S6 h1 ]; v& Y' }9 h 解决方案: |
|
|
|
|
|