<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<select id="getListByPage" resultType="com.bw.pojo.TbRole">
select * from tb_role
order by role_id
</select>
public interface TbRoleMapper {
List<TbRole> getListByPage();
}
@Override
public PageInfo getListByPage(Integer pageNum, Integer pageSize) {
//--1 开启分页
PageHelper.startPage(pageNum, pageSize);
//--2 执行分页查询
List<TbRole> listByPage = tbRoleMapper.getListByPage();
//--3 返回分页数据
return new PageInfo(listByPage);
}
@GetMapping(value = "/bill")
public String bill(Model model, Integer pageNum, Integer pageSize) {
if(pageNum==null || pageNum==0) {
pageNum = 1;
}
if(pageSize==null || pageSize==0) {
pageSize = 10;
}
PageInfo pageInfo = tbBillService.getBillByPage(pageNum, pageSize);
List list = pageInfo.getList();
model.addAttribute("list", list);
model.addAttribute("pages", pageInfo.getPages());
model.addAttribute("curPage", pageNum);
model.addAttribute("pageSize", pageInfo.getPageSize());
model.addAttribute("prePage", pageInfo.getPrePage());
model.addAttribute("nextPage", pageInfo.getNextPage());
model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());
return "bill";
}
1)、控制层代码
@GetMapping(value = "/page")
public String page(Model model, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
if(pageNum == 0) {
pageNum = 1;
}
PageInfo listByPage = tbRoleService.getListByPage(pageNum, pageSize);
model.addAttribute("listByPage", listByPage);
return "roleList";
}
2)、jsp代码
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
Created by IntelliJ IDEA.
User: andy
Date: 2023/11/11
Time: 9:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>角色管理</title>
</head>
<body>
<div>
<h3>角色管理</h3>
</div>
<div>
<table>
<tr>
<th>角色ID</th>
<th>角色名称</th>
<th>创建时间</th>
</tr>
<c:forEach items="${listByPage.list}" var="item">
<tr>
<td>${item.roleId}</td>
<td>${item.roleName}</td>
<td>
<fmt:formatDate value="${item.createTime}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>
</td>
</tr>
</c:forEach>
</table>
</div>
<div>
<c:if test="${!listByPage.isFirstPage}">
<a href="/zg2/role/page.do?pageNum=${listByPage.prePage}">上一页</a>
</c:if>
<c:forEach items="${listByPage.navigatepageNums}" var="item">
<a href="/zg2/role/page.do?pageNum=${item}">${item}</a>
</c:forEach>
<c:if test="${!listByPage.isLastPage}">
<a href="/zg2/role/page.do?pageNum=${listByPage.nextPage}">下一页</a>
</c:if>
</div>
</body>
</html>