IDEA_BAIMG5f506967e7bce7620521f2ba.jpg

一、分页插件PageHelper

PageHelper是Mybatis一款分页插件,简单上手

二、pageInfo类属性

public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow 和endRow 不常用,这里说个具体的用法
//可以在页面中"显示startRow 到endRow 共size 条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}

三、controller

    @RequestMapping("/selectbookforpage")
    public ModelAndView selectBookForPage(@RequestParam(defaultValue="1")Integer currentPage){
        PageHelper.startPage(currentPage,5);
        //紧跟的一条查询语句会被分页
        //selectAllBooks : select * from books;
        List<Books> books = bookService.selectAllBooks();
        PageInfo pageInfo = new PageInfo(books,5);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("admin_book");
        return mv;
    }

四、jsp

        <div class="row clearfix">
            <div class="col-md-12">
                <table class="table table-hover table-bordered">
                    <tr>
                        <th>书籍ID</th>
                        <th>书籍名称</th>
                        <th>书籍数量</th>
                        <th>书籍详情</th>
                        <th>操作</th>
                    </tr>

                    <c:forEach var="book" items="${pageInfo.list}">
                        <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.detail}</td>
                         <td>
                             <a href="${pageContext.request.contextPath}/book/toupdatebook?bookID=${book.bookID}">修改</a>
                             &nbsp;|&nbsp;
                             <a href="${pageContext.request.contextPath}/book/deletebook?bookID=${book.bookID}">删除</a>
                         </td>
                        </tr>
                    </c:forEach>
                </table>
            </div>
        </div>

        <div style="float: left;font-size: 16px;width: 500px;height: 100px;margin-top: 15px">
            <span>共<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.total}&nbsp;</span>条记录,</span>
            <span>当前显示第<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.pageNum}&nbsp;</span>页,</span>
            <span>共<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.pages}&nbsp;</span>页</span>
        </div>

        <nav aria-label="Page navigation" style="float: right;">
            <ul class="pagination">
                <c:if test="${pageInfo.hasPreviousPage}">
                    <li>
                        <a href="${pageContext.request.contextPath}/book/selectbookforpage?currentPage=${pageInfo.pageNum-1}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span></a>
                    </li>
                </c:if>

                <c:if test="${pageInfo.hasPreviousPage == false}">
                    <li>
                        <a href="#">&laquo;</a>
                    </li>
                </c:if>


                    <c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
                        <c:if test="${page_num == pageInfo.pageNum}">
                            <li>
                            <a href="#">${page_num}</a>
                            </li>
                        </c:if>

                        <c:if test="${page_num != pageInfo.pageNum}">
                            <li>
                                <a href="${pageContext.request.contextPath}/book/selectbookforpage?currentPage=${page_num}">${page_num}</a>
                            </li>
                        </c:if>
                    </c:forEach>


                <c:if test="${pageInfo.hasNextPage}">
                <li>
                    <a href="${pageContext.request.contextPath}/book/selectbookforpage?currentPage=${pageInfo.pageNum+1}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                </c:if>

                <c:if test="${pageInfo.hasNextPage == false}">
                    <li>
                        <a href="#">&raquo;</a>
                    </li>
                </c:if>
            </ul>
        </nav>

四、books表记录

在这里插入图片描述

四、效果截图

在这里插入图片描述

再完善几个功能后公布源码,想要目前的源码可以私信我,我的代码永无bug!!!

最后修改:2020 年 09 月 12 日 06 : 02 PM
如果觉得我的文章对你有用,请随意赞赏