SSM整合简单分页功能实现

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>
                            &nbsp;|&nbsp;
                            <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.页面展示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值