《Spring Boot框架入门到实践》(21)Spring Boot分页查询

参考博主
该分页查询用到的工具:使用SpringBoot、MyBatis、MySQL完成分页的查询用Thymeleaf显示。

  1. 先写分页查询需要的接口
	/*
	 * 分页查询
	 */
	List<User> getUserByPage(Map<String, Object> paramMap);

	/*
	 * 分页需要查询数据总数
	 */
	int getUserByTotal();
  1. 写Mapper
	List<User> selectUserByPage(Map<String, Object> paramMap);

	int selectUserByTotal();
  1. 写接口的是实现类
public List<User> getUserByPage(Map<String, Object> paramMap) {
		return userMapper.selectUserByPage(paramMap);
	}
	public int getUserByTotal() {
		return userMapper.selectUserByTotal();
	}
  1. 写Mapper的SQL语句
	<!-- 分页查询,startRow:从哪行开始查 pageSize:查多少条数据 -->
	<select id="selectUserByPage" parameterType="java.util.Map" resultMap="BaseResultMap">
		select id, nick, phone, address, email
		from u_user
	<!-- jdbcType=INTEGER:表示参数属性为INTEGER -->
		limit #{startRow,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
	</select>
	
	<!-- 分页查询总数 -->
	<select id="selectUserByTotal" resultType="java.lang.Integer">
		select
		count(*)
		from u_user
	</select>
  1. 写Controller

为了分页查询与显示 ,我们要设置5个参数:

  • 当前页:curPage
  • 每页显示的数据条数:pageSize
  • 数据的总条数:totalCount
  • 总页数:totalPage
  • 数据总数:totalRows
@RequestMapping("/index")
	public String index(@RequestParam(value = "curPage", required = false) Integer curPage, Model model) {
		/*
		 * |当前页:curPage 
		 * |每页显示的数据条数:pageSize 
		 * |数据的总条数:totalCount
		 * |总页数:totalPage
		 * |数据总数:totalRows
		 */

		// 每页显示的数据条数(固定写死了),也可以计算 
		int pageSize = 5;
		
		// 当前页为空值或小于一为负数时,固定为1
		if (curPage == null || curPage < 1) {
			curPage = 1;
		}
		
		// 分页需要查询数据总数
		int totalRows = userService.getUserByTotal();

		// 计算需要分几页
		int totalPage;
		if (totalRows % pageSize == 0) {
			// 如果totalRows可以把pageSize除尽,也就是正好的页数不会有余数。
			totalPage = totalRows / pageSize;
		} else {
			// 反之+1页显示余数
			totalPage = totalRows / pageSize + 1;
		}

		// 当前页大于分页数时
		if (curPage > totalPage) {
			curPage = totalPage;
		}
		
		// 计算查询的开始行
		int startRow = curPage * pageSize - pageSize;

		// startRow和pageSize的向SQL语句的参数传递
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("startRow", startRow);// 从那行开始查
		paramMap.put("pageSize", pageSize);// 查多少条数据
		List<User> userlist = userService.getUserByPage(paramMap);

		model.addAttribute("userlist", userlist);// 将查询的数据传入前端
		model.addAttribute("curPage", curPage);// 当前页
		model.addAttribute("totalPages", totalPage);// 总页数
		return "index";
	}
  1. 前端展示页面
<table class="table table-striped">
		<thead>
			<tr>
				<th>序号</th>
				<th>昵称</th>
				<th>手机</th>
				<th>住址</th>
				<th>邮箱</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="user:${userlist}">
				<td th:text="${userStat.count}"></td>
				<td th:text="${user.nick}"></td>
				<td th:text="${user.phone}"></td>
				<td th:text="${user.address}"></td>
				<td th:text="${user.email}"></td>
			</tr>
			<tr style="text-align: center;">
				<td colspan="6">
				<!-- 判断是否可以点击首页和上一页 -->
					<!-- 当前页小于等于1不可以点击 -->
					<span th:if="${curPage<=1}">
						首页
						上一页
					</span>
					
					<!-- 当前页大于1可以点击 -->
					<span th:if="${curPage>1}">
						<a th:href="@{/index}">首页</a>
						<!-- 返回的页数等于当前页减一页 -->
						<a th:href="@{|/index?curPage=${curPage-1}|}">上一页</a>
					</span>
					
					<span th:if="${curPage==totalPages}">
						下一页
						 尾页
					</span>
					
					<span th:if="${curPage<totalPages}">
						<a th:href="@{|/index?curPage=${curPage+1}|}">下一页</a>
						<a th:href="@{|/index?curPage=${totalPage}|}"> 尾页</a>
					</span>
					
				  </td>
			</tr>
		</tbody>
	</table>
发布了50 篇原创文章 · 获赞 13 · 访问量 1869

猜你喜欢

转载自blog.csdn.net/qq_43581078/article/details/104024898
今日推荐