1. 程式人生 > 其它 >用PageHelper外掛實現分頁功能(mybatis+springMVC)

用PageHelper外掛實現分頁功能(mybatis+springMVC)

1、可以去官網檢視PageHelper

2、分頁外掛的三種配置方法:

<1>在 mybatis.xml中 配置

<2>在spring的配置檔案中配置

<3>在配置類中配置

3、步驟

《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:下一頁