Spring Data Jpa 在Springboot中,排序不适用于Pageable

46qrfjad  于 4个月前  发布在  Spring
关注(0)|答案(2)|浏览(72)

我试图通过在Springboot中使用Pageable实现分页和排序,JPARepository。不知何故排序不起作用。我在下面包括我的代码,其中我有控制器,服务类,存储库,实体等。我还发布了我的控制台输出,你可以看到只有限制被追加,但不是“排序依据”到SQL查询。我不知道我在这里错过了什么,因为我已经按照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;
    }

}

获取请求:

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

控制台输出:

正如我们在控制台输出中看到的,即使sort对象被传递到JPARepository查询,在sql查询中也没有追加orderby。

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

7y4bm7vi

7y4bm7vi1#

在您的Repository上扩展PagingAndSortingRepository如下:

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

字符串

ki0zmccv

ki0zmccv2#

我在尝试分页和排序命名查询时也遇到了同样的问题
在我的例子中,只有分页一个命名查询才能工作,分页+排序不行。
所以我直接在命名查询中添加了排序,因为排序列不是动态的,而分页来自我的@Service

Pageable pageable = PageRequest.of(page, perPage);
Page<Customer> pageCustomer = prospectRepository.findAllNotDeletedSortByName(pageable);

字符串
我的orm.xml中的命名查询:

<named-query name="Prospect.findAllNotDeletedSortByDenomAzienda">
    <query>
        <![CDATA[SELECT cr 
                   FROM Customer cr 
                  WHERE cr.deleted = false
                  ORDER BY cr.name ASC ]]>
    </query>
</named-query>

相关问题