当前位置:   article > 正文

MongoDB实现分页查询 java_paging getpage(paging paging, query query, c

paging getpage(paging paging, query query, class clazz)

之前的csdn找不回来了,决定重新注册一个。望支持~~~

之前有个新同学问我mongo分页的问题,分享一下。

一如既往,不多BB。直接搂代码:

  1. @Override
  2. public List<T> find(Query query, Class<T> clazz) {
  3. return mongoTemplate.find(query, clazz);
  4. }
  5. @Override
  6. public List<T> find(Query query, Class<T> clazz, String collectionName) {
  7. return mongoTemplate.find(query, clazz, collectionName);
  8. }
  1. /**
  2. * 通过条件查询,查询分页结果
  3. *
  4. * @param paging 分页信息
  5. * @param query 查询条件
  6. * @param clazz clazz
  7. * @return 分页数据
  8. */
  9. @Override
  10. public Paging<T> getPage(Paging<T> paging, Query query, Class<T> clazz) {
  11. //查询总条数
  12. long totalCount = this.mongoTemplate.count(query, clazz);
  13. //设置总条数
  14. paging.setTotalCount(totalCount);
  15. //skip相当于从那条记录开始
  16. query.skip(paging.getFirstResult());
  17. //从skip开始,取多少条记录
  18. query.limit(paging.getPageSize());
  19. List<T> list = this.find(query, clazz);
  20. paging.setData(list);
  21. return paging;
  22. }
  1. /**
  2. * 通过条件查询,查询分页结果
  3. *
  4. * @param paging 分页信息
  5. * @param query 查询条件
  6. * @param clazz clazz
  7. * @param collectionName 集合名称
  8. * @return 分页数据
  9. */
  10. @Override
  11. public Paging<T> getPage(Paging<T> paging, Query query, Class<T> clazz, String collectionName) {
  12. //查询总条数
  13. long totalCount = this.mongoTemplate.count(query, clazz, collectionName);
  14. //设置总条数
  15. paging.setTotalCount(totalCount);
  16. //skip相当于从那条记录开始
  17. query.skip(paging.getFirstResult());
  18. //从skip开始,取多少条记录
  19. query.limit(paging.getPageSize());
  20. List<T> list = this.find(query, clazz, collectionName);
  21. paging.setData(list);
  22. return paging;
  23. }
  1. /**
  2. * 分页数据类
  3. */
  4. public class Paging<T> {
  5. /**
  6. * 每页数据默认10条
  7. */
  8. private int pageSize = 10;
  9. /**
  10. * 当前页码,默认为第一页
  11. */
  12. private int pageNo = 1;
  13. /**
  14. * 首页,默认为第一页
  15. */
  16. private int firstPage = 1;
  17. /**
  18. * 尾页,默认为第一页
  19. */
  20. private int lastPage = 1;
  21. /**
  22. * 上一页,默认为第一页
  23. */
  24. private int prevPage = 1;
  25. /**
  26. * 下一页,默认为第一页
  27. */
  28. private int nextPage = 1;
  29. /**
  30. * 总条数,默认为0
  31. */
  32. private long totalCount = 0;
  33. /**
  34. * 总页数,默认为一页
  35. */
  36. private int totalPage = 1;
  37. /**
  38. * 数据集合
  39. */
  40. private List<T> data;
  41. public int getPageSize() {
  42. return pageSize;
  43. }
  44. public void setPageSize(int pageSize) {
  45. this.pageSize = pageSize;
  46. }
  47. public int getPageNo() {
  48. return pageNo;
  49. }
  50. public void setPageNo(int pageNo) {
  51. this.pageNo = pageNo;
  52. }
  53. public int getFirstPage() {
  54. return firstPage;
  55. }
  56. public void setFirstPage() {
  57. this.firstPage = 1;
  58. }
  59. public int getLastPage() {
  60. return lastPage;
  61. }
  62. public void setLastPage() {
  63. this.lastPage = this.getTotalPage() <= 0 ? 1 : this.getTotalPage();
  64. }
  65. public int getPrevPage() {
  66. return prevPage;
  67. }
  68. public void setPrevPage() {
  69. this.prevPage = (this.pageNo > 1) ? this.pageNo - 1 : this.pageNo;
  70. }
  71. public int getNextPage() {
  72. return nextPage;
  73. }
  74. public void setNextPage() {
  75. this.nextPage = (this.pageNo == this.totalPage) ? this.pageNo : this.pageNo + 1;
  76. }
  77. public long getTotalCount() {
  78. return totalCount;
  79. }
  80. public void setTotalCount(long totalCount) {
  81. this.totalCount = totalCount;
  82. }
  83. public int getTotalPage() {
  84. return totalPage;
  85. }
  86. public void setTotalPage() {
  87. this.totalPage = (int) ((this.totalCount % this.pageSize > 0) ? (this.totalCount / this.pageSize + 1)
  88. : this.totalCount / this.pageSize);
  89. }
  90. public List<T> getData() {
  91. return data;
  92. }
  93. public void setData(List<T> data) {
  94. this.data = data;
  95. }
  96. /**
  97. * 初始化分页信息
  98. */
  99. public Paging() {
  100. this.init();
  101. }
  102. /**
  103. * 设置页码和每页条数
  104. *
  105. * @param pageNo 页码
  106. * @param pageSize 每页条数
  107. */
  108. public Paging(int pageNo, int pageSize) {
  109. this.setPageNo(pageNo);
  110. this.setPageSize(pageSize);
  111. this.init();
  112. }
  113. /**
  114. * 初始化计算分页
  115. */
  116. private void init() {
  117. //总页数
  118. this.setTotalPage();
  119. //上一页
  120. this.setPrevPage();
  121. //下一页
  122. this.setNextPage();
  123. //首页
  124. this.setFirstPage();
  125. //尾页
  126. this.setLastPage();
  127. }
  128. /**
  129. * 当前页的起始位置
  130. *
  131. * @return 当前页的起始位置
  132. */
  133. public int getFirstResult() {
  134. return (this.getPageNo() - 1) * this.getPageSize();
  135. }
  136. }

这里采用的MongoTemplate进行客户端连接~~~

之前的csdn找不回来了,决定重新注册一个。望支持~~~

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号