oyhk 学习笔记

spring MongoDB 集成(分页),这次的分页功能.是在spring MongoDB 集成crud操作(简单封装)基础上添加的,希望对操作spring mongodb 集成的朋友们有所帮助,那么,直接看代码吧..

首页创建分页类

Pagination.java

Java代码

mongodbtemplate分页 spring data mongodb 分页_spring

mongodbtemplate分页 spring data mongodb 分页_分页_02

mongodbtemplate分页 spring data mongodb 分页_分页_03

  1. package

  2. import

  3. /**
  4. * 分页数据类
  5. *
  6. * @author hk
  7. *
  8. * 2012-10-26 下午8:23:15
  9. */
  10. public class

  11. /**
  12. * 一页数据默认20条
  13. */
  14. private int pageSize = 20;
  15. /**
  16. * 当前页码
  17. */
  18. private int

  19. /**
  20. * 上一页
  21. */
  22. private int

  23. /**
  24. * 下一页
  25. */
  26. private int
  27. /**
  28. * 一共有多少条数据
  29. */
  30. private long

  31. /**
  32. * 一共有多少页
  33. */
  34. private int
  35. /**
  36. * 数据集合
  37. */
  38. private

  39. /**
  40. * 分页的url
  41. */
  42. private

  43. /**
  44. * 获取第一条记录位置
  45. *
  46. * @return
  47. */
  48. public int
  49. return (this.getPageNo() - 1) * this.getPageSize();
  50. }

  51. /**
  52. * 获取最后记录位置
  53. *
  54. * @return
  55. */
  56. public int
  57. return this.getPageNo() * this.getPageSize();
  58. }

  59. /**
  60. * 计算一共多少页
  61. */
  62. public void
  63. this.totalPage = (int) ((this.totalCount % this.pageSize > 0) ? (this.totalCount / this.pageSize + 1)
  64. : this.totalCount / this.pageSize);
  65. }

  66. /**
  67. * 设置 上一页
  68. */
  69. public void
  70. this.upPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo;
  71. }

  72. /**
  73. * 设置下一页
  74. */
  75. public void
  76. this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1;
  77. }

  78. public int
  79. return
  80. }

  81. public int
  82. return
  83. }

  84. public int
  85. return
  86. }

  87. public int
  88. return
  89. }

  90. public void setPageSize(int
  91. this.pageSize = pageSize;
  92. }

  93. public int
  94. return
  95. }

  96. public void setPageNo(int
  97. this.pageNo = pageNo;
  98. }

  99. public long
  100. return
  101. }

  102. public void setTotalCount(long
  103. this.totalCount = totalCount2;
  104. }

  105. public
  106. return
  107. }

  108. public void
  109. this.datas = datas;
  110. }

  111. public
  112. return
  113. }

  114. public void
  115. this.pageUrl = pageUrl;
  116. }

  117. public Pagination(int pageNo, int pageSize, long
  118. this.setPageNo(pageNo);
  119. this.setPageSize(pageSize);
  120. this.setTotalCount(totalCount2);
  121. this.init();
  122. }

  123. /**
  124. * 初始化计算分页
  125. */
  126. private void
  127. this.setTotalPage();// 设置一共页数
  128. this.setUpPage();// 设置上一页
  129. this.setNextPage();// 设置下一页
  130. }
  131. }
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 添加了以下分页的代码

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. }