PageHelper分页使用及测试

一、引入相关jar包

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

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

<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!--分页参数合理化  -->
			<property name="reasonable" value="true"/>		
		</plugin>
	</plugins>

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

三、编写Controller

@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

	@Autowired
	EmployeeMapper employeeMapper;

	/**
	 * 查询所有员工
	 * 
	 * @return
	 */
	public List<Employee> getAll() {
		return employeeMapper.selectByExampleWithDept(null);
	}

五、测试

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

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

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

@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());
		}
	}
}

六、使用

<%@ 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>

猜你喜欢

转载自blog.csdn.net/qq_38151401/article/details/79870500