1.分页对象:
public class PageBean<T> {
private int totalCount;
private int totalPage;
private List<T> list;
private int currentPage;
private int rows;
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
@Override
public String toString() {
return "PageBean{" +
"totalCount=" + totalCount +
", totalPage=" + totalPage +
", list=" + list +
", currentPage=" + currentPage +
", rows=" + rows +
'}';
}
}
2.mapper层:
接口
List<UserExt> findByPage(@Param("start") int start, @Param("rows") int rows);

xml文件:
<select id="findByPage" parameterType="java.lang.Integer" resultMap="UserWithEmp">
select t_user.id,t_user.username,t_user.password,t_employee.chinese_name,t_employee.english_name
from t_user,t_employee where t_user.emp_id = t_employee.id limit #{start},#{rows}
</select>

3.service层:
接口
PageBean<UserExt> findUserByPage(String currentPage);
实现类
@Override
public PageBean<UserExt> findUserByPage(String _currentPage) {
int currentPage = Integer.parseInt(_currentPage.trim());
int rows = 8;
PageBean<UserExt> pb = new PageBean<UserExt>();
pb.setCurrentPage(currentPage);
pb.setRows(rows);
int count = userMapper.findCount();
pb.setTotalCount(count);
int start = (currentPage - 1) * rows;
List<UserExt> list = userMapper.findByPage(start,rows);
pb.setList(list);
int totalPage = (count % rows) == 0 ? count / rows : count / rows + 1;
pb.setTotalPage(totalPage);
return pb;
}
4.Controller:
@RequestMapping("findUserByPage")
public String findUserByPage(String currentPage,Model model){
if (currentPage == null || "".equals(currentPage)){
currentPage = "1";
}
PageBean<UserExt> pb = userService.findUserByPage(currentPage);
model.addAttribute("pb",pb);
return "User/UserList";
}
5.jsp页面:
<span class="x-right" style="line-height:40px">共有数据:${pb.totalCount}条 ${pb.totalPage}页</span>
<form id="delAllForm" action="${pageContext.request.contextPath}/user/delAllUser.do" method="post">
<table class="layui-table">
<thead>
<tr>
<th style="text-align: center;">
<input type="checkbox" name="userCheck" id="firstCb">
</th>
<th style="text-align: center;">
ID
</th>
<th style="text-align: center;">
用户名
</th>
<th style="text-align: center;">
密码
</th>
<th style="text-align: center;">
负责人中文名
</th>
<th style="text-align: center;">
负责人中文名
</th>
<th style="text-align: center;">
操作
</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pb.list}" var="user">
<tr>
<td style="text-align: center;">
<input type="checkbox" name="userCheck" value=" ${user.id}">
</td>
<td style="text-align: center;">
${user.id}
</td>
<td style="text-align: center;">
${user.username}
</td>
<td style="text-align: center;">
${user.password}
</td>
<td style="text-align: center;">
${user.employee.chineseName}
</td>
<td style="text-align: center;">
${user.employee.englishName}
</td>
<td class="td-manage" style="text-align: center;">
<a style="text-decoration:none" onclick="member_stop(this,'10001')" href="javascript:;" title="停用">
<i class="layui-icon"></i>
</a>
<a title="编辑" href="javascript:;" onclick="member_edit('编辑','${pageContext.request.contextPath}/user/edit.do?id=${user.id}','','')"
class="ml-5" style="text-decoration:none">
<%-- <a title="编辑" href="${pageContext.request.contextPath}/user/edit.do?id=${user.id}"--%>
<%-- class="ml-5" style="text-decoration:none">--%>
<i class="layui-icon"></i>
</a>
<a style="text-decoration:none" onclick="member_password('修改密码','${pageContext.request.contextPath}/user/editPassword.do?id=${user.id}','600','400')"
href="javascript:;" title="修改密码">
<i class="layui-icon"></i>
</a>
<a title="删除" href="javascript:;" onclick="member_del(this,'${user.id}')"
style="text-decoration:none">
<i class="layui-icon"></i>
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
<!-- 右侧内容框架,更改从这里结束 -->
<!-- 分页部分-----------------------------------------------------------------开始-->
<xblock>
<div class="row pad-15">
<div class="col-md-12">
<nav class="pagination-outer" aria-label="Page navigation">
<ul class="pagination">
<c:if test="${pb.currentPage == 1}">
<li class="page-item ">
<a hidden="hidden" class="page-link " aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pb.currentPage != 1}">
<li class="page-item">
<a href="${pageContext.request.contextPath}/user/findUserByPage.do?currentPage=${pb.currentPage - 1}" class="page-link" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach begin="1" end="${pb.totalPage}" var="i">
<c:if test="${pb.currentPage == i}">
<li class="page-item active"><a class="page-link" href="${pageContext.request.contextPath}/user/findUserByPage.do?currentPage=${i}">${i}</a></li>
</c:if>
<c:if test="${pb.currentPage != i}">
<li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/user/findUserByPage.do?currentPage=${i}">${i}</a></li>
</c:if>
</c:forEach>
<c:if test="${pb.currentPage == pb.totalPage}">
<li class="page-item">
<a hidden="hidden" class="page-link" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pb.currentPage < pb.totalPage}">
<li class="page-item">
<a href="${pageContext.request.contextPath}/user/findUserByPage.do?currentPage=${pb.currentPage + 1}" class="page-link" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
</nav>
</div>
</div>
</xblock>
<!-- 分页部分-------------------------------------------------------结束-->
6.效果展示:
