- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过在 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
[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/
“不是首页,但 Pageable 不是有效的 DocumentDbPageRequest,非首页请求需要 requestContinuation”findAll(PageRequest.of(1,10
正如标题所说, @CacheConfig(cacheNames = {"familyUserDao"}) public interface FamilyUserDao extends JpaRepos
所以我想返回一个包含约会的可分页。 pageable 中的每一页都应该只包含发生在某一天的约会。第 0 页应该是今天的日期,下一页将是明天发生的所有约会,并根据请求的页数继续。 如果您请求 3 页,您
我正在使用 JpaRepository Pageable 查询进行分页。除了排序字段区分大小写的问题外,所有的东西都工作正常。以下查询用于获取列表。 Pageable pageable = null;
Controller : @RequestMapping(path = "/serviceslist", method = RequestMethod.GET) public Page get
我想将此代码与 specification-arg-resolver 一起使用使用 @PageableDefault(sort = "createdAt", Direction = Sort.Dire
我应该用 Mockito 为这个方法编写一个测试: public Optional> getAllByPage(Integer pageSize, Integer page) { Pageab
我正在使用 org.springframework.data.domain.Pageable和我的 @RestController . 如何验证或限制页面大小? 没有任何验证,当客户端调用 size
我将一个实体嵌入到另一个实体中,如下所示。该存储库是在 DepEntity 上创建的。我正在尝试检索在 totalExp 列上排序的 DepEntity 对象在ExpEntity中。我正在使用 Pag
我在任何地方都找不到是否可以从字符串手动创建可分页,假设我们有以下服务方法 public List findAnything(final int page, final int size, fina
我使用 Spring 4.1.6.RELEASE 和 Spring Data Jpa 1.8.0.RELEASE。我在创建 org.springframework.data.domain.Pageab
我知道 Pageable 来自 spring-data- 域。 有没有什么优雅的方法可以直接使用 org.springframework.data.domain.Pageable 中的 @RestCo
我正在将 Spring 数据与 一起使用JPA 2.1 使用以下方法检索具有分页和规范(标准构建器)的实体: Page findAll(Specification spec, Pageable pag
我正在尝试通过在 Springboot、JPARepository 中使用 Pageable 来实现分页和排序。不知何故排序不起作用。我在下面包含我的代码,其中我有 Controller 、服务类、存
我花了很多时间在 Webflux 中找到有关 Pageable 的解决方案,不幸的是,在撰写本文时,Webflux 不支持 Pageable 所以我想出了一个解决方案,这就是我已经实现的。这是解决 S
如果我进行查询 @Query("SELECT t FROM Product t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categ
我有下面的代码。 public static Pageable defaultSort(Pageable currentPageable, String defaultSort) {
我有以下测试代码,我正在测试可分页端点,其中列出了学生的所有条目。 @Autowired private MockMvc mockMvc; @MockBean private StudentR
如何在 Pageable 对象中使用 isPaged()。我已经创建了这样的 pageable 对象。 Pageable pageable = new PageRequest (pageNumber,
在数据库表中products我有12条记录。 Ids 从 1 到 12。我使用 JpaRepository 调用 db,它扩展了 PagingAndSortingRepository。无法理解: 为什
我是一名优秀的程序员,十分优秀!