oyhk 学习笔记
spring MongoDB 集成(分页),这次的分页功能.是在spring MongoDB 集成crud操作(简单封装)基础上添加的,希望对操作spring mongodb 集成的朋友们有所帮助,那么,直接看代码吧..
首页创建分页类
Pagination.java
- package
- import
- /**
- * 分页数据类
- *
- * @author hk
- *
- * 2012-10-26 下午8:23:15
- */
- public class
- /**
- * 一页数据默认20条
- */
- private int pageSize = 20;
- /**
- * 当前页码
- */
- private int
- /**
- * 上一页
- */
- private int
- /**
- * 下一页
- */
- private int
- /**
- * 一共有多少条数据
- */
- private long
- /**
- * 一共有多少页
- */
- private int
- /**
- * 数据集合
- */
- private
- /**
- * 分页的url
- */
- private
- /**
- * 获取第一条记录位置
- *
- * @return
- */
- public int
- return (this.getPageNo() - 1) * this.getPageSize();
- }
- /**
- * 获取最后记录位置
- *
- * @return
- */
- public int
- return this.getPageNo() * this.getPageSize();
- }
- /**
- * 计算一共多少页
- */
- public void
- this.totalPage = (int) ((this.totalCount % this.pageSize > 0) ? (this.totalCount / this.pageSize + 1)
- : this.totalCount / this.pageSize);
- }
- /**
- * 设置 上一页
- */
- public void
- this.upPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo;
- }
- /**
- * 设置下一页
- */
- public void
- this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1;
- }
- public int
- return
- }
- public int
- return
- }
- public int
- return
- }
- public int
- return
- }
- public void setPageSize(int
- this.pageSize = pageSize;
- }
- public int
- return
- }
- public void setPageNo(int
- this.pageNo = pageNo;
- }
- public long
- return
- }
- public void setTotalCount(long
- this.totalCount = totalCount2;
- }
- public
- return
- }
- public void
- this.datas = datas;
- }
- public
- return
- }
- public void
- this.pageUrl = pageUrl;
- }
- public Pagination(int pageNo, int pageSize, long
- this.setPageNo(pageNo);
- this.setPageSize(pageSize);
- this.setTotalCount(totalCount2);
- this.init();
- }
- /**
- * 初始化计算分页
- */
- private void
- this.setTotalPage();// 设置一共页数
- this.setUpPage();// 设置上一页
- this.setNextPage();// 设置下一页
- }
- }
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 添加了以下分页的代码
1. package
2.
3. import
4.
5. import
6. import
7. import
8.
9. import
10.
11. /**
12. * mongodb 基础操作类
13. *
14. * @author oyhk
15. *
16. * 2013-1-22下午5:28:26
17. */
18. public abstract
class
19.
20. /**
21. * 通过条件查询,查询分页结果
22. *
23. * @param pageNo
24. * @param pageSize
25. * @param query
26. * @return
27. */
28. public PaginationgetPage(int pageNo,
int
29. long totalCount = this.mongoTemplate.count(query,
this.getEntityClass());
30. Paginationpage = new
31. query.skip(page.getFirstResult());// skip相当于从那条记录开始
32. query.limit(pageSize);// 从skip开始,取多少条记录
33. Listdatas = this.find(query);
34. page.setDatas(datas);
35. return
36. }
37. //.......其他代码,请下载源代码吧
38. }