【问题标题】:Sorting is not working with Pageable in Springboot排序不适用于 Spring Boot 中的 Pageable
【发布时间】:2019-05-08 21:56:58
【问题描述】:

我正在尝试通过在 Springboot、JPARepository 中使用 Pageable 来实现分页和排序。不知何故,排序不起作用。我在下面包含了我的代码,其中有控制器、服务类、存储库、实体等。我还发布了我的控制台输出,您可以在其中看到只有限制被附加但不能“排序”到 sql 查询。我不知道我在这里缺少什么,因为我已经按照 Spring.io 中记录的所有内容进行分页和排序。

测试控制器:

    @RestController
    @RequestMapping("/test")
    public class TestController {

        @Autowired
        private TestService testService;

        @GetMapping("/list/{fileId}")
        public Page<Test> list(@PathVariable Integer fileId, @RequestParam Map<String, String> queryMap) throws Exception {
            return testService.getTestList(fileId, queryMap);
        }

    }

测试实体:

public class Test implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @NotNull
    @Column(name = "id")
    private Integer id;

    @Column(name = "fileId")
    private Integer fileId;

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

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

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

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

    public Test() {
    }

    public Test(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getFileId() {
        return fileId;
    }

    public void setFileId(Integer fileId) {
        this.fileId = fileId;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}

TestRepository:

public interface TestRepo extends JpaRepository<Test, Integer> {

    Page<Test> findByFileId(@Param("fileId") int fileId, Pageable pageable);

}

测试服务:

@Service
public class TestService {

    @Autowired
    private TestRepo testRepo;

    public Page<Test> getTestList(Integer fileId, Map<String, String> queryMap) {

        Sort sort = Sort.by(Direction.valueOf(queryMap.get("direction")), queryMap.get("property"));

        Pageable pageable = PageRequest.of(Integer.parseInt(queryMap.get("pageNo")) - 1,
                Integer.parseInt(queryMap.get("pageSize")), sort);

        Page<Test> testDetails = testRepo.findById(id, pageable);

        return testDetails;
    }

}

GetRequest:

http://localhost:8080/cms/test/list/0?pageNo=1&pageSize=5&direction=DESC&property=fname

控制台输出:

正如我们在控制台输出中看到的那样,即使将排序对象传递给 JPARepository 查询,sql 查询中也没有附加 order by。我可以在这里寻求帮助吗?

[nio-8080-exec-3] org.hibernate.SQL                        : select test0_.id as id1_21_, test0_.address as address2_21_, test0_.email as email3_21_, test0_.fname as fname4_21_, test0_.lname as lname5_21_ from test test0_ where test0_.fileId=? limit ?

[nio-8080-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [0]

【问题讨论】:

  • 您的代码对我来说看起来还不错。您是否调试过代码并确保所有参数都设置正确?
  • 您是否尝试过您的代码,但使用了不同类型的查询?因为您的查询总是返回一个特定的结果 (where test0_.id=?),而且分页和排序对于按主键查询的搜索都没有意义。也许可以尝试使用findAll 方法,看看结果查询是什么。
  • @Patrick 是的,我已经调试了代码并验证了所有参数都已通过。
  • @pleft 查询在分页上工作正常,但在 JPA 查询中未附加“排序依据”。
  • @pleft 很抱歉造成这种混乱,实际上我并没有在主键上搜索,它的 ID 不同,我已经更新了代码,对于一个 FileId 会有很多记录,我需要应用分页并对这些记录进行排序。所以我不认为 findAll() 对我有用。

标签: sorting spring-boot pagination spring-data-jpa


【解决方案1】:

在您的存储库上扩展 PagingAndSortingRepository 如下:

public interface TestRepo extends PagingAndSortingRepository<Test, Integer> {
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2020-08-22
    • 1970-01-01
    • 2019-02-08
    • 2020-01-19
    • 2018-03-21
    • 2015-04-13
    • 2018-10-18
    相关资源
    最近更新 更多