java - 使用 jpa 通过 spring boot 进行分页

标签 java jpa spring-boot spring-data-jpa

如何实现使用 JpaRepository 而不是 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);
}

我的休息 Controller 方法:

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

最后我得到了这个错误:

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 方法。 Reference

  Page<T> findAll(Pageable pageable);

请更改存储库类。

考试:

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

关于java - 使用 jpa 通过 spring boot 进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43408618/

相关文章:

java - 如何计算 QueryDSL 中结果组的数量?

java - 如何在 Spring Boot 中从 MongoDb 中仅读取特定 JSON 节点

java - Spring 启动 : exception while exiting application

java - REST 端点 Spring Boot 中参数的自定义验证逻辑

java - 是否有针对此问题的分布式软件和一个后端数据库的良好模式?

java - 旋转对象然后平移它

java - JPA Repository.findById() 返回 null 但该值存在于数据库中

java - 使用 gradle 对带有嵌入式 glassfish 的 jar 进行容器测试失败

java - 无法包含 spring-security-web 依赖项

java - Hibernate HQL 是否支持正则表达式模式匹配?