1.在pom文件中引入Pagehelper分页插件
1
2
3
4
5
|
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency>
|
2.配置分页插件
1
2
3
4
5
|
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
|
![]()
3.controller代码
1
2
3
4
5
6
7
8
|
@RequestMapping("alluser") public String alluser(Model model,HttpSession session,@RequestParam(defaultValue = "1",value ="pageNum" ) Integer pageNum){ PageHelper.startPage(pageNum,5); List<UserTable> list=userService.selectAll(); PageInfo<UserTable> pageInfo = new PageInfo<UserTable>(list); model.addAttribute("user",pageInfo); session.setAttribute("Page","alluser");//显示的html页面 return "index"; }
|
其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:
PageInfo.list 结果集
PageInfo.pageNum 当前页码
PageInfo.pageSize 当前页面显示的数据条目
PageInfo.pages 总页数
PageInfo.total 数据的总条目数
PageInfo.prePage 上一页
PageInfo.nextPage 下一页
PageInfo.isFirstPage 是否为第一页
PageInfo.isLastPage 是否为最后一页
PageInfo.hasPreviousPage 是否有上一页
PageHelper.hasNextPage 是否有下一页
4.list页面代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!DOCTYPE html>
<html lang= "en" xmlns:th= "http://www.w3.org/1999/xhtml" >
<head>
<meta charset= "UTF-8" >
<title>Title</title>
</head>
<body>
<div align= "center" >
<table class='table' style='margin-bottom:0;' > <thead> <tr> <th> ID </th> <th> 用户 </th> <th> 年龄 </th> <th> 性别 </th> <th> 住址 </th> <th> 入驻 </th> <th> 车牌 </th> <th> 操作 </th> </tr> </thead> <tbody> <tr th:each="user:${user.list}"> <td th:text="${user.userId}"></td> <td th:text="${user.userName}"></td> <td th:text="${user.userAge}"></td> <td th:text="${user.userSex}"></td> <td th:text="${user.userAddress}"></td> <td th:text="${user.userIntime}"></td> <td th:text="${user.carId}"></td> <td> <div class='text-right'> <a class='btn btn-success btn-mini' th:href="@{userone(id=${user.userId})}"> <i class='icon-ok'></i> </a> <a class='btn btn-danger btn-mini'th:href="@{deleteuser(id=${user.userId})}"> <i class='icon-remove'></i> </a> </div> </td> </tr> </tbody> </table> <p>当前 <span th:text="${user.pageNum}"></span> 页,总 <span th:text="${user.pages}"></span> 页,共 <span th:text="${user.total}"></span> 条记录</p> <a th:href="@{alluser}">首页</a> <a th:href="@{alluser(pageNum=${user.hasPreviousPage}?${user.prePage}:1)}">上一页</a> <a th:href="@{alluser(pageNum=${user.hasNextPage}?${user.nextPage}:${user.pages})}">下一页</a> <a th:href="@{alluser(pageNum=${user.pages})}">尾页</a>
</div>
</body>
</html>
|