我有一个带有方法名查询的存储库来返回前3个实体,例如。
@Repository
public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Integer> {
Page<MyEntity> findTop3By(Pageable pageable);
}
该方法还支持 Pageable
给结果分页。但是,返回的结果不符合可分页大小,并且非常混乱。e、 g.当共有5个实体时
myEntityRepository.findTop3By(PageRequest.of(0, 9))
// page 0 and size 9, got top 3 entities
// (good)
myEntityRepository.findTop3By(PageRequest.of(0, 4))
// page 0 and size 4, still got top 3 entities
// (good)
myEntityRepository.findTop3By(PageRequest.of(0, 3))
// page 0 and size 3, still got top 3 entities, however, returned TotalElements is 5
// (partial, expected TotalElements is 3 so that there is no next page)
myEntityRepository.findTop3By(PageRequest.of(1, 3))
// page 1 and size 3, got the 4th and 5th entities, returned TotalElements is 5
// (wrong, expected no entities)
myEntityRepository.findTop3By(PageRequest.of(0, 2))
// page 0 and size 2, still got top 3 entities, returned TotalElements is 5
// (wrong, does not respect the page size, expected only the 1st and 2nd entities)
myEntityRepository.findTop3By(PageRequest.of(1, 2))
// page 1 and size 2, got 3rd, 4th and 5th entities, returned TotalElements is 5
// (wrong, expected only the 3rd entity)
如何解决此问题以获得预期结果?
4条答案
按热度按时间rm5edbpk1#
如果我的理解是正确的,那么您需要对存储库查询进行分页,并对响应进行排序。在这种情况下,您的存储库应该扩展
PagingAndSortingRepository<MyEntity, Integer>
```public interface MyEntityRepository extends
PagingAndSortingRepository<MyEntity, Integer> {
}
for(int pageCounter= 0; pageCounter < 100; ++i)
Pageable PageRequestwithTopthree =
PageRequest.of(pageCounter, 3, sort.by("column_name").descending());
}
Pageable PageRequestwithTopthree =
PageRequest.of(pageCounter, pageSize,
sort.by("column_name").descending());
isr3a4wc2#
类pagerequest的构造函数如下
我们倾向于通过一个'排序'的一些规则,你想订购。
sq1bmfud3#
您应该配置
Pageable
以这种方式在服务层上nnvyjq4y4#
您可以按如下方式定义一个方法(找到所有行,然后分页):