Pagination with mongoTemplate(使用 mongoTemplate 进行分页)
问题描述
我有一个带有 Pageable 的查询:
I have a Query with Pageable:
Query query = new Query().with(new PageRequests(page, size))
如何使用 MongoTemplate 执行它?我没有看到一个方法返回 Page<T>
.
How can I execute it with MongoTemplate ? I don't see a single method returning Page<T>
.
推荐答案
确实MongoTemplate
没有findXXX
with Pageables.
It's true that the MongoTemplate
doesn't have findXXX
with Pageables.
但是你可以使用 Spring Repository PageableExecutionUtils
来实现.
But you can use the Spring Repository PageableExecutionUtils
for that.
在您的示例中,它看起来像这样:
In your example it would look like this:
Pageable pageable = new PageRequests(page, size);
Query query = new Query().with(pageable);
List<XXX> list = mongoTemplate.find(query, XXX.class);
return PageableExecutionUtils.getPage(
list,
pageable,
() -> mongoTemplate.count(Query.of(query).limit(-1).skip(-1), XXX.class));
就像在原始的 Spring Data Repository 中一样,PageableExecutionUtils
将执行一个计数请求并将其包装到一个漂亮的 Page
中.
Like in the original Spring Data Repository, the PageableExecutionUtils
will do a count request and wrap it into a nice Page
for you.
这里你可以看到spring也在做同样的事情.
Here you can see that spring is doing the same.
这篇关于使用 mongoTemplate 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 mongoTemplate 进行分页


基础教程推荐
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01