MongoPagination
吴单鹗
2023-12-01
protected MongoPagination<T> _paginatedQuery(Query query, MongoPagination<T> page) {
int skip = 0;
int pageNo = page.getPageNo();
int limitSize = page.getPageLimitSize();
if ((pageNo >= 1) && (limitSize > 0)) {
skip = (pageNo - 1) * limitSize;
}
Sort sort = null;
String[] orderBy = page.getOrderBy();
if ((orderBy != null) && (orderBy.length > 0)) {
sort = new Sort(page.isDesc() ? Sort.Direction.DESC : Sort.Direction.ASC, orderBy);
}
if (query == null) {
query = new Query();
}
if (sort != null) {
query.with(sort);
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Pagination,Query=" + query);
}
long totalSize = page.getTotalSize();
if (totalSize < 0L) {
totalSize = getMongoTemplate().count(query, getCollectionName());
page.setTotalSize(totalSize);
}
if (skip > 0)
query.skip(skip);
if (limitSize > 0)
query.limit(limitSize);
List<T> result = getMongoTemplate().find(query, getEntityClass(), getCollectionName());
page.setPojos(result);
return page;
}
}