spring MongoDB 集成(分页)

oyhk 学习笔记

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

首页创建分页类

Pagination.java

Java代码 复制代码 收藏代码
  1. package com.mkfree.framework.common.page;
  2. import java.util.List;
  3. /**
  4. * 分页数据类
  5. *
  6. * @author hk
  7. *
  8. * 2012-10-26 下午8:23:15
  9. */
  10. public class Pagination{
  11. /**
  12. * 一页数据默认20条
  13. */
  14. private int pageSize = 20;
  15. /**
  16. * 当前页码
  17. */
  18. private int pageNo;
  19. /**
  20. * 上一页
  21. */
  22. private int upPage;
  23. /**
  24. * 下一页
  25. */
  26. private int nextPage;
  27. /**
  28. * 一共有多少条数据
  29. */
  30. private long totalCount;
  31. /**
  32. * 一共有多少页
  33. */
  34. private int totalPage;
  35. /**
  36. * 数据集合
  37. */
  38. private Listdatas;
  39. /**
  40. * 分页的url
  41. */
  42. private String pageUrl;
  43. /**
  44. * 获取第一条记录位置
  45. *
  46. * @return
  47. */
  48. public int getFirstResult() {
  49. return (this.getPageNo() - 1) * this.getPageSize();
  50. }
  51. /**
  52. * 获取最后记录位置
  53. *
  54. * @return
  55. */
  56. public int getLastResult() {
  57. return this.getPageNo() * this.getPageSize();
  58. }
  59. /**
  60. * 计算一共多少页
  61. */
  62. public void setTotalPage() {
  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 setUpPage() {
  70. this.upPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo;
  71. }
  72. /**
  73. * 设置下一页
  74. */
  75. public void setNextPage() {
  76. this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1;
  77. }
  78. public int getNextPage() {
  79. return nextPage;
  80. }
  81. public int getTotalPage() {
  82. return totalPage;
  83. }
  84. public int getUpPage() {
  85. return upPage;
  86. }
  87. public int getPageSize() {
  88. return pageSize;
  89. }
  90. public void setPageSize(int pageSize) {
  91. this.pageSize = pageSize;
  92. }
  93. public int getPageNo() {
  94. return pageNo;
  95. }
  96. public void setPageNo(int pageNo) {
  97. this.pageNo = pageNo;
  98. }
  99. public long getTotalCount() {
  100. return totalCount;
  101. }
  102. public void setTotalCount(long totalCount2) {
  103. this.totalCount = totalCount2;
  104. }
  105. public ListgetDatas() {
  106. return datas;
  107. }
  108. public void setDatas(Listdatas) {
  109. this.datas = datas;
  110. }
  111. public String getPageUrl() {
  112. return pageUrl;
  113. }
  114. public void setPageUrl(String pageUrl) {
  115. this.pageUrl = pageUrl;
  116. }
  117. public Pagination(int pageNo, int pageSize, long totalCount2) {
  118. this.setPageNo(pageNo);
  119. this.setPageSize(pageSize);
  120. this.setTotalCount(totalCount2);
  121. this.init();
  122. }
  123. /**
  124. * 初始化计算分页
  125. */
  126. private void init() {
  127. this.setTotalPage();// 设置一共页数
  128. this.setUpPage();// 设置上一页
  129. this.setNextPage();// 设置下一页
  130. }
  131. }

然后,我们看回

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

Java代码 复制代码 收藏代码
  1. package com.mkfree.framework.common.mongodb;
  2. import java.util.List;
  3. import org.springframework.data.mongodb.core.MongoTemplate;
  4. import org.springframework.data.mongodb.core.query.Query;
  5. import org.springframework.data.mongodb.core.query.Update;
  6. import com.mkfree.framework.common.page.Pagination;
  7. /**
  8. * mongodb 基础操作类
  9. *
  10. * @author oyhk
  11. *
  12. * 2013-1-22下午5:28:26
  13. */
  14. public abstract class MongodbBaseDao{
  15. /**
  16. * 通过条件查询,查询分页结果
  17. *
  18. * @param pageNo
  19. * @param pageSize
  20. * @param query
  21. * @return
  22. */
  23. public PaginationgetPage(int pageNo, int pageSize, Query query) {
  24. long totalCount = this.mongoTemplate.count(query, this.getEntityClass());
  25. Paginationpage = new Pagination(pageNo, pageSize, totalCount);
  26. query.skip(page.getFirstResult());// skip相当于从那条记录开始
  27. query.limit(pageSize);// 从skip开始,取多少条记录
  28. Listdatas = this.find(query);
  29. page.setDatas(datas);
  30. return page;
  31. }
  32. //.......其他代码,请下载源代码吧
  33. }  
posted on 2013-02-05 16:31  蜜雪薇琪  阅读(607)  评论(0编辑  收藏  举报