PageHelper分页插件及PageInfo介绍及使用

1. MyBatis分页插件-PageHelper的配置

maven配置文件pom.xml 引入依赖:

        
        
            com.github.pagehelper
            pagehelper
            5.0.0
        

mybatis-config.xml全局配置文件中配置拦截器插件:



    
    
	

 2. 分页的使用

  • Service
    public List selectByList() {
        List employeeList = employeeMapper.selectByExampleWithDepartment(null);
        return employeeList;
    }
  • Controller
    @RequestMapping("/empsList")
    public String userList(@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model) {
        //在查询之前调用,传入pn默认值是1,pageSize是5,意思是从第1页开始,每页显示5条记录。
        PageHelper.startPage(pn, 5);
        //startPage后面紧跟查询就是一个分页查询。
        List list = employeeService.selectByList();
        //使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就行。
        //封装了详细的分页信息,包括我们查询出来的数据userList,传入连续显示的页数5。
        PageInfo page = new PageInfo(list, 5);
        model.addAttribute("pageInfo", page);
        return "employeeDepartment-list";
    }            

 3.PageInfo.class是插件里的类,非常方便的调用,分页再次提高性能:

    //当前页
    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 prePage;
    //下一页
    private int nextPage;
    //是否为第一页
    private boolean isFirstPage;
    //是否为最后一页
    private boolean isLastPage;
    //是否有前一页
    private boolean hasPreviousPage;
    //是否有下一页
    private boolean hasNextPage;
    //导航页码数
    private int navigatePages;
    //所有导航页号
    private int[] navigatepageNums;
    //导航条上的第一页
    private int navigateFirstPage;
    //导航条上的最后一页
    private int navigateLastPage;

    public PageInfo() {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
    }            

4. 使用spring测试模块提供的测试请求功能

package cn.xiwh.crud.test;

import cn.xiwh.crud.bean.Employee;
import com.github.pagehelper.PageInfo;
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 java.util.List;

/**
 * 使用spring测试模块提供的测试请求功能,测试crud请求的正确性
 * spring4测试时,需要servlet3.0以上的版本支持
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:springmvc.xml"})
public class MvcTest {

    //虚拟的mvc请求,获取到处理结果
    MockMvc mockMvc;

    //传入springMVC的IOC。需要在类上加入@WebAppConfiguration注解。
    @Autowired
    WebApplicationContext webApplicationContext;

    // @Before每次使用初始化
    @Before
    public void initMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    /**
     * 测试分页的方法
     */
    @Test
    public void empsTest() throws Exception {
        //模拟请求拿到返回值
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "10")).andReturn();
        //请求成功后,请求域中会有pageInfo,我们可以取出pageInfo进行验证。
        MockHttpServletRequest mockHttpServletRequest = result.getRequest();
        PageInfo pageInfo = (PageInfo) mockHttpServletRequest.getAttribute("pageInfo");
        System.out.println("当前页码:" + pageInfo.getPageNum());
        System.out.println("总页面:" + pageInfo.getPages());
        System.out.println("总记录数:" + pageInfo.getTotal());
        System.out.println("在页面连续显示的页码:");
        int[] page = pageInfo.getNavigatepageNums();
        for (int pn : page) {
            System.out.print(pn + " ");
        }
        System.out.println();
        //获取员工数据
        List employees = pageInfo.getList();
        for (Employee emp : employees) {
            System.out.println("员工ID:" + emp.getEmpId() + "员工姓名:" + emp.getEmpName() + "员工邮箱:" + emp.getEmail());
        }
    }

}            

 5. 页面使用

1). 头部引用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2). 页面使用

    
${epm.empId} ${epm.empName} ${epm.gender == "M"?"男":"女"} ${epm.email} ${epm.department.depName}
        <%-- 分页文字信息 --%>
        
当前第${pageInfo.pageNum}页,共有${pageInfo.pages}页,总计${pageInfo.total}条记录

转载请注明出处:BestEternity亲笔。

你可能感兴趣的:(JSP)