SpringBoot整合Pagehelper
1、导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
2、编写yml文件
pagehelper:
helper-dialect: mysql
params: countSql
reasonable: true
support-methods-arguments: true
3、编写controller
@GetMapping("/types")
public String types(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
Model model){
# 设置Pagehelper初始化 默认一页10条
PageHelper.startPage(pageNum,10);
# 查询所有对象
List<Type> typelist = typeService.getAllType();
# 将查询出来的对象放入pageinfo对象中封装保存
PageInfo<Type> pageInfo = new PageInfo<>(typelist);
# 将数据回显到网页上
model.addAttribute(pageInfo);
return "admin/types";
}
4、前端代码
<tr>
<th colspan="7">
<div class="ui inverted divided stackable grid">
<div class="two wide column" align="center">
<div class="item"><a th:href="@{/admin/types}">首页</a></div>
</div>
<div class="two wide column" align="center">
<div class="item"><a
th:href="@{/admin/types(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
</div>
</div>
<div class="eight wide m-text-thin column" align="center">
<p>第 <span th:text="${pageInfo.pageNum}"></span> 页,共 <span
th:text="${pageInfo.pages}"></span> 页,有 <span
th:text="${pageInfo.total}"></span> 个分类</p>
</div>
<div class="two wide column" align="center">
<div class="item"><a
th:href="@{/admin/types(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
</div>
</div>
<div class="two wide column" align="center">
<div class="item"><a th:href="@{/admin/types(pageNum=${pageInfo.pages})}">尾页</a>
</div>
</div>
</div>
</th>
</tr>
5、测试
|