spring-mybatis插件实现分页查询

导入配置文件

  • 导入2个jar
    jsqlparser-0.9.5.jar
    pagehelper-5.0.0.jar
 
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                        value>
                    property>
                bean>
            array>
        property>

controller

 @RequestMapping("/find.action")
    public String queryBookInfo(Model model,Integer now) {
        if(now == null || now<1)
            now = 1;
        PageHelper.startPage(now , 5);
        List books = bookInfoService.selectAllBookInfo();
        PageInfo page = new PageInfo(books);

        model.addAttribute("books", books);
        model.addAttribute("page",page);
        return "main";
    }

view

<%@ page language="java" contentType="text/html; charset=utf-8" isELIgnored="false"
         pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/js/jquery-1.8.3.min.js" >script>
<html>

<body>
<h2>商品信息h2>

<table>
    <tr>
        <th>书本编号th>
        <th>书本名称th>
        <th>书本类型th>
        <th>书本作者th>
        <th>出版社th>
        <th>出版日期th>
        <th>借阅信息th>
        <th>图片th>
        <th>详情th>
        <th>删除th>
        <th>修改th>
    tr>
    <form id="query" action="${pageContext.request.contextPath }/book/find.action">
        <c:forEach items="${books}" var="book" >
            <tr style="border: 3px solid;">
                <td>${book.bookCode}td>
                <td>${book.bookName}td>
                <td>${book.bookTypes.typeName}td>
                <td>${book.bookAuthor}td>
                <td>${book.publishPress}td>
                <td><fmt:formatDate value="${book.publishDate}" pattern="yyyy-MM-dd HH-mm-ss" />td>
                <td>${book.isBorrow}td>
                <td>
                    <c:if test="${book.bookPath != null}">
                        <img src="${book.bookPath}" width="100" height="100" />
                    c:if>
                    <c:if test="${book.bookPath == null}">
                        没有图片
                    c:if>
                td>
                <td><c:if test="${book.isBorrow ==0 }">未借阅c:if> <c:if
                        test="${book.isBorrow ==1 }">已借阅c:if>td>
                <td><a href="${pageContext.request.contextPath}/book/showbooks.action?id=${book.bookId}">详情a>td>
                <td><a href="${pageContext.request.contextPath}/book/deletebook.action?id=${book.bookId}">删除a>td>
                <td><a href="${pageContext.request.contextPath}/book/bookType.action?method=update">修改a>td>
            tr>
        c:forEach>
        <input type="hidden" name="now" id="now" />
    form>
    <tr>
        <td>
            <a href="javascript:void(0)" onclick="changePage(1)">首页a>  
            <a href="javascript:void(0)" onclick="changePage(${page.pageNum - 1})">上一页a>  
            <a href="javascript:void(0)" onclick="changePage(${page.pageNum + 1})">下一页a>  
            <a href="javascript:void(0)" onclick="changePage(${page.pages})">末页a>  
            ${page.pageNum}/${page.pages}
        td>
    tr>
table>
<script type="text/javascript">
    function changePage(data){
        $("#now").val(data);
        $("#pageNum").val(data);
        //提交查询的表单
        $("#query").submit();
    }
script>
body>
html>

PageInfo

//当前页
    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 list;

    //第一页
    private int firstPage;
    //前一页
    private int prePage;

    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;
    //是否有前一页
    private boolean hasPreviousPage = false;
    //是否有下一页
    private boolean hasNextPage = false;
    //导航页码数
    private int navigatePages;
    //所有导航页号
    private int[] navigatepageNums;

你可能感兴趣的:(spring-mybatis插件实现分页查询)