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

转载于:https://www.cnblogs.com/shhaoran/archive/2013/02/05/2924521.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud集成MongoDB可以通过使用Spring Data MongoDB实现。Spring Data MongoDB是一个用于简化和简化与MongoDB交互的框架,它提供了一套简单的API,可以轻松地进行数据库操作。 首先,需要在pom.xml文件中添加相关的依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> ``` 然后,在Spring Boot的主类上添加`@EnableMongoRepositories`注解,以启用MongoDB的支持: ```java @EnableMongoRepositories(basePackages = "com.example.repository") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 其中,`com.example.repository`是存放MongoDB相关的repository接口的包路径。 接下来,可以创建一个实体类来映射MongoDB中的集合: ```java @Document(collection = "users") public class User { @Id private String id; private String name; private int age; // getters and setters } ``` 在这个例子中,我们创建了一个名为"users"的集合,并在User类中定义了id、name和age字段。 然后,可以创建一个继承自`MongoRepository`的接口来定义对于User集合的操作: ```java @Repository public interface UserRepository extends MongoRepository<User, String> { List<User> findByAge(int age); } ``` 在这个例子中,我们定义了一个`findByAge`方法,用于根据年龄查询用户。 最后,可以在其他组件中通过依赖注入的方式使用UserRepository来进行数据库操作: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> findByAge(int age) { return userRepository.findByAge(age); } } ``` 通过上述步骤,就可以很方便地使用Spring Cloud集成MongoDB进行数据库操作了。当然,除了上述的基本操作外,Spring Data MongoDB还提供了更多高级的功能,如聚合查询、分页查询等,可以根据具体需求进行扩展使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值