博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSM-CRUD(2)---查询
阅读量:4086 次
发布时间:2019-05-25

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

逻辑分析:启动项目后,进入到index.jsp页面,点击index.jsp页面的查询员工链接,发出查询员工列表请求,来到empList.jsp页面。

empList.jsp页面使用插件pageHelper完成分页功能。使用pageHelper需要给它几个参数:我要查询第几页;每页查询几条记录;需要连续显示几条记录?

第1步、index.jsp页面如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    		
员工查询
部门查询

第2步、在src/main/java下新建包com.cn.controller,然后在该包下新建类EmpController,代码如下:

package com.cn.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * 处理员工CRUD请求的控制层 * */@Controllerpublic class EmpController {	@RequestMapping("/empList")	public String toEmpList(){		return "empList";	}}

第3步、在WEB-INF下新建文件夹jsp,然后在jsp下新建empList.jsp页面,页面内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>      <%    	pageContext.setAttribute("path",request.getContextPath());    %>    员工列表	
SSM-CRUD
序号 员工名称 性别 邮箱 所在部门 操作
1 张三 zhangsan@163.com 研发部
当前记录数:xxx

注:逻辑是点击index.jsp页面的员工查询,<a href="empList">会根据empList去Controller中查找名为empList的映射,找到后会执行EmpController中的toEmpList()方法,然后返回empList页面,去/WEB-INF/jsp/empList.jsp返回响应。

执行结果如下:

*************************************************************************************************************************************************上面是固定的写法,实际应用中需要去后台查询数据,将后台的数据分页显示到前端页面中

第4步、编写EmpController实现分页,代码如下:

编写前,需要在pom.xml中引入分页插件PageHelper的相关依赖,如下:

com.github.pagehelper
pagehelper
5.0.0
com.github.miemiedev
mybatis-paginator
1.2.17
com.github.jsqlparser
jsqlparser
0.9.5

EmpController分页代码如下:

package com.cn.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.cn.bean.Employee;import com.cn.service.EmpService;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;/** * 处理员工CRUD请求的控制层 * */@Controllerpublic class EmpController {	@Autowired	private EmpService empService;	/**	 * 跳转员工页面	 * */	@RequestMapping("/empList")	public String toEmpList(){		return "empList";	}		/**	 * 分页查询员工数据	 * */	@RequestMapping("/emps")	public String getEmpByPage(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){		//在查询之前传入分页参数:起始页和每页记录数		PageHelper.startPage(pn, 5);		//分页查询,需要使用empService		List
empList=empService.getAll(); //使用pageInfo对结果进行包装,只需要将pageInfo交给页面处理即可(pageInfo里面封装了分页的详细信息 @SuppressWarnings({ "rawtypes", "unchecked" }) PageInfo pageInfo=new PageInfo(empList,5); model.addAttribute("pageInfo", pageInfo); //跳转到查询页面 return "empList"; }}

EmpService代码如下:

package com.cn.service;import java.util.List;import com.cn.bean.Employee;/** * 员工service * */public interface EmpService {	//查询所有员工	public List
getAll();}

EmpService实现类EmpServiceImpl代码如下:

package com.cn.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.cn.bean.Employee;import com.cn.dao.EmployeeMapper;import com.cn.service.EmpService;@Service("empService")public class EmpServiceImpl implements EmpService {		@Autowired	private EmployeeMapper empMapper;		//没有查询条件的,查询所有(带部门信息)	@Override	public List
getAll() { return empMapper.selectByExampleWithDept(null); }}

使用mock模拟请求,测试emps是否可以拿到返回值

package com.cn.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.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 com.cn.bean.Employee;import com.github.pagehelper.PageInfo;/** * 使用spring的单元测试来进行模拟发送请求的测试 * */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:spring-mvc.xml"})public class MVCTest {	//springMvc的IOC容器	@Autowired	private WebApplicationContext context;	//模拟mvc请求	private MockMvc mockMvc;	@Before	public void setup(){		mockMvc=MockMvcBuilders.webAppContextSetup(context).build();	}	@Test	public void testPage() throws Exception{		//模拟请求拿到返回值		MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "2")).andReturn();		//请求域中有pageInfo		PageInfo page=(PageInfo) result.getRequest().getAttribute("pageInfo");		System.out.println(page.getPageNum());		System.out.println(page.getTotal());				//得到员工数据		List
empList=page.getList(); for(Employee employee:empList){ System.out.println("ID:"+employee.getEmpId()+"员工名称:"+employee.getEmpName()); } }}

证明pageInfo中拿到值,那么再使用bootstrap的删格系统快速搭建简单的界面,页面empList.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>      <%    	pageContext.setAttribute("path",request.getContextPath());    %>    员工列表	
SSM-CRUD
序号 员工名称 性别 邮箱 所在部门 操作
${emp.empId} ${emp.empName} ${emp.gender=="M"?"男":"女"} ${emp.email} ${emp.department.deptName}
当前第:${pageInfo.pageNum}页,总共:${pageInfo.pages}页,总共:${pageInfo.total}条记录

注意:一定要在mybatis的配置文件mybatis-conf.xml中配置分页插件

运行项目后,页面效果如下

*************************************************************************************************************************************************至此,页面的查询功能基本完成,分页查询也已实现

转载地址:http://hluii.baihongyu.com/

你可能感兴趣的文章
SQL join
查看>>
JavaScript实现页面无刷新让时间走动
查看>>
CSS实例:Tab选项卡效果
查看>>
前端设计之特效表单
查看>>
前端设计之CSS布局:上中下三栏自适应高度CSS布局
查看>>
Java的时间操作玩法实例若干
查看>>
JavaScript:时间日期格式验证大全
查看>>
pinyin4j:拼音与汉字的转换实例
查看>>
XML工具代码:SAX从String字符串XML内获取指定节点或属性的值
查看>>
时间日期:获取两个日期相差几天
查看>>
责任链模式 Chain of Responsibility
查看>>
高并发与大数据解决方案概述
查看>>
解决SimpleDateFormat线程安全问题NumberFormatException: multiple points
查看>>
MySQL数据库存储引擎简介
查看>>
处理Maven本地仓库.lastUpdated文件
查看>>
Kafka | 请求是怎么被处理的?
查看>>
Java并发编程1-线程池
查看>>
CentOS7,玩转samba服务,基于身份验证的共享
查看>>
计算机网络-网络协议模型
查看>>
计算机网络-OSI各层概述
查看>>