Mybatis-PageHelper插件实现分页

(一)使用PageHelper分页插件的基本配置

参考:MyBatis 分页插件 PageHelper
参考 : PageHelper作者博客文章学习

1.在 pom.xml中引入依赖Jar包

<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelperartifactId>
    <version>5.0.0version>
dependency>
2.在mybatis-config.xml中配置PageInterceptor拦截器

<plugins>
     <plugin interceptor="com.github.pagehelper.PageInterceptor">

         <property name="reasonable" value="true"/>
    plugin>
plugins>
3.EmployeeControlller.java服务端程序
@Controller
public class EmployeeControlller {

    @Autowired
    EmployeeService employeeService;
    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
        //1.使用PageHelper分页插件,在查询之前只需传入页码及每页显示的数据记录参数即可!
        PageHelper.startPage(pn, 5);
        //2.startPage后紧跟的就是一个分页查询
        List emps=employeeService.getAll();
        //3.使用PageInfo封装查询结果,只需将PageInfo交给页面即可
        //4.PageInfo封装分页的详细信息
        //5. PageInfo构造器的5表示连续显示的页码数字
        PageInfo page=new PageInfo(emps,5);
        model.addAttribute("pageInfo", page);
        return "list";

    }

}
4.使用Spring-Test测试模块:模拟浏览器向服务器发送请求
package com.wang.ssm.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.github.pagehelper.PageInfo;
import com.wang.ssm.bean.Employee;

/*
 * spring4的测试需要servlet-api 3.0以上版本
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
//引入spring-bean.xml,springmvc.xml文件
@ContextConfiguration(locations={"classpath:spring-bean.xml","classpath:springmvc.xml"})
public class SpringMVCTest {
    @Autowired
    WebApplicationContext context;
    MockMvc mvc; //虚拟springmvc

    @Before
    public void initMockMvc(){
        //首先初始化
        mvc=MockMvcBuilders.webAppContextSetup(context).build();
    }
    @Test
    public void page() throws Exception{
        //1.MockMvcRequestBuilders模拟浏览器向服务器发送请求
        MvcResult result=mvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();

        //2.服务器端返回给浏览器的数据
        MockHttpServletRequest request=result.getRequest();
        @SuppressWarnings("unchecked")
        //3.获取pageInfo封装的数据
        PageInfo info=(PageInfo) request.getAttribute("pageInfo");

        //4.info调用以下数据可以获取分页信息
        System.out.println("当前页码="+info.getPageNum());
        System.out.println("总页码="+info.getPages());
        System.out.println("每页的个数="+info.getSize());
        System.out.println("总记录数="+info.getTotal());
        System.out.println("连续显示的页码数字");
        int[]pages=info.getNavigatepageNums();
        for(int i:pages) {
            System.out.print(i+",");
        }
        System.out.println("显示当前页码所显示的员工数据:");
        List list=info.getList();
        for(Employee e:list){
            System.out.println(e);
        }
    }
}

(二)PageHelp实现前台分页功能

项目结构:
Mybatis-PageHelper插件实现分页_第1张图片Mybatis-PageHelper插件实现分页_第2张图片
Mybatis-PageHelper插件实现分页_第3张图片
注:下载项目源码

1.前端实现分页源码(后台源码在上面以展示)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工列表title>

<%
    pageContext.setAttribute("path", request.getContextPath());
%>
<link rel="stylesheet"
    href="${path}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script type="text/javascript"
    src="${path }/static/bootstrap-3.3.7-dist/js/bootstrap.min.js">script>
<script type="text/javascript"
    src="${path }/static/js/jquery-1.12.4.min.js">script>
head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h1>SSM演示h1>
            div>
        div>
        <div class="row">
            <div class="col-md-4 col-md-offset-0">
                <button class="btn btn-primary">增加button>
                 <button class="btn btn-warning">删除button> 
            div>
        div>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-hover" border="2">
                    <tr>
                        <th>编号th>
                        <th>姓名th>
                        <th>性别th>
                        <th>邮箱th>
                        <th>所属部门th>
                        <th>操作th>
                    tr>
                    <c:forEach items="${pageInfo.list }" var="emp">
                        <tr>
                            <td>${emp.empId }td>
                            <td>${emp.empName }td>
                            <td>${emp.gender=="M"?"男":"女" }td>
                            <td>${emp.email }td>
                            <td>${emp.department.deptName }td>
                            <td>
                                <button class="btn btn-primary btn-sm">
                                    <span class="glyphicon glyphicon-pencil" aria-hidden="true">span>
                                    增加
                                button>
                                <button class="btn btn-warning btn-sm">
                                    <span class="glyphicon glyphicon-trash" aria-hidden="true">span>
                                    删除
                                button>
                            td>
                        tr>
                    c:forEach>
                table>
            div>
        div>
        <div class="row">
            <div class="col-md-6">
                当前${pageInfo.pageNum }页,总共${pageInfo.pages }页,总共${pageInfo.total }条记录
            div>
            <div class="col-md-6">
                <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li><a href="${path }/emps?pn=1">首页a>li>
                    <c:if test="${pageInfo.hasPreviousPage }">
                        <li>
                            <a href="${path }/emps?pn=${pageInfo.pageNum-1}" aria-label="Previous"> 
                                <span aria-hidden="true">«span>
                            a>
                        li>
                    c:if>
                    <c:forEach items="${pageInfo.navigatepageNums }" var="page">
                        <c:if test="${page==pageInfo.pageNum }">
                            <li class="active"><a href="${path }/emps?pn=${page}">${page}a>li>
                        c:if>
                        <c:if test="${page!=pageInfo.pageNum }">
                            <li><a href="${path }/emps?pn=${page}">${page}a>li>
                        c:if>
                    c:forEach>
                    <c:if test="${pageInfo.hasNextPage }">
                        <li>
                            <a href="${path }/emps?pn=${pageInfo.pageNum+1 }" aria-label="Next">
                                <span aria-hidden="true">»span>
                            a>
                        li>
                    c:if>

                    <li><a href="${path }/emps?pn=${pageInfo.pages}">末页a>li>
                ul>
                nav>
            div>
        div>
    div>
body>
html>

你可能感兴趣的:(Mybatis,PageHelper,Mybatis,SSM,Maven,Java)