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

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

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

        <!--引入PageHelper分页插件 → PageHelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

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

<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
	</plugin>
</plugins>

 2. 分页的使用

  • Service
    public List<Employee> selectByList() {
        List<Employee> 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<Employee> list = employeeService.selectByList();
        //使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就行。
        //封装了详细的分页信息,包括我们查询出来的数据userList,传入连续显示的页数5。
        PageInfo<Employee> page = new PageInfo<Employee>(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<T> 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<Employee> 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). 页面使用

    <div>
        <c:forEach items="${pageInfo.list}" var="epm">
            <tr>
                <th> ${epm.empId} </th>
                <th> ${epm.empName} </th>
                <th> ${epm.gender == "M"?"男":"女"}</th>
                <th> ${epm.email}</th>
                <th> ${epm.department.depName}</th>
            </tr>
        </c:forEach>
    </div>
        <%-- 分页文字信息 --%>
        <div class="col-md-6">
            当前第<span class="badge">${pageInfo.pageNum}</span>页,共有<span class="badge">${pageInfo.pages}</span>页,总计<span
                class="badge">${pageInfo.total}</span>条记录
        </div>

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值