gpt4 book ai didi

排序不适用于 Spring Boot 中的 Pageable

转载 作者:行者123 更新时间:2023-12-03 17:25:58 26 4
gpt4 key购买 nike

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

测试 Controller :

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


}

测试库:
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;
}

}

获取请求:
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]

最佳答案

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

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

关于排序不适用于 Spring Boot 中的 Pageable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com