博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
PX4官方用户和开发手册的首页面是会给你选择英文和中文的
查看>>
网络协议栈我是不是可以这么理解,就是把你要发送的数据自动处理成TCPIP格式的消息发出去,这种底层的转换不需要你弄了。
查看>>
除了LwIP还有uIP
查看>>
《跟工程师学嵌入式开发》这本书最后的终极项目我反而觉得有说头
查看>>
博士的申请考核制
查看>>
MAVLink学习之路05_MAVLink应用编程接口分析(也有讲STM32下的收发函数)
查看>>
找到了中文版的mavlink手册
查看>>
浅谈飞控开发的仿真功能
查看>>
我觉得在室内弄无人机开发装个防撞机架还是很有必要的,TBUS就做得很好。
查看>>
serial也是见到很多次了,似乎它就是一种串行通信协议
查看>>
TBUS的一些信息
查看>>
PX4+激光雷达在gazebo中仿真实现(古月居)
查看>>
专业和业余的区别就在于你在基础在基本功打磨练习花的时间
查看>>
通过mavlink实现自主航线的过程笔记
查看>>
Ardupilot飞控Mavlink代码学习
查看>>
这些网站有一些嵌入式面试题合集
查看>>
我觉得刷题是有必要的,不然小心实际被问的时候懵逼,我觉得你需要刷个50份面试题。跟考研数学疯狂刷卷子一样!
查看>>
我觉得嵌入式面试三要素:基础吃透+项目+大量刷题,缺一不可。不刷题是不行的。而且得是大量刷,刷出感觉套路,别人做题都做得是固定题型套路条件反射了,你还在那慢慢理解慢慢推是不行的,也是考研的教训。
查看>>
现在来看,做个普罗米修斯的docker镜像对我而言并不难,对PX4仿真环境配置也熟悉了。
查看>>
删除docker容器和镜像的命令
查看>>