持有层代码: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,则不会又告警触发