1.导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
2.Mybatis-Config.xml配置
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
pagehelper插件在4.0以上不用配置<property name="dialect" value="mysql"/>
3.Controller层中进行查询调用
@RequestMapping("/allCompany")
public String list(Model model,@RequestParam(required = false,defaultValue = "1",value = "pn")Integer pn, Map<String,Object> map) {
//5是每页显示几条数据
PageHelper.startPage(pn,5);
//调用查询方法
List<Company> list = companyService.queryAllCompany();
//3是页面中显示几页数据
PageInfo pageInfo = new PageInfo<>(list,3);
map.put("pageInfo",pageInfo);
model.addAttribute("list", list);
return "allCompany";
}
4.jsp页面代码
<html>
<head>
<title>公司</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>公司列表————显示所有公司</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/company/toAddCompany">新增公司</a>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/company/allCompany">显示全部公司</a>
</div>
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form action="${pageContext.request.contextPath}/company/queryCompany" method="post" style="float: right"
class="form-inline">
<span style="color: red;font-weight: bold">${error}</span>
<input type="text" placeholder="请输入要查询的公司名称" class="form-control" name="queryCompanyName">
<input type="submit" value="查询" class="btn btn-primary">
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>公司编号</th>
<th>公司</th>
<th>联系方式</th>
<th>公司地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="company" items="${list}">
<tr>
<td>${company.companyId}</td>
<td>${company.company}</td>
<td>${company.contacts}</td>
<td>${company.address}</td>
<td>
<a href="${pageContext.request.contextPath}/company/toUpdate?id=${company.companyId}">修改</a>
|
<a href="${pageContext.request.contextPath}/company/deleteCompany/${company.companyId}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div class="row">
<!--文字信息-->
<div class="col-md-6">
当前第 ${pageInfo.pageNum} 页.总共 ${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录
</div><!--点击分页-->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/company/allCompany?pn=1">首页</a></li><!--上一页-->
<li>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/company/allCompany?pn=${pageInfo.pageNum-1}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</c:if>
</li>
<!--循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接-->
<c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
<c:if test="${page_num == pageInfo.pageNum}">
<li class="active"><a href="#">${page_num}</a></li>
</c:if>
<c:if test="${page_num != pageInfo.pageNum}">
<li><a href="${pageContext.request.contextPath}/company/allCompany?pn=${page_num}">${page_num}</a></li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/company/allCompany?pn=${pageInfo.pageNum+1}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:if>
</li>
<li><a href="${pageContext.request.contextPath}/company/allCompany?pn=${pageInfo.pages}">尾页</a></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</body>
</html>
5.页面展示