闲话不多说直接开始
1.导入依赖
<!-- 分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
2.配置properties,我用的是yml 文件以下是新添加的
# 分页依赖
hepagehelper:
lperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
3.后端代码实现:可能会和大家的有出入
//跳转页面
@RequestMapping("/refresherCourse")
public String refresher(HttpSession session ,Model model) {
model.addAttribute("state",0);
PageHelper.startPage(1,4);
StudentTable table= (StudentTable) session.getAttribute("data");
model.addAttribute("stu",table);
List<CourseTable> tabless= courseService.findAll();
PageInfo<CourseTable> pageInfo = new PageInfo<CourseTable>(tabless);
model.addAttribute("tabless",tabless);
model.addAttribute("PageInfo",pageInfo);
return "refresherCourse";
}
//以下这个是点击下一页 上一页或者尾页 刷新页面
@RequestMapping("/PageDown")
public String PageDown(Model model,CourseTable table ,@RequestParam(defaultValue = "1",value ="pageNum")Integer pageNum,HttpSession session){
PageHelper.startPage(pageNum,4);
List<CourseTable> tabless= courseService.findAll();
PageInfo<CourseTable> pageInfo = new PageInfo<CourseTable>(tabless);
model.addAttribute("PageInfo",pageInfo);
return "refresherCourse";
}
//上述方法也可直接返回json 然后进行解析 局部刷新
4.前端实现
<table>
<thead>
<tr>
<td 这里是遍历值>
</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div align="center">
<p>当前 <span th:text="${tabless.pageNum}"></span> 页,总 <span th:text="${tabless.pages}"></span> 页,共 <span th:text="${tabless.total}"></span> 条记录</p>
<a th:href="@{/refresherCourse}">首页</a>
<a th:href="@{PageDown(pageNum=${PageInfo.hasPreviousPage}?${PageInfo.prePage}:1)}">上一页</a>
<a th:href="@{PageDown(pageNum=${PageInfo.hasNextPage}?${PageInfo.nextPage}:${PageInfo.pages})}">下一页</a>
<a th:href="@{PageDown(pageNum=${PageInfo.pages})}">尾页</a>
</div>
代码命名不是很规范,敬请谅解