Spring+SpringMVC+Mybatis实现增删改查--(二)SSM分页查询页面搭建(通过URI请求)
在前面搭建好的Spring+Spring MVC+Mybatis项目基础环境搭建项目中,进行操作。
查询:
1.访问index.jsp
(http://localhost:8080/ssm_crud/)
2.index.jsp页面转发查询员工列表请求
(<jsp:forward page="/emps"></jsp:forward>)
3.EmployeeController接受请求,通过EmployeeService调用查询全部请求。
(List<Employee> emps=employeeService.getAll();)
4.EmployeeService调用查询员工和部门的方法
(employeeMapper.selectByExampleWithDept(null);)
5.在EmployeeController中对查询过来的数据利用pagehelper分页插件封装到pageinfo中。
//这里写一个测试方法来测试是否能够成功查询分页
6.返回到list.jsp页面进行展示分页查询过来的数据
具体流程:
1.src/main/webapp/index.jsp页面转发URI请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:forward page="/emps"></jsp:forward>
2.src/main/java/com.lcz.crud.service/EmployeeService.java
查询所有员工的方法
package com.lcz.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lcz.crud.bean.Employee;
import com.lcz.crud.dao.EmployeeMapper;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
/**
* 查询所有员工
* @return
*/
public List<Employee> getAll() {
return employeeMapper.selectByExampleWithDept(null);
}
}
3.分页pagehelper插件加载,地址可见
(1)在pom.xml中引入pagehelper包
<!-- 分页查询pagehelper插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
(2)mybaits_config.xml配置插件
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
4.src/main/java/com.lcz.crud.controller/EmployController.java
实现分页查询员工方法
package com.lcz.crud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcz.crud.bean.Employee;
import com.lcz.crud.service.EmployeeService;
/**
* 处理员工CRUD请求
* @author LvChaoZhang
*
*/
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
/**
* 查询员工数据(分页查询)
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model ) {
//这不是一个分页查询
//引入PageHelper分页插件
//在查询之前只需要调用,传入页面以及每页的大小
PageHelper.startPage(pn,5);
//startpage后面紧跟的这个查询就是一个分页查询
List<Employee> emps=employeeService.getAll();
//用PageInfo对结果进行包装,只需要pageInfo交给页面,封装了详细的分页信息
//包括有我们查询出来的数据,传入连续显示的页数
PageInfo page = new PageInfo(emps,5);
model.addAttribute("pageInfo",page);
return "list";
}
}
5.在src/main/java/com.lcz.crud.test
书写测试方法,测试看是否能够获得分页查询的数据(当前页码、总页码、总记录数、在页面中需要连续显示的页码)
package com.lcz.crud.test;
import java.util.List;
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.MockMvcBuilder;
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 com.github.pagehelper.PageInfo;
import com.lcz.crud.bean.Employee;
/**
* 使用spring测试模块提供的测试请求功能,测试crud请求的正确性
* @author LvChaoZhang
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:spring_mvc.xml"})
public class MvcTest {
//传入SpringMVC的IOC
@Autowired
WebApplicationContext context;
//虚拟MVC请求,获取到处理结果
MockMvc mockMvc;
//每次初始化
@Before
public void initMokcMvc() {
mockMvc=MockMvcBuilders.webAppContextSetup(context).build();
}
/**
* 测试查询分页
* @throws Exception
*/
@Test
public void testPage() throws Exception {
//模拟请求拿到返回值
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).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.out.print(""+i);
}
//获取员工数据
List<Employee> list = pi.getList();
for(Employee employee:list) {
System.out.println("ID:"+employee.getEmpId()+"==>Name:"+employee.getEmpName());
}
}
}
6.返回到src/webapp/WEB-INF/views/list.jsp页面
通过bootstrap来搭建页面,这里bootstrap界面是通过绝对路径来引入的。
若想查看pageinfo中的变量,可以用快捷键ctrl+shift+t搜索pageinfo,打开pageinfo的实现类,其中有pageinfo实现的方法和变量使用。
<%@ 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">
<title>员工列表</title>
<%
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
<!-- web路径:
不以/开始的相对路径,找资源,以当前资源的路径为基准,经常容易出问题。
以/开始的相对路径,找资源,以服务器的路径为标准(http://localhost:8080);需要加上项目名
http://localhost:3306/crud
-->
<script type="text/javascript" src="${APP_PATH }/static/js/jquery-3.3.1.min.js"></script>
<link href="${APP_PATH }/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="${APP_PATH }/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</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>
<br>
<!-- 显示表格数据 -->
<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>