赞
踩
之前的csdn找不回来了,决定重新注册一个。望支持~~~
之前有个新同学问我mongo分页的问题,分享一下。
一如既往,不多BB。直接搂代码:
- @Override
- public List<T> find(Query query, Class<T> clazz) {
- return mongoTemplate.find(query, clazz);
- }
-
- @Override
- public List<T> find(Query query, Class<T> clazz, String collectionName) {
- return mongoTemplate.find(query, clazz, collectionName);
- }
- /**
- * 通过条件查询,查询分页结果
- *
- * @param paging 分页信息
- * @param query 查询条件
- * @param clazz clazz
- * @return 分页数据
- */
- @Override
- public Paging<T> getPage(Paging<T> paging, Query query, Class<T> clazz) {
- //查询总条数
- long totalCount = this.mongoTemplate.count(query, clazz);
- //设置总条数
- paging.setTotalCount(totalCount);
- //skip相当于从那条记录开始
- query.skip(paging.getFirstResult());
- //从skip开始,取多少条记录
- query.limit(paging.getPageSize());
- List<T> list = this.find(query, clazz);
- paging.setData(list);
- return paging;
- }

- /**
- * 通过条件查询,查询分页结果
- *
- * @param paging 分页信息
- * @param query 查询条件
- * @param clazz clazz
- * @param collectionName 集合名称
- * @return 分页数据
- */
- @Override
- public Paging<T> getPage(Paging<T> paging, Query query, Class<T> clazz, String collectionName) {
- //查询总条数
- long totalCount = this.mongoTemplate.count(query, clazz, collectionName);
- //设置总条数
- paging.setTotalCount(totalCount);
- //skip相当于从那条记录开始
- query.skip(paging.getFirstResult());
- //从skip开始,取多少条记录
- query.limit(paging.getPageSize());
- List<T> list = this.find(query, clazz, collectionName);
- paging.setData(list);
- return paging;
- }

- /**
- * 分页数据类
- */
- public class Paging<T> {
-
- /**
- * 每页数据默认10条
- */
- private int pageSize = 10;
- /**
- * 当前页码,默认为第一页
- */
- private int pageNo = 1;
- /**
- * 首页,默认为第一页
- */
- private int firstPage = 1;
- /**
- * 尾页,默认为第一页
- */
- private int lastPage = 1;
- /**
- * 上一页,默认为第一页
- */
- private int prevPage = 1;
- /**
- * 下一页,默认为第一页
- */
- private int nextPage = 1;
- /**
- * 总条数,默认为0
- */
- private long totalCount = 0;
- /**
- * 总页数,默认为一页
- */
- private int totalPage = 1;
- /**
- * 数据集合
- */
- private List<T> data;
-
- public int getPageSize() {
- return pageSize;
- }
-
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
-
- public int getPageNo() {
- return pageNo;
- }
-
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
-
- public int getFirstPage() {
- return firstPage;
- }
-
- public void setFirstPage() {
- this.firstPage = 1;
- }
-
- public int getLastPage() {
- return lastPage;
- }
-
- public void setLastPage() {
- this.lastPage = this.getTotalPage() <= 0 ? 1 : this.getTotalPage();
- }
-
- public int getPrevPage() {
- return prevPage;
- }
-
- public void setPrevPage() {
- this.prevPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo;
- }
-
- public int getNextPage() {
- return nextPage;
- }
-
- public void setNextPage() {
- this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1;
- }
-
- public long getTotalCount() {
- return totalCount;
- }
-
- public void setTotalCount(long totalCount) {
- this.totalCount = totalCount;
- }
-
- public int getTotalPage() {
- return totalPage;
- }
-
- public void setTotalPage() {
- this.totalPage = (int) ((this.totalCount % this.pageSize > 0) ? (this.totalCount / this.pageSize + 1)
- : this.totalCount / this.pageSize);
- }
-
- public List<T> getData() {
- return data;
- }
-
- public void setData(List<T> data) {
- this.data = data;
- }
-
- /**
- * 初始化分页信息
- */
- public Paging() {
- this.init();
- }
-
- /**
- * 设置页码和每页条数
- *
- * @param pageNo 页码
- * @param pageSize 每页条数
- */
- public Paging(int pageNo, int pageSize) {
- this.setPageNo(pageNo);
- this.setPageSize(pageSize);
- this.init();
- }
-
- /**
- * 初始化计算分页
- */
- private void init() {
- //总页数
- this.setTotalPage();
- //上一页
- this.setPrevPage();
- //下一页
- this.setNextPage();
- //首页
- this.setFirstPage();
- //尾页
- this.setLastPage();
- }
-
- /**
- * 当前页的起始位置
- *
- * @return 当前页的起始位置
- */
- public int getFirstResult() {
- return (this.getPageNo() - 1) * this.getPageSize();
- }
-
- }

这里采用的MongoTemplate进行客户端连接~~~
之前的csdn找不回来了,决定重新注册一个。望支持~~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。