写点什么

Spring MVC 框架:第六章:传统增删改查

作者:Java高工P7
  • 2021 年 11 月 11 日
  • 本文字数:5715 字

    阅读完需:约 19 分钟

装配 EmpDao


装配 DeptDao


创建 Handler


装配 Service


创建 index.jsp


点一个超链接,到目标页面显示 Employee 的 list


根据 id 删除 Employee


创建 Employee


准备表单


执行保存操作


更新 Employee


回显表单


执行更新操作


1.实体类


Department 类


public class Department {


private String deptId;


private String deptName;


public Department() {


}


public Department(String deptId, String deptName) {


super();


this.deptId = deptId;


this.deptName = deptName;


}


public String getDeptId() {


return deptId;


}


public void setDeptId(String deptId) {


this.deptId = deptId;


}


public String getDeptName() {


return deptName;


}


public void setDeptName(String deptName) {


this.deptName = deptName;


}


@Override


public String toString() {


return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";


}


}


Employee 类:


public class Employee {


private String empId;


private String empName;


private String ssn;


private Department department;


public Employee(String empId, String empName, String ssn, Department department) {


super();


this.empId = empId;


this.empName = empName;


this.ssn = ssn;


this.department = department;


}


public Employee() {


}


public String getEmpId() {


return empId;


}


public void setEmpId(String empId) {


this.empId = empId;


}


public String getEmpName() {


return empName;


}


public void setEmpName(String empName) {


this.empName = empName;


}


public String getSsn() {


return ssn;


}


public void setSsn(String ssn) {


this.ssn = ssn;


}


public Department getDepartment() {


return department;


}


public void setDepartment(Department department) {


this.department = department;


}


@Override


public String toString() {


return "Employee [empId=" + empId + ", empName=" + empName + ", ssn=" + ssn + ", department=" + department


  • "]";


}


}


2.Dao


EmpDao:


package com.dao;


import java.util.ArrayList;


import java.util.HashMap;


import java.util.List;


import java.util.Map;


import java.util.UUID;


import org.springframework.stereotype.Repository;


import com.pojo.Department;


import com.pojo.Employee;


@Repository


