用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