java实现注解分页,spring data jpa 中@Query注解分页pageable查询

持有层代码:Repository

@Query(value = "select * from user_liveapp where user_Id = :userId ORDER BY ?#{#pageable}", nativeQuery = true)

Pagesearch(@Param("userId") Long userId, Pageable pageable);

@Query(value = "select * from user_liveapp where user_Id = :userId ORDER BY ?#{#pageable}",

countQuery="select count(*) from user_liveapp where user_Id = :userId",

nativeQuery = true)

Pagesearch1(@Param("userId") Long userId, Pageable pageable);

使用这两种写法都可以查询时分页。

第一种写法,如果pageable中的page is not lastpage, 后台日志会抛出警告:

2018-07-13 10:32:37.122 [main] WARN at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.potentiallyBindExpressionParameters(SpelExpressionStringQueryParameterBinder.java:108) - Setting the parameter with name 'null' and position '1' lead to an exception.

java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502)

at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692)

at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181)

at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)

……

Hibernate: select count(*) from user_liveapp where user_Id = ?Hibernate: select count(*) from user_liveapp where user_Id = ?

第二种写法,如果pageable中的page is not lastpage, 后台日志会抛出警告:

2018-07-13 10:32:37.122 [main] WARN at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.potentiallyBindExpressionParameters(SpelExpressionStringQueryParameterBinder.java:108) - Setting the parameter with name 'null' and position '1' lead to an exception.

java.lang.IllegalArgumentException: Parameter with that position [2] did not exist

at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502)

at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692)

at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181)

at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)

……

Hibernate: select count(*) from user_liveapp where user_Id = ?

不影响查询结果,是spring data jpa本身处理抛出问题.

如果pageable中的page is  lastpage,并且查询结构size小于pageable中size,则不会又告警触发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值