public class EmpDao {


private static Map<String, Employee> dataMap;


static {


dataMap = new HashMap<>();


String empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "乔峰", "SSN001", DeptDao.getDeptByName("市场部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "虚竹", "SSN002", DeptDao.getDeptByName("市场部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "段誉", "SSN003", DeptDao.getDeptByName("市场部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "鸠摩智", "SSN004", DeptDao.getDeptByName("技术部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "萧远山", "SSN005", DeptDao.getDeptByName("技术部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "慕容复", "SSN006", DeptDao.getDeptByName("技术部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "段正淳", "SSN007", DeptDao.getDeptByName("公关部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "段延庆", "SSN008", DeptDao.getDeptByName("公关部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "丁春秋", "SSN009", DeptDao.getDeptByName("销售部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "无崖子", "SSN010", DeptDao.getDeptByName("人事部")));


empId = UUID.randomUUID().toString();


dataMap.put(empId, new Employee(empId, "慕容博", "SSN011", DeptDao.getDeptByName("人事部")));


}


public void saveEmp(Employee employee) {


String empId = UUID.randomUUID().toString();


employee.setEmpId(empId);


String deptId = employee.getDepartment().getDeptId();


Department department = DeptDao.getDeptById(deptId);


employee.setDepartment(department);


dataMap.put(empId, employee);


}


public void removeEmp(String empId) {


dataMap.remove(empId);


}


public void updateEmp(Employee employee) {


String deptId = employee.getDepartment().getDeptId();


Department department = DeptDao.getDeptById(deptId);


employee.setDepartment(department);


dataMap.put(employee.getEmpId(), employee);


}


public Employee getEmpById(String empId) {


return dataMap.get(empId);


}


public List<Employee> getEmpList() {


return new ArrayList<>(dataMap.values());


}


}


DeptDao:


package com.dao;


import java.util.ArrayList;


import java.util.HashMap;


import java.util.List;


import java.util.Map;


import java.util.UUID;


import org.springframework.stereotype.Repository;


import com.pojo.Department;


@Repository


public class DeptDao {


private static Map<String, Department> dataMap;


private static Map<String, Department> namedMap;


static {


dataMap = new HashMap<>();


namedMap = new HashMap<>();


Department department = new Department(UUID.randomUUID().toString(), "市场部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


department = new Department(UUID.randomUUID().toString(), "销售部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


department = new Department(UUID.randomUUID().toString(), "行政部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


department = new Department(UUID.randomUUID().toString(), "人事部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


department = new Department(UUID.randomUUID().toString(), "技术部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


department = new Department(UUID.randomUUID().toString(), "公关部");


dataMap.put(department.getDeptId(), department);


namedMap.put(department.getDeptName(), department);


}


public static Department getDeptByName(String deptName) {


return namedMap.get(deptName);


}


public static Department getDeptById(String uuid) {


return dataMap.get(uuid);


}


public List<Department> getDeptList() {


return new ArrayList<>(dataMap.values());


}


}


3.Services:


package com.services;


import java.util.List;


import or


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


g.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import com.dao.DeptDao;


import com.dao.EmpDao;


import com.pojo.Department;


import com.pojo.Employee;


@Service


public class Services {


@Autowired


private DeptDao deptDao;


@Autowired


private EmpDao empDao;


public List<Department> deptDaoList(){


return deptDao.getDeptList();


};


public List<Employee> empDaoList(){


return empDao.getEmpList();


}


public void saveData(Employee employee) {


empDao.saveEmp(employee);


}


public void delectData(String empId) {


empDao.removeEmp(empId);


}


public Employee emdit(String empId) {


Employee empById = empDao.getEmpById(empId);


return empById;


}


public void updateEmp(Employee employee) {


empDao.updateEmp(employee);


}


}


4.handled:


package com.handled;


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 com.pojo.Department;


import com.pojo.Employee;


import com.services.Services;


@Controller


public class Handled {


@Autowired


private Services services;


@RequestMapping("/list")


public String list(Model model) {


List<Employee> empDaoList = services.empDaoList();


model.addAttribute("list", empDaoList);


return "dataList";


}


@RequestMapping("/addData")


public String toAdd(Model model) {


List<Department> deptDaoList = services.deptDaoList();


model.addAttribute("deptList", deptDaoList);


return "dataAdd";


}


@RequestMapping("/submitData")


public String submit(Employee employee) {


services.saveData(employee);


return "redirect:/list";


}


@RequestMapping("/removeData")


public String removeData(String empId) {


services.delectData(empId);


return "redirect:/list";


}


@RequestMapping("/emditData")


public String emditData(String empId,Model model) {


Employee emdit = services.emdit(empId);


List<Department> deptDaoList = services.deptDaoList();


model.addAttribute("emdit",emdit);


model.addAttribute("deptList",deptDaoList);


return "dataEdit";


}


@RequestMapping("/submitEmpData")


public String submitEmp(Employee employee) {


services.updateEmp(employee);


return "redirect:/list";


}


}


spring-mvc.xml:


<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xmlns:context="http://www.springframework.org/schema/context"


xmlns:mvc="http://www.springframework.org/schema/mvc"


xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<context:component-scan base-package="com.*" />


<bean id="viewResolver"


class="org.springframework.web.servlet.view.InternalResourceViewResolver">


<property name="prefix" value="/WEB-INF/page/" />


<property name="suffix" value=".jsp" />


</bean>


mvc:annotation-driven</mvc:annotation-driven>


<mvc:default-servlet-handler />


</beans>


web.xml:


<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xmlns="http://java.sun.com/xml/ns/javaee"


xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"


id="WebApp_ID" version="2.5">


<!-- The front controller of this Spring Web application, responsible for


handling all application requests -->


<servlet>


<servlet-name>springDispatcherServlet</servlet-name>


<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


<init-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath:spring-mvc.xml</param-value>


</init-param>


<load-on-startup>1</load-on-startup>


</servlet>


<servlet-mapping>


<servlet-name>springDispatcherServlet</servlet-name>


<url-pattern>/</url-pattern>


</servlet-mapping>


<filter>


<filter-name>CharacterEncodingFilter</filter-name>


<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>


<init-param>


<param-name>encoding</param-name>


<param-value>UTF-8</param-value>


</init-param>


<init-param>


<param-name>forceEncoding</param-name>


<param-value>true</param-value>


</init-param>


</filter>


<filter-mapping>


<filter-name>CharacterEncodingFilter</filter-name>


<url-pattern>/*</url-pattern>


</filter-mapping>


</web-app>


index.jsp 页面:


<%@ page language="java" contentType="text/html; charset=UTF-8"


pageEncoding="UTF-8"%>


<!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>Insert title here</title>


</head>


<body>


<a href="${pageContext.request.contextPath }/list">去列表</a>


</body>


</html>


dataList.jsp:


<%@ 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>Insert title here</title>


<script type="text/javascript" src="${pageContext.request.contextPath }/script/jquery-1.7.2.js"></script>


<script type="text/javascript">


$(function(){


$(".remove").click(function(){


var data = $(this).parents("tr").children("td:eq(1)").text();


var confirm = window.confirm("你确定要"+data+"删除吗?");


if(!confirm){


return false;


}


});


});


</script>


</head>


<body>

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
Spring MVC框架:第六章:传统增删改查