简体   繁体   English

在 Spring 中使用带有 Pageable 的 findAll 的最佳方法是什么?

[英]What is the best way to use findAll with Pageable in Spring?

I have employee class我有员工 class

@Entity
 class Employee {
     @Id
     Integer id;
     String name;
     Integer age;
}

and another class EmployeeInfo:和另一个 class EmployeeInfo:

class EmployeeInfo {
         
     Integer id;
     String name;
 }

Now, I need to build a service to get a paginated list of EmployeeInfo by using findAll(Pageable pageable) from the repository现在,我需要使用存储库中的findAll(Pageable pageable)构建一个服务来获取 EmployeeInfo 的分页列表

 @Repository
    public interface EmployeeRepository extends JpaRepository<Employee, Long> {

what is the best way to do that?最好的方法是什么? I want to avoid getting the page from findAll and create a new object of EmployeeInfo then adding it to a list in a loop我想避免从 findAll 获取页面并创建一个新的 object 的 EmployeeInfo 然后将其添加到循环中的列表中

You can utilize spring-data-jpa projections .您可以使用 spring-data-jpa投影
There are many ways to use them (open/closed projections, class or interface based, etc.), but since you already have a EmployeeInfo class, it can be achieved by defining a new method in your repository:有很多方法可以使用它们(打开/关闭投影、class 或基于接口等),但由于您已经有一个EmployeeInfo class,它可以通过在您的存储库中定义一个新方法来实现:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
    Page<EmployeeInfo> getAllBy(Pageable pageable);
}

Note that your projection DTO class properties must exactly match properties in the aggregate root (entity class).请注意,您的投影 DTO class 属性必须与聚合根(实体类)中的属性完全匹配。
Also reference documentation suggest to define .equals() and .hashcode() methods.参考文档还建议定义.equals().hashcode()方法。

Other methods can be found in the official documentation .其他方法可以参考官方文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.data.domain.PageImpl 无法反序列化,当我想使用带有注释 @Cacheable(spring cache) 的 findAll(Pageable pageable) 时? - org.springframework.data.domain.PageImpl can't be deserialized,when i want to use findAll(Pageable pageable) with annotation @Cacheable(spring cache)? Spring Boot 分页 - Mockito 存储库 findAll(Pageable) 返回 null - Spring Boot pagination - Mockito repository findAll(Pageable) returns null Spring Data JPA:具有Specification和Pageable的findAll在计数查询上失败 - Spring Data JPA: findAll with Specification and Pageable fails on count query 覆盖Pageable findAll,用于在Spring Data Rest中选择较少的列 - Override Pageable findAll for selecting fewer columns in Spring Data Rest 在spring xml配置中使用应用程序常量的最佳方法是什么? - what's the best way to use application constants in spring xml configuration? @FindBys和@FindAll的用途是什么 - What is the use of @FindBys and @FindAll 如何在 Spring 中使用 OrderBy 和 findAll - How to use OrderBy with findAll in Spring 使用for循环的最佳方法是什么? - what is the best way to use a for loop? 当我在spring jpa的nativeQuery中使用?#{#pageable}时,mysql的日志中有乱码。这是怎么回事? - when i use ?#{#pageable} in spring jpa's nativeQuery,there is messy code in mysql's log.What's the matter? 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM