【SpringBoot项目】pageHelper对查询分页并显示页面

所需依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
  </dependency>

controller层

//获得全部类别信息
    @GetMapping("/categorys")
    public String listBlogCategory(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                   @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                                   Category category,
                                   Map<String, Object> map) {

        PageHelper.startPage(pageNum, pageSize);
        List<Category> categorys = categoryMapper.getAllCategoryList(category);
        PageInfo<Category> pageInfo = new PageInfo<Category>(categorys);
        map.put("num", pageNum);
        map.put("pageInfo", pageInfo);
        map.put("categorys", pageInfo.getList());
        map.put("categoryname", category.getCategoryname());

        return "html/table_complete_category";
    }

页面实现

<div class="am-cf" >
				当前第[[${pageInfo.pageNum}]]页,共[[${pageInfo.pages}]]页,共[[${pageInfo.total}]]条记录
              <div class="am-fr" >
               <ul class="am-pagination" >
				   <li class=""><a href="#" th:href="@{/categorys/(categoryname=${categoryname})}"> 首页</a></li>
                  <li class=""><a href="#" th:href="@{/categorys/(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1 ,categoryname=${categoryname})}"> «</a></li>

				   <a th:each="page,pageStat:${pageInfo.navigatepageNums}"
					  th:href="@{/categorys/(pageNum=${page},categoryname=${categoryname})}">
                                        <span  th:if="${page}==${pageInfo.pageNum}" >
                                            <span class="am-alert-secondary" th:text="${page}"></span>
                                        </span>
					                    <span th:if="${page}!=${pageInfo.pageNum}">
                                            <span class=" " th:text="${page}"></span>
                                        </span>
				   </a>
                  <li><a href="#" th:href="@{/categorys/(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages},categoryname=${categoryname})}">»</a></li>
                  <li><a href="#" th:href="@{/categorys/(pageNum=${pageInfo.pages},categoryname=${categoryname})}">尾页</a></li>

			   </ul>
              </div>
            </div>

个人总结

pagehelper中 pageinfo 包含各种信息
【SpringBoot项目】pageHelper对查询分页并显示页面_第1张图片
href注意携带参数,若名字模糊查询需要在请求头中带上名字参数 否则将按照查询全部结果进行查询

categoryname=${categoryname}

你可能感兴趣的:(SpringBoot)