SSM框架中分页插件pageHelper的使用实例

分页原理

物理分页
实现原理: SELECT * FROM xxx [WHERE…] LIMIT #{param1}, #{param2}
第一个参数是开始数据的索引位置
第二个参数是要查询多少条数据

  • 优点: 不会造成内存溢出
  • 缺点: 翻页的速度比较慢

逻辑分页

实现原理: 一次性将所有的数据查询出来放在内存之中,每次需要查询的时候就直接从内存之中去取出相应索引区间的数据

  • 优点: 分页的速度比较快
  • 缺点: 可能造成内存溢出

1.导入依赖

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

2.在mybatis配置文件中配置

 <!-- mybatis分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

3.controller

  @RequestMapping("/allStudent")
    public String list(@RequestParam(required = true,defaultValue = "1") Integer page, Model model){
    	//page当前页,5-每页的条目数
        PageHelper.startPage(page,5);
        List<Student> stuList = studentService.queryAllStudent();
        PageInfo p = new PageInfo(stuList,5);
        //将分页信息传到前端
        model.addAttribute("stuList",p);
        return "allStudent";
    };

4.前端展示


    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>学生ID</th>
                    <th>学生姓名</th>
                    <th>性别</th>
                    <th>邮箱</th>
                    <th>手机号</th>
                    <th>qq号</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>
                <c:forEach var="stu" items="${stuList.list}">
                    <tr>
                        <th>${stu.sid}</th>
                        <th>${stu.sname}</th>
                        <th>${stu.sex}</th>
                        <th>${stu.email}</th>
                        <th>${stu.phone}</th>
                        <th>${stu.qq}</th>
                        <th><a href="${pageContext.request.contextPath}/student/toUpdatePage?id=${stu.sid}">更改</a> |
                            <a href="${pageContext.request.contextPath}/student/deleteStudent?id=${stu.sid}">删除</a>
                        </th>
                    </tr>

                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
    <!-- 分页条 -->
    <div id="div_pagination_bottom" style="align-content: center">
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li >
                    <a <c:if test="${stuList.hasPreviousPage}"> href="${pageContext.request.contextPath}/student/allStudent?page=${stuList.pageNum-1}</c:if> "
                       aria-label="Previous">
                        <span aria-hidden="true">«</span>
                    </a>
                </li>
                <c:forEach items="${stuList.navigatepageNums }" var="page_Num">
                    <c:if test="${page_Num == stuList.pageNum }">
                        <li class="active"><a href="#">${ page_Num}</a></li>
                    </c:if>
                    <c:if test="${page_Num != stuList.pageNum }">
                        <li>
                            <a href="${pageContext.request.contextPath}/student/allStudent?page=${ page_Num}">${ page_Num}</a>
                        </li>
                    </c:if>
                </c:forEach>
                <li  >
                    <a <c:if test="${stuList.hasNextPage}"> href="${pageContext.request.contextPath}/student/allStudent?page=${stuList.pageNum+1}</c:if>"
                       aria-label="Next">
                        <span aria-hidden="true">»</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>
</div>

5.pageInfo工具类中的属性

	//当前页
    private int currentPage;
    //总页数
    private int pageCount;
    //总条数
    private int totalRecord;
    //页面大小
    private int pageSize;
    //是否有下一页
    private boolean hasNextPage;
    //是否有上一页
    private boolean hasPrePage;
    //当前页码数组
    private int[] pageArray;
    //当前页数据
    private List<T> list;
    //下一页
    private int nextPage;
    //上一页
    private int prePage;
    //是否是第一页
    private boolean isFirstPage;
    //是否是最后一页
    private boolean isLastPage;
    //开始行数
    private int startRow;
    //结束行数
    private int endRow;
    //需要显示多少页
    private int navigatePages;
    
	public PageInfo() {
        this.navigatePages = 5;		//默认显示5页
        this.hasPrePage = true;
        this.hasNextPage = true;
        this.isFirstPage = false;
        this.isLastPage = true;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值