使用JPA进行Spring Boot分页 [英] Pagination with spring boot usiing jpa

查看:81
本文介绍了使用JPA进行Spring Boot分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现使用JpaRepository而不是PagingAndSortingRepository返回对象页面的方法?

How can I implement a method that return a page of objects using JpaRepository and not PagingAndSortingRepository ?

我的资料库

public interface GroupRepository extends JpaRepository<Group, Long> {   
    @Query(value = "SELECT g FROM Group")
    Page<Group> listAllByPage(Pageable pageable);
}

我的服务实施:

@Override
public 
Page<Group> findGroupesByPagination(Pageable pageable) {
    return groupeRepository.listAllByPage(pageable);
}

我的休息控制器方法:

@RequestMapping(value="/groups", method = RequestMethod.GET)
public @ResponseBody Page<Group> list( Pageable pageable){
    Page<Group> groupes = groupeServiceImpl.findGroupesByPagination(pageable);
    return groupes;
}

最后我得到了这个错误:

Finally I got this error:

创建名称为'groupServiceImpl'的bean时出错:不满意 通过字段'groupeRepository'表示的依赖关系;嵌套的 异常是org.springframework.beans.factory.BeanCreationException: 创建名称为'groupRepository'的bean时出错:init的调用 方法失败;嵌套的异常是java.lang.IllegalArgumentException: 验证方法公共摘要查询失败 org.springframework.data.domain.Page rimtrack.org.repository.GroupRepository.listAllByPage(org.springframework.data.domain.Pageable)!

Error creating bean with name 'groupServiceImpl': Unsatisfied dependency expressed through field 'groupeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'groupRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page rimtrack.org.repository.GroupRepository.listAllByPage(org.springframework.data.domain.Pageable)!

推荐答案

查询可以通过某个地方的注释定义,也可以通过其他方式声明.请查阅特定商店的文档,以找到该商店的可用选项.如果存储库基础结构在引导时找不到该方法的声明查询,则它将失败.

The query can be defined by an annotation somewhere or declared by other means. Consult the documentation of the specific store to find available options for that store. If the repository infrastructure does not find a declared query for the method at bootstrap time, it fails.

您应该使用Spring Data Jpa方法. 参考

You should using Spring Data Jpa method. Reference

  Page<T> findAll(Pageable pageable);

请更改存储库类.

考试:

public interface GroupRepository extends JpaRepository<Group, Long> {   
  Page<Group> findAlll(Pageable pageable);
}

这篇关于使用JPA进行Spring Boot分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