在Springboot中排序不适用于Pageable

时间:2018-12-07 06:26:22

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

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

TestController:

    @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);
        }

    }

TestEntity:

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);

}

TestService:

@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

控制台输出:

正如我们在控制台输出中看到的那样,即使将sort对象传递给JPARepository查询,在sql查询中也没有附加任何顺序。我可以在这里得到一些帮助吗。

[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]

1 个答案:

答案 0 :(得分:1)

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

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