【实战】Spring+SpringMVC+Mybatis实现增删改查--(二)SSM分页查询页面搭建(通过URI请求)
发布日期:2021-06-29 15:42:09 浏览次数:4 分类:技术文章

本文共 5847 字,大约阅读时间需要 19 分钟。

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

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;@Servicepublic class EmployeeService {		@Autowired	EmployeeMapper employeeMapper;	/**	 * 查询所有员工	 * @return	 */	public List
getAll() { return employeeMapper.selectByExampleWithDept(null); }}

3.分页pagehelper插件加载,地址可见

1)在pom.xml中引入pagehelper包

com.github.pagehelper
pagehelper
5.0.0

2)mybaits_config.xml配置插件

    

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 * */@Controllerpublic 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
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
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"%>
员工列表<% pageContext.setAttribute("APP_PATH", request.getContextPath());%>

SSM-CRUD

# empName gender email deptName 操作
${emp.empId } ${emp.empName} ${emp.gender=="M"?"男":"女"} ${emp.email } ${emp.department.deptName }
当前${pageInfo.pageNum}页,总共${pageInfo.pages}页,总共${pageInfo.total}记录数

转载地址:https://codingchaozhang.blog.csdn.net/article/details/80639569 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring+SpringMVC+Mybatis实现增删改查--(三)SSM分页查询页面搭建(通过json请求)
下一篇:【实战】Spring+SpringMVC+Mybatis实现增删改查--(一)SSM环境的搭建及整合

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月17日 19时24分38秒