public <T, R> PageResult<R> aggPageQuery(Criteria criteria, Class entityClass, Integer pageSize, Integer pageNum,
Class targetClass, Sort sort) {
Query query = new Query(criteria);
long total = mongoTemplate.count(query, entityClass);
final Integer pages = (int) Math.ceil(total / (double) pageSize);
if (pageNum <= 0 || pageNum > pages) {
pageNum = FIRST_PAGE_NUM;
}
MatchOperation match = Aggregation.match(criteria);
Aggregation agg = Aggregation.newAggregation(
match,
Aggregation.sort(sort),
Aggregation.skip(pageNum > 1 ? (pageNum - 1) * pageSize : 0),
Aggregation.limit(pageSize)
);
List<T> mappedResultList = mongoTemplate.aggregate(agg, entityClass, targetClass).getMappedResults();
log.info("mappedResultList :{}", mappedResultList);
final PageResult pageResult = new PageResult<>();
pageResult.setTotal(total);
pageResult.setPages(pages);
pageResult.setPageSize(pageSize);
pageResult.setPageNum(pageNum);
pageResult.setList(mappedResultList);
return pageResult;
}
本文作者:Eistert 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处! 声援博主:如果您觉得文章对您有帮助,可以收藏文章或推荐一下。您的鼓励是博主的最大动力!
参考
java操作MangoDB的聚合Aggregation用法-分页查询 https://blog.csdn.net/weixin_37828719/article/details/102765150
java-mongo复杂管道聚合aggregate的填坑之路(分页、allowDiskUse、统计) https://blog.csdn.net/u014773389/article/details/80938335?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link
JAVA如何实现MongoDB动态条件的分页查询 https://www.yisu.com/zixun/234807.html
|