1.首先加入pagehelper的pom依赖
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2.准备实体类User.java
public class User {
private int id;
private String username;
private String password;
省略getter setter 方法
}
3.controller层代码
[@Controller](https://my.oschina.net/u/1774615)
public class UserController {
@Autowired
UserMapper userMapper;
//第一种实现方式
//http://localhost:8080/getUser?pageNum=1&pageSize=2
@RequestMapping(value = {"/getUser","/"})
public String getUser(Integer pageNum, Integer pageSize, Model model){
if (pageNum==null&&pageSize==null){
pageNum=1;
pageSize=5;
}
if (pageNum!=null&&pageSize==null){
pageSize=3;
}
PageHelper.startPage(pageNum,pageSize);
Page<User> userList = userMapper.getUserList();
model.addAttribute("userStat",userList);
return "index";
}
}
4.mapper类
@Mapper
public interface UserMapper {
@Select("select * from user")
Page<User> getUserList();
}
5.页面展示index.html
<body>
<h1>查询所有的用户。。。</h1>
<table class="table table-hover">
<tr>
<td class="active">编号</td>
<td class="success">用户名</td>
<td class="warning">密码</td>
</tr>
<tr th:each="user : ${userStat}">
<td class="active" th:text="${user.id}">编号</td>
<td class="success" th:text="${user.username}">用户名</td>
<td class="warning" th:text="${user.password}" >密码</td>
</tr>
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#" th:href="@{/getUser?pageNum=1}">1</a></li>
<li><a href="#" th:href="@{/getUser?pageNum=2}">2</a></li>
<li><a href="#" th:href="@{/getUser?pageNum=3}">3</a></li>
<li><a href="#" th:href="@{/getUser?pageNum=4}">4</a></li>
<li><a href="#" th:href="@{/getUser?pageNum=5}">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</body>
来源:oschina
链接:https://my.oschina.net/wyn365/blog/3198236