thymeleaf结合springboot实现分页展示功能;
mybatis集成PageHelper分页插件可以实现后台数据查询功能以及返回数据封装
效果如下
pom.xml配置
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--mybatis分页插件pagehelper-->
<!--<dependency>-->
<!--<groupId>com.github.pagehelper</groupId>-->
<!--<artifactId>pagehelper</artifactId>-->
<!--<version>5.0.0</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- pagehelper的依赖包:jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.5</version>
</dependency>
<!--thymeleaf引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Mapp层
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springBoot.mapper.test1.EmpMapper">
<resultMap id="BaseResultMap" type="com.example.springBoot.domain.Employee">
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id, name, address, position
</sql>
<select id="getAll" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM Employee
</select>
</mapper>
public interface EmpMapper {
List<Employee> getAll();
}
Controller控制层
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
@Autowired
EmpMapper empMapper;
/**
* 用户查询.
* @return
*/
@RequestMapping("/userList")
public String userInfo(Model model,Integer pageNum){
System.out.println("当前页为:"+pageNum);
pageNum = pageNum==null?1:pageNum;
PageHelper.startPage(pageNum,2);
List<Employee> list = empMapper.getAll();
PageInfo<Employee> page = new PageInfo<>(list);
System.out.println("总记录条数为:"+page.getTotal());
model.addAttribute("page",page);
return "userInfo";
}
}
展示页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>userList</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>用户列表</h1>
<br/><br/>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Address</th>
<th>Position</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${page.list}">
<th scope="row" th:text="${user.id}">1</th>
<td th:text="${user.name}">neo</td>
<td th:text="${user.address}">Otto</td>
<td th:text="${user.position}">6</td>
<td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td>
<td><a th:href="@{/delete(id=${user.id})}">delete</a></td>
</tr>
</tbody>
</table>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">
<a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a>
</div>
<div class="col-sm-2 control-label">
<a href="/userInfo/userAdd" th:href="@{/userInfo/userAdd}" class="btn btn-info">上传</a>
</div>
</div>
<div class="modal-footer no-margin-top">
<ul class="pagination pull-right no-margin">
<!-- 首页 -->
<li>
<a th:href="@{'/userInfo/userList?pageNum=1'}">首页</a>
</li>
<!-- 上一页 -->
<li th:if="${page.hasPreviousPage}">
<a th:href="@{'/userInfo/userList?pageNum=' + ${page.prePage}}" th:text="上一页"></a>
</li>
<!-- 中间页 -->
<li th:each="pageNum:${#numbers.sequence(1, page.pages)}">
<a th:href="@{'/userInfo/userList?pageNum=' + ${pageNum}}" th:text="${pageNum}" th:if="${pageNum ne page.pageNum}"></a>
<a th:href="@{'/userInfo/userList?pageNum=' + ${pageNum}}" th:text="${pageNum}" th:if="${pageNum eq page.pageNum}" th:style="'font-weight:bold;background: #6faed9;'"></a>
</li>
<!-- 下一页 -->
<li th:if="${page.hasNextPage}">
<a th:href="@{'/userInfo/userList?pageNum=' + ${page.nextPage}}" th:text="下一页"></a>
</li>
<!-- 尾页 -->
<li>
<a th:href="@{'/userInfo/userList?pageNum=' + ${page.pages}}">尾页</a>
</li>
</ul>
</div>
</body>
</html>