我是靠谱客的博主 微笑紫菜,这篇文章主要介绍PageHelper分页使用及测试,现在分享给大家,希望可以做个参考。

一、引入相关jar包

复制代码
1
2
3
4
5
6
<!-- pagehelper分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>

二·、在SpringMVC配置文件中进行配置

复制代码
1
2
3
4
5
6
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--分页参数合理化 --> <property name="reasonable" value="true"/> </plugin> </plugins>

注意:此配置要放在别名下面

三、编写Controller

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Autowired EmployeeService employeeService; /** * 查询员工数据(分页查询) * * @return */ @RequestMapping("/emps") public String getEmps(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) { //使用分页插件 PageHelper.startPage(pn, 5); List<Employee> emps = employeeService.getAll(); //使用PageInfo包装查询后的结果,只需要将PageInfo交个页面就好了 //可传入连续显示的页数 PageInfo page = new PageInfo(emps,5); model.addAttribute("pageInfo", page); return "list";//由于视图解析器,会跳转到/WEB-INF/views/目录下 }

四、编程Service

复制代码
1
2
3
4
5
6
7
8
9
10
11
@Autowired EmployeeMapper employeeMapper; /** * 查询所有员工 * * @return */ public List<Employee> getAll() { return employeeMapper.selectByExampleWithDept(null); }

五、测试

注意:Spring4测试的时候需要servlet3.0支持

1.需要引入Spring和SpringMVC的配置文件

2.PageInfo里面封装了很多信息

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={"classpath:spring/applicationContext-*.xml","classpath:spring/dispatcherServlet.xml"}) public class MVCTest { //传入SpringMVC的ioc //@Autowired只能注入自己,要注入WebApplicationContext里面的东西 //还需要一个注解@WebAppConfiguration @Autowired WebApplicationContext context; //虚拟mvc请求,获取处理结果 MockMvc mockMvc ; @Before public void initMokcMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPageHelper() throws Exception { //模拟请求,拿到返回值 MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn","1")).andReturn(); //请求成功后,请求域中会有pageInfo,我们可以取出pageInfo进行验证 MockHttpServletRequest request = result.getRequest(); PageInfo pi = (PageInfo) request.getAttribute("pageInfo"); System.out.println("当前页码:"+pi.getPageNum()); System.out.println("总页码:"+pi.getPages()); System.out.println("总记录数:"+pi.getTotal()); System.out.println("在页面需要连续显示的页码"); int []nums = pi.getNavigatepageNums(); for(int i : nums){ System.err.println(" "+i); } //获取员工数据 List<Employee> list = pi.getList(); for(Employee employee : list){ System.out.println("ID:"+employee.getEmpId()+"name=>"+employee.getEmpName()); } } }

六、使用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <% pageContext.setAttribute("APP_PATH", request.getContextPath()); %> <!-- web路径: 不以/开始的相对路径,找资源,以当前资源的路径为基准,经常容易出问题。 以/开始的相对路径,找资源,以服务器的路径为标准(http://localhost:3306);需要加上项目名 http://localhost:3306/crud --> <link href="${APP_PATH}/static/bootstrap/css/bootstrap.min.css" rel="stylesheet"/> <script type="text/javascript" src="${APP_PATH}/static/bootstrap/js/jquery.js"></script> <script type="text/javascript" src="${APP_PATH}/static/bootstrap/js/bootstrap.min.js"></script> <title>员工列表</title> </head> <body> <!-- 搭建显示页面 --> <div class="container"> <!-- 标题 --> <div class="row"> <div class="col-md-12"> <h1>SSM-CRUD</h1> </div> </div> <!-- 按钮 --> <div class="row"> <div class="col-md-4 col-md-offset-8"> <button class="btn btn-primary">新增</button> <button class="btn btn-danger">删除</button> </div> </div> <!-- 显示表格数据 --> <div class="row"> <div class="col-md-12"> <table class="table table-hover"> <tr> <th>#</th> <th>empName</th> <th>gender</th> <th>email</th> <th>deptName</th> <th>操作</th> </tr> <c:forEach items="${pageInfo.list }" var="emp"> <tr> <th>${emp.empId }</th> <th>${emp.empName }</th> <th>${emp.gender=="M"?"男":"女" }</th> <th>${emp.email }</th> <th>${emp.department.deptName }</th> <th> <button class="btn btn-primary btn-sm"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 编辑 </button> <button class="btn btn-danger btn-sm"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除 </button> </th> </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="${APP_PATH }/emps?pn=1">首页</a></li> <!-- 如果有上一页就显示上一页 --> <c:if test="${pageInfo.hasPreviousPage }"> <li><a href="${APP_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_Num"> <c:if test="${page_Num == pageInfo.pageNum }"> <li class="active"><a href="#">${page_Num }</a></li> </c:if> <c:if test="${page_Num != pageInfo.pageNum }"> <!-- 如果不是当前页,发生请求,带上页码数 --> <li><a href="${APP_PATH}/emps?pn=${page_Num }">${page_Num }</a></li> </c:if> </c:forEach> <!-- 如果有下一页就显示下一页 --> <c:if test="${pageInfo.hasNextPage }"> <li><a href="${APP_PATH }/emps?pn=${pageInfo.pageNum+1 }" aria-label="Next"> <span aria-hidden="true">»</span> </a></li> </c:if> <li><a href="${APP_PATH }/emps?pn=${pageInfo.pages}">末页</a></li> </ul> </nav> </div> </div> </div> </body> </html>

 

最后

以上就是微笑紫菜最近收集整理的关于PageHelper分页使用及测试的全部内容,更多相关PageHelper分页使用及测试内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部