相关疑难解决方法(0)

Spring Data JPA中的分页(限制和偏移)

我希望用户能够在我的查询方法中指定限制(返回的数量的大小)和偏移量(返回的第一个记录/返回的索引).

这是我没有任何分页功能的类.我的实体:

@Entity
public Employee {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="NAME")
    private String name;

    //getters and setters
}
Run Code Online (Sandbox Code Playgroud)

我的存储库:

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

    @Query("SELECT e FROM Employee e WHERE e.name LIKE :name ORDER BY e.id")
    public List<Employee> findByName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)

我的服务界面:

public interface EmployeeService {

    public List<Employee> findByName(String name);
}
Run Code Online (Sandbox Code Playgroud)

我的服务实施:

public class EmployeeServiceImpl {

    @Resource
    EmployeeRepository repository;

    @Override
    public List<Employee> findByName(String name) {
        return repository.findByName(name);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我尝试提供支持偏移和限制的分页功能.我的实体类保持不变.

我的"新"存储库包含一个可分页的参数:

public interface EmployeeRepository extends JpaRepository<Employee, …
Run Code Online (Sandbox Code Playgroud)

paging spring pagination jpa spring-data

34
推荐指数
5
解决办法
6万
查看次数

标签 统计

jpa ×1

pagination ×1

paging ×1

spring ×1

spring-data ×1