用PageHelper插件實現分頁功能(mybatis+springMVC)


1、可以去官網查看PageHelper

2、分頁插件的三種配置方法:

    <1>在 mybatis.xml中 配置

   <2>在spring的配置文件中配置

   <3>在配置類中配置

3、在mybatis。xml文件中的配置步驟

《1》在pom.xml文件中加入PageHelper依賴

 

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.3.0</version>
</dependency>

 


依賴庫網址:https://mvnrepository.com/
《2》配置插件,在mybatis.xml中(注意位置,在數據庫連接的上面)
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>

 


《3》使用{
1、接口方法
2、實現接口{加入分頁方法}
          PageHelper.startPage(page,5);
        }
《4》具體實現如下:
Controller.java
/* PageHelper.startPage(page,5); * page:代表當前頁面(從jsp頁面傳過來的參數) * 5:代表每頁有多少條信息 * @RequestParam(value = "page",defaultValue = "1") int page * @RequestParam:表示從jsp頁面傳過來的參數 * value:為傳過來的參數 * */
/*查詢所有*/ @RequestMapping("/alllist") public String alllist(@RequestParam(value = "page",defaultValue = "1") int page, Model model){ PageHelper.startPage(page,5); SingleDao singleDao=new SingleDaoImpl(); List<Lists> allLists=singleDao.selectall(); //創建PageInfo對象,把列表中的信息封裝到PageInfo中,因為PageInfo中有很多分頁方法可以調用
  PageInfo pageInfos=new PageInfo(allLists); //把創建的PageInfo對象添加到model中傳入 jsp頁面
  model.addAttribute("pageInfos" , pageInfos); return "single/allList"; } //model傳過來的數據在jsp頁面怎么調用 ${pageinfos.list}:顯示列數據

 



alllist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 實例 - 上下文類</title>
</head>
<body>
    <table class="table">
          <thead>
            <tr>
                    <th>id</th>
                    <th>學科</th>
                    <th>題型</th>
                    <th>題干</th>
                    <th>分數</th>
                    <th>難度</th>
                    <th >編輯</th>
             </tr>
            </thead>
   <c:forEach items="${pageInfos.list }" var="a">
             <tbody>
                 <tr  class="warning">
                        <td>${a.id }</td>
                        <td>${a.subject }</td>
                        <td>${a.questiontype }</td>
                        <td>${a.question }</td>
                        <td>${a.score }</td>
                        <td>${a.difficulty }</td>
                        <td>
                            <a href="/student/delete?id=${s.id}">刪除</a>
                            <a href="/student/selectOne?id=${s.id}">修改</a>
                        </td>
                            <%-- <a href="deleteStudent?id=${s.id}">刪除</a>--%>
                    </tr>
                    </tbody>
                </c:forEach>
            </table> 當前是 第${pageInfos.pageNum} 頁,每頁${pageInfos.pageSize}條 //:/single/alllist:控制方法路徑; //page:要傳入到控制方法的參數
        <a href="/single/alllist?page=1">首頁</a>
        <a href="/single/alllist?page=${pageInfos.prePage==0?1:pageInfos.prePage}">上一頁</a>
        <a href="/single/alllist?page=${pageInfos.nextPage}">下一頁</a>
        <a href="/single/alllist?page=${pageInfos.pages}">尾頁</a>
</body>
</html>

 

PageInfo中的方法:(要先創建PageIngo對象 ,再調用方法)
PageNum:獲取當前頁
Pages:獲取總頁數;
PageSize;每頁條數;
Total:總記錄數;
NavigateFirstPage:獲取第一頁
NavigateLastPage:獲取最后一頁
PrePage:上一頁
NextPage:下一頁


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM