springdatajpa 分页pageable 不能用呢

为了那份宁静 为了那份宁静 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天试了一下springdatajpa 的带查询参数的分页功能,如果像官网给出的例子,使用本地sql 就报错,官方的例子如下
Note, that we currently don’t support execution of dynamic sorting for native queries as we’d have to manipulate the actual query declared and we cannot do this reliably for native SQL. You can however use native queries for pagination by specifying the count query yourself:

Example 50. Declare native count queries for pagination at the query method using @Query
public interface UserRepository extends JpaRepository<User, Long> {

@Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
nativeQuery = true)
Page<User> findByLastname(String lastname, Pageable pageable);
}
This also works with named native queries by adding the suffix .count to a copy of your query. Be aware that you probably must register a result set mapping for your count query, though.


我本地是这样写的。
@Query(value = "select u from UserInfo u where u.name=?1",
countQuery = "select count(*) from UserInfo u where u.name=?1",
nativeQuery = true)
Page<UserInfo> findByNameLike(String name,Pageable pageable);

运行就出错了。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appliation': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.it.core.repository.UserRepository com.it.core.web.Appliation.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework.data.domain.Page com.it.core.repository.UserRepository.findByNameLike(java.lang.String,org.springframework.data.domain.Pageable)
...全文

相关推荐

springDataJPA:弹簧数据-jpa

弹簧数据JPA 弹簧数据-jpa

笨猫猪

SpringDataJPA学习

SpringDataJPA学习

阳光灿烂999

springdatajpa源码.rar

《SpringDataJpa系列教程》01-初体验(基于springboot2.0.3+MySQL5.7)源码文件

建行一世

JPA分页查询与条件分页查询

JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件分页查询JPA分页查询与条件

计算机小陈

keesun_springdatajpa

keesun_springdatajpa

皮卡学长
评论 12
收藏 已收藏
分享
返回顶部
为了那份宁静
Java EE 67,535 成员 225,843 内容