欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring MongoDB 集成(分页)

程序员文章站 2022-04-30 21:44:19
...

 

 

首页创建分页类

Pagination.java

 

package com.mkfree.framework.common.page;import java.util.List;/**
 * 分页数据类
 * 
 * @author hk
 * 
 *         2012-10-26 下午8:23:15
 */publicclassPagination{/**
	 * 一页数据默认20条
	 */privateint pageSize =20;/**
	 * 当前页码
	 */privateint pageNo;/**
	 * 上一页
	 */privateint upPage;/**
	 * 下一页
	 */privateint nextPage;/**
	 * 一共有多少条数据
	 */privatelong totalCount;/**
	 * 一共有多少页
	 */privateint totalPage;/**
	 * 数据集合
	 */privateListdatas;/**
	 * 分页的url
	 */privateString pageUrl;/**
	 * 获取第一条记录位置
	 * 
	 * @return
	 */publicint getFirstResult(){return(this.getPageNo()-1)*this.getPageSize();}/**
	 * 获取最后记录位置
	 * 
	 * @return
	 */publicint getLastResult(){returnthis.getPageNo()*this.getPageSize();}/**
	 * 计算一共多少页
	 */publicvoid setTotalPage(){this.totalPage =(int)((this.totalCount %this.pageSize >0)?(this.totalCount /this.pageSize +1):this.totalCount /this.pageSize);}/**
	 * 设置 上一页
	 */publicvoid setUpPage(){this.upPage =(this.pageNo >1)?this.pageNo -1:this.pageNo;}/**
	 * 设置下一页
	 */publicvoid setNextPage(){this.nextPage =(this.pageNo ==this.totalPage)?this.pageNo :this.pageNo +1;}publicint getNextPage(){return nextPage;}publicint getTotalPage(){return totalPage;}publicint getUpPage(){return upPage;}publicint getPageSize(){return pageSize;}publicvoid setPageSize(int pageSize){this.pageSize = pageSize;}publicint getPageNo(){return pageNo;}publicvoid setPageNo(int pageNo){this.pageNo = pageNo;}publiclong getTotalCount(){return totalCount;}publicvoid setTotalCount(long totalCount2){this.totalCount = totalCount2;}publicListgetDatas(){return datas;}publicvoid setDatas(Listdatas){this.datas = datas;}publicString getPageUrl(){return pageUrl;}publicvoid setPageUrl(String pageUrl){this.pageUrl = pageUrl;}publicPagination(int pageNo,int pageSize,long totalCount2){this.setPageNo(pageNo);this.setPageSize(pageSize);this.setTotalCount(totalCount2);this.init();}/**
	 * 初始化计算分页
	 */privatevoid init(){this.setTotalPage();// 设置一共页数this.setUpPage();// 设置上一页this.setNextPage();// 设置下一页}}

然后,我们看回

 

MongodbBaseDao.java 添加了以下分页的代码

 

package com.mkfree.framework.common.mongodb;import java.util.List;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import com.mkfree.framework.common.page.Pagination;/**
 * mongodb 基础操作类
 * 
 * @author oyhk
 * 
 *         2013-1-22下午5:28:26
 */publicabstractclassMongodbBaseDao{/**
	 * 通过条件查询,查询分页结果
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param query
	 * @return
	 */publicPaginationgetPage(int pageNo,int pageSize,Query query){long totalCount =this.mongoTemplate.count(query,this.getEntityClass());Paginationpage=newPagination(pageNo, pageSize, totalCount);
		query.skip(page.getFirstResult());// skip相当于从那条记录开始
		query.limit(pageSize);// 从skip开始,取多少条记录Listdatas=this.find(query);
		page.setDatas(datas);return page;}//.......其他代码,请下载源代码吧}

come from internet

源碼見附件