首页 > 编程知识 正文

pagehelper如何使用,pagehelper用法

时间:2023-05-06 00:56:32 阅读:24208 作者:442

1. MyBatis分页插件-页面帮助器配置依赖于maven配置文件pom.xml部署:

! 部署PageHelper分页插件- page helper---- dependencygroupidcom.github.page helper/groupidartifactidpagehelper/artifaction

! - -配置文件中的-- plugins位置必须满足要求。 如果不满足,将报告错误。 顺序是: properties?settings?typeAliases?typeHandlers?对象工厂?objectWrapperFactory?plugins?环境?数据库服务提供商?mappers? --plugins! -- com.github.PageHelper是pagehelper类所在的包名称--plugin interceptor=' com.github.page helper.page interceptor plugin plugins 2.使用分页servicepubliclistemployeeselectbylist ({ listemployeemployeelist=employee mapper.selectbyexamplewew } controller @ request mapping (/em pslist ' ) public string userlist (request param ) value='pn ',required=true,dequestred 在modelmodel}//查询之前调用并传递的pn的默认值为1,pageSize为5,这意味着从第一页到第一页显示五条记录。 pagehelper.startpage(pn,5 ); 寻呼查询紧跟在//startPage之后。 listemployeelist=employeeservice.selectbylist (; 使用//PageInfo包装查询的结果,只需将PageInfo传递到页面即可。 //封装详细的寻呼信息,传递包括我们调查的数据userList在内的连续显示的页数5。 pageinfoemployeepage=newpageinfoemployee (列表,5; model.addattribute('pageinfo ',page ); 返回' employee department-list '; } 3.PageInfo.class是插件中的类,可以非常方便地调用,分页可以再次提高性能。 //当前页面专用页面;//每页的数量private int pageSize; //当前页数private int size; //startRow和endRow很少使用,因此,在此具体使用方法为://页面上显示“startrow到endRow的size书的数据”//当前页面第一个元素的数据库中的行号private int ssist //数据库中当前页最后一个元素的行号private int endRow; //总记录数private long total; //总页数private int pages; //结果集私有列表列表; //上一页的私密int prepage; //下一页隐私下一页; //是否为第一页的专用布尔is第一页; //是否为最后一页的专用布尔islas tpage; //是否有上一页的专用布尔硬件; //是否有下一页的专用布尔散列页; //导航页码

数 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亲笔。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。