更新一下:千万注意pom里面com.github.pagehelper版本为4.1.6 5.0.x以上的好像配置文件不一样!
-----------------------------这是分割线-------------------------------------
ssm集成PageHelper
心血来潮记录一下PageHelper分页插件,下面言归正传:
以下是在ssm框架的基础上进行:
1.引入pagehelper jar
2.在mybatis配置文件中添加分页插件:
这里贴一下这段代码的位置,在mybatis-config.xml配置文件中,我的项目resource文件结构如下:
然后这个mybatis-config.xml文件一般是在配置sqlSessionFactory的位置引入:
3.编写service/serviceImpl/dao/mapper四个文件
service层:
serviceImpl层:
dao层:
Mapper.xml:
4.编写controller
这里需要注意的是
PageHelper.startPage(pn,3);
List lstStudent = userService.selectAllUser();//获取用户信息
这两句代码的位置,一定要将PageHelper.startPage(pn,3);放置在前面。
5.编写视图层
jsp是真的不好写,所以我在网上找了一现成的代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script
src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>
<body>
<center>
<table width="200" border="1" class="table table-striped">
<tr>
<th>id</th>
<th>用户名</th>
<th>邮箱</th>
<th>密码</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="stu">
<tr>
<th>${stu.userId}</th>
<th>${stu.userName}</th>
<th>${stu.userEmail}</th>
<th>${stu.userPassword}</th>
<th>
<button class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-pencil"></span>编辑
</button>
<button class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span>删除
</button>
</th>
</tr>
</c:forEach>
<c:forEach begin="0" step="1" items="${userList}" var="list"
varStatus="userlist">
<tr>
<td></td>
<td>${list.userId}</td>
<td>${list.userName}</td>
<td>${list.userPassword}</td>
<td></td>
</tr>
</c:forEach>
</table>
<p>当前${pageInfo.pageNum }页,总${pageInfo.pages }页,总${pageInfo.total }条记录</p>
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="<%=request.getContextPath()%>/s/showUser.action?pn=1">首页</a></li>
<!--如果有前一页则加上前一页的超链接 -->
<c:if test="${pageInfo.hasPreviousPage }">
<li><a href="<%=request.getContextPath()%>/s/showUser.action?pn=${pageInfo.pageNum-1}"
aria-label="Previous"> <span aria-hidden="true">«</span>
</a></li>
</c:if>
<!--循环遍历所有页码并显示 -->
<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num==pageInfo.pageNum }">
<li class="active"><a href="javascript:void(0)">${page_Num}</a></li>
</c:if>
<c:if test="${page_Num!=pageInfo.pageNum }">
<li><a href="<%=request.getContextPath()%>/s/showUser.action?pn=${page_Num}">${page_Num}</a></li>
</c:if>
</c:forEach>
<!--如果有下一页则显示下一页的超级链接 -->
<c:if test="${pageInfo.hasNextPage }">
<li><a href="<%=request.getContextPath()%>/s/showUser.action?pn=${pageInfo.pageNum+1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>
</c:if>
<li><a href="<%=request.getContextPath()%>/s/showUser.action?pn=${pageInfo.pages}">末页</a></li>
</ul>
</nav>
</center>
</html>
ok,大功告成,下面看看效果:
想了一下需要注意的应该就是在controller里面的设置每页显示数的位置。
第一次写博客,有不妥的地方请大家批评指正,嘿嘿!