一、导入pagehelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
二、在Controller中使用pagehelper分页插件
@RequestMapping("/toShop")
public String toShop(Model model, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
//启动分页插件(pageNum为当前页码,9为每页记录条数)
PageHelper.startPage(pageNum,9);
//获取书籍记录
List<Book> bookList=shopService.queryAllBooks();
PageInfo<Book> pageInfo=new PageInfo<Book>(bookList);
//将数据放进model中
model.addAttribute("pageInfo",pageInfo);
//存储请求路径,方便切换页面
model.addAttribute("requestPreUrl","/toShop");
return "shop";
}
@RequestMapping("/shop/btid/{btid}")
public String showBookType(@PathVariable(name="btid") Integer btid,Model model, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
//启动分页
PageHelper.startPage(pageNum,9);
//获取书籍记录
List<Book> bookList=shopService.queryBooksBtid(btid);
PageInfo<Book> pageInfo=new PageInfo<>(bookList);
//将数据放进model中
model.addAttribute("pageInfo",pageInfo);
//存储请求路径,方便切换页面
model.addAttribute("requestPreUrl","/shop/btid/"+btid);
return "shop";
}
@RequestMapping("/shop/pid/{pid}")
public String queryBooksByPid(Model model,@PathVariable(name = "pid") Integer pid,@RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
//启动分页
PageHelper.startPage(1,9);
//获取数据
List<Book> bookList=pressService.queryBooksByPid(pid);
PageInfo<Book> pageInfo=new PageInfo<>(bookList);
//存储数据到model
model.addAttribute("pageInfo",pageInfo);
//存储请求路径,方便切换页面
model.addAttribute("requestPreUrl","/shop/pid/"+pid);
return "shop";
}
-
注意PageHelper.startPage()放在获取数据之前,不然分页不生效(其中原理有关MyBatis分页)
-
为了满足三个方法的功能需求,将请求requestPreUrl保存起来方便切换页面数据( 如果功能简单可以忽略)
三、thymeleaf使用
<ul class="pagination" id="page_ul">
<li th:if="${pageInfo.pageNum!=1}">
<a th:href="@{${requestPreUrl}(pageNum=1)}">首页</a>
</li>
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{${requestPreUrl}(pageNum=${pageInfo.prePage})}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li th:if="${pageInfo.pageNum!=1}" th:each="page : ${#numbers.sequence(pageInfo.pageNum-4>0?pageInfo.pageNum-4:1,pageInfo.pageNum-1)}">
<a th:href="@{${requestPreUrl}(pageNum=${page})}" th:text="${page}"></a>
</li>
<li th:if="${pageInfo.pageNum!=pageInfo.pages}">
<a href="#" style="color: red;" th:text="${pageInfo.pageNum}"></a>
</li>
<li th:if="${pageInfo.pageNum!=pageInfo.pages}" th:each="page : ${#numbers.sequence(pageInfo.pageNum+1,pageInfo.pageNum+4<pageInfo.pages?pageInfo.pageNum+4:pageInfo.pages)}">
<a th:href="@{${requestPreUrl}(pageNum=${page})}" th:text="${page}"></a>
</li>
<li th:if="${pageInfo.hasNextPage}">
<a th:href="@{${requestPreUrl}(pageNum=${pageInfo.nextPage})}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<li th:if="${pageInfo.pageNum!=pageInfo.pages}">
<a th:href="@{${requestPreUrl}(pageNum=${pageInfo.pages})}">尾页</a>
</li>
</ul>
-
两次使用th:each遍历,控制显示的页码数量
-
使用th:if判断来增加删除li标签(例如:当前页数为1时,删除上一页)
-
其中pageInfo.pages为总页数,pageInfo.pageNum为当前页,pageInfo.prePage为上一页,pageInfo.nextPage为下一页,pageInfo.hasPreviousPage判断是否有上一页,pageInfo.hsaNextPage判断是否有下一页
-
上面使用预先保存在request域中的requestPreUrl,就可以满足多个功能需求
四、实现结果