spring MongoDB 集成(分页)
oyhk 学习笔记
spring MongoDB 集成(分页),这次的分页功能.是在spring MongoDB 集成crud操作(简单封装)基础上添加的,希望对操作spring mongodb 集成的朋友们有所帮助,那么,直接看代码吧..
首页创建分页类
Pagination.java
package com.mkfree.framework.common.page; import java.util.List; /** * 分页数据类 * * @author hk * * 2012-10-26 下午8:23:15 */ public class Pagination{ /** * 一页数据默认20条 */ private int pageSize = 20; /** * 当前页码 */ private int pageNo; /** * 上一页 */ private int upPage; /** * 下一页 */ private int nextPage; /** * 一共有多少条数据 */ private long totalCount; /** * 一共有多少页 */ private int totalPage; /** * 数据集合 */ private Listdatas; /** * 分页的url */ private String pageUrl; /** * 获取第一条记录位置 * * @return */ public int getFirstResult() { return (this.getPageNo() - 1) * this.getPageSize(); } /** * 获取最后记录位置 * * @return */ public int getLastResult() { return this.getPageNo() * this.getPageSize(); } /** * 计算一共多少页 */ public void setTotalPage() { this.totalPage = (int) ((this.totalCount % this.pageSize > 0) ? (this.totalCount / this.pageSize + 1) : this.totalCount / this.pageSize); } /** * 设置 上一页 */ public void setUpPage() { this.upPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo; } /** * 设置下一页 */ public void setNextPage() { this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1; } public int getNextPage() { return nextPage; } public int getTotalPage() { return totalPage; } public int getUpPage() { return upPage; } 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 long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount2) { this.totalCount = totalCount2; } public ListgetDatas() { return datas; } public void setDatas(Listdatas) { this.datas = datas; } public String getPageUrl() { return pageUrl; } public void setPageUrl(String pageUrl) { this.pageUrl = pageUrl; } public Pagination(int pageNo, int pageSize, long totalCount2) { this.setPageNo(pageNo); this.setPageSize(pageSize); this.setTotalCount(totalCount2); this.init(); } /** * 初始化计算分页 */ private void 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 */ public abstract class MongodbBaseDao{ /** * 通过条件查询,查询分页结果 * * @param pageNo * @param pageSize * @param query * @return */ public PaginationgetPage(int pageNo, int pageSize, Query query) { long totalCount = this.mongoTemplate.count(query, this.getEntityClass()); Paginationpage = new Pagination(pageNo, pageSize, totalCount); query.skip(page.getFirstResult());// skip相当于从那条记录开始 query.limit(pageSize);// 从skip开始,取多少条记录 Listdatas = this.find(query); page.setDatas(datas); return page; } //.......其他代码,请下载源代码吧 }
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19