SSM整合
mybatis-spring 适配包
SSM 整合时,需要导入 mybatis-spring 适配包。
下载适配包:
mybatis-spring 适配包
该适配包下为 MyBatis 提供了两个类:
SqlSessionFactoryBean
:管理 MyBatis 操作数据库的会话对象 SqlSession,加载 MyBatis 核心配置文件,指定映射文件位置,配置核心配置文件中的一些配置等。
MapperScannerConfigurer
:生成指定包下所有 Mapper 接口的代理实现类对象,并将对象交给spring管理。
整合思路
- 搭建环境:
(1)创建一个动态 WEB 工程;
(2)导入 Spring、springMVC、MyBatis 需要使用的 jar 包;
(3)导入整合适配包;
(4)导入其他技术的一些支持包:数据库连接池、数据库驱动、日志… - Spring + SpringMVC:
springMVC:
(1)在 web.xml 中配置:字符编码过滤器CharacterEncodingFilter,REST风格过滤器HiddenHttpMethodFilter,SpringMVC核心控制器DispatchServlet,加载Spring配置文件的监听器ContextLoaderListener。
(2)创建 springMVC 配置文件:扫描控制层注解,配置视图解析器,开启tomcat默认servlet,开启mvc驱动。
Spring:
(1)创建Spring的配置文件: 扫描除@Controller注解以外的所有组件,引入资源文件,配置数据库连接池,配置事务管理器,开启事务注解,配置SqlSessionFactoryBean,配置MapperScannerConfigurer… - Spring + MyBatis:
MyBatis的 SqlSession的创建 .
MyBatis的 Mapper接口的代理实现类 - 测试:REST CRUD
项目结构
jar 包
web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置CharacterEncodingFilter-->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置HiddenHttpMethodFilter-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置DispatchServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
</web-app>
springMVC 配置文件
springMVC-config.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--只扫描控制层包下的注解-->
<context:component-scan base-package="com.mcc.ssm.control"/>
<!--配置视图解析器-->
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--开启默认Servlet-->
<mvc:default-servlet-handler/>
<!--开启mvc驱动-->
<mvc:annotation-driven/>
</beans>
Spring 配置文件
spring-config.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描除@Controller注解以外的全部注解-->
<context:component-scan base-package="com.mcc.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--引入资源文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--管理MyBatis的数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解驱动-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!--管理MyBatis操作数据库的会话对象SqlSession-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--设置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载MyBatis核心配置文件(路径,要以classpath:开始)-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--设置类的别名-->
<property name="typeAliasesPackage" value="com.mcc.ssm.bean"/>
<!--加载映射文件(路径,要以classpath:开始)-->
<property name="mapperLocations" value="classpath:com/mcc/ssm/mapper/*.xml"/>
</bean>
<!--生成指定包下所有Mapper接口的代理实现类对象,并将对象交给spring管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mcc.ssm.mapper"/>
</bean>
</beans>
MyBatis 配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置 -->
<settings>
<!-- mapUnderscoreToCamelCase:下划线格式转换为驼峰格式,如user_name => userName -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!--开启二级缓存-->
<!--<setting name="cacheEnabled" value="true"/>-->
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>
日志、数据库连接
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&rewriteBatchedStatements=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=密码
Bean
Emp.java
package com.mcc.ssm.bean;
public class Emp {
private Integer id;
private String eName;
private Integer age;
private String sex;
private Dept dept;
public Emp() {
}
public Emp(Integer id, String eName, Integer age, String sex) {
this.id = id;
this.eName = eName;
this.age = age;
this.sex = sex;
}
public Emp(Integer id, String eName, Integer age, String sex, Dept dept) {
this.id = id;
this.eName = eName;
this.age = age;
this.sex = sex;
this.dept = dept;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", eName='" + eName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", dept=" + dept +
'}';
}
}
Dept.java
package com.mcc.ssm.bean;
import java.util.List;
public class Dept {
private String id;
private String dName;
private List<Emp> empList;
public Dept() {
}
public Dept(String id, String dName) {
this.id = id;
this.dName = dName;
}
public Dept(String id, String dName, List<Emp> empList) {
this.id = id;
this.dName = dName;
this.empList = empList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department{" +
"id='" + id + '\'' +
", dName='" + dName + '\'' +
", emps=" + empList +
'}';
}
}
Mapper 接口、映射文件
EmpMapper.java
package com.mcc.ssm.mapper;
import com.mcc.ssm.bean.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.*;
public interface EmpMapper {
//获取全部员工信息
List<Emp> getAllEmps();
//根据id查询员工信息
Emp getEmpById(Integer id);
//修改员工信息
void updateEmp(Emp emp);
//删除员工信息
void deleteEmps(@Param("deleteId") String[] deleteId);
//删除单个员工信息
void deleteOneEmp(String id);
}
DeptMapper.java
package com.mcc.ssm.mapper;
import com.mcc.ssm.bean.Dept;
import java.util.List;
public interface DeptMapper {
//获取所有部门信息
List<Dept> getAllDepts();
}
EmpMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mcc.ssm.mapper.EmpMapper">
<!--List<Emp> getAllEmps();-->
<resultMap id="getallemps" type="Emp">
<id column="id" property="id"/>
<result column="eName" property="eName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="dept" javaType="Dept">
<id column="id" property="id"/>
<result column="dName" property="dName"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="getallemps">
SELECT e.id,e.eName,e.age,e.sex,e.departmentId ,d.dName
FROM employee e
LEFT JOIN department d
ON e.departmentId = d.id
</select>
<!--Emp getEmpById(Integer id);-->
<select id="getEmpById" resultMap="getallemps">
SELECT e.id,e.eName,e.age,e.sex,e.departmentId ,d.dName
FROM employee e
LEFT JOIN department d
ON e.departmentId = d.id
where e.id = #{id}
</select>
<!--void updateEmp(Emp emp);-->
<update id="updateEmp">
update employee set eName=#{eName},age=#{age},sex=#{sex},departmentId=#{dept.id}
where id = #{id}
</update>
<!--void deleteEmps(@Param("deleteId") String[] deleteId);-->
<delete id="deleteEmps">
delete from employee where id in
<foreach collection="deleteId" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!--void deleteOneEmp(String id);-->
<delete id="deleteOneEmp">
delete from employee where id = #{id}
</delete>
</mapper>
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mcc.ssm.mapper.DeptMapper">
<!--List<Dept> getAllDepts();-->
<resultMap id="getalldepts" type="Dept">
<id column="did" property="id"/>
<result column="dName" property="dName"/>
<collection property="empList" ofType="Emp">
<id column="eid" property="id"/>
<result column="eName" property="eName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
<select id="getAllDepts" resultMap="getalldepts">
select d.id did,d.dName,e.id eid,e.eName,e.age,e.sex
from department d
left join employee e
on e.id = d.id
</select>
</mapper>
Service 层
EmpService.java
package com.mcc.ssm.service;
import com.mcc.ssm.bean.Emp;
import java.util.List;
public interface EmpService {
//获取全部员工信息
List<Emp> getAllEmps();
//根据id查询员工信息
Emp getEmpById(Integer id);
//修改员工信息
void updateEmp(Emp emp);
//删除多个员工信息
void deleteEmps(String[] id);
//删除一个员工信息
void deleteOneEmp(String id);
}
DeptService.java
package com.mcc.ssm.service;
import com.mcc.ssm.bean.Dept;
import java.util.List;
public interface DeptService {
//获取全部部门信息
List<Dept> getAllDepts();
}
EmpServiceImpl.java
package com.mcc.ssm.service.serviceImpl;
import com.mcc.ssm.bean.Emp;
import com.mcc.ssm.mapper.EmpMapper;
import com.mcc.ssm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Emp> getAllEmps() {
return empMapper.getAllEmps();
}
@Override
public Emp getEmpById(Integer id) {
return empMapper.getEmpById(id);
}
@Override
public void updateEmp(Emp emp) {
empMapper.updateEmp(emp);
}
@Override
public void deleteEmps(String[] id) {
empMapper.deleteEmps(id);
}
@Override
public void deleteOneEmp(String id) {
empMapper.deleteOneEmp(id);
}
}
DeptServiceImpl.java
package com.mcc.ssm.service.serviceImpl;
import com.mcc.ssm.bean.Dept;
import com.mcc.ssm.mapper.DeptMapper;
import com.mcc.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> getAllDepts() {
return deptMapper.getAllDepts();
}
}
控制层
EmpControllor.java
package com.mcc.ssm.control;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mcc.ssm.bean.Dept;
import com.mcc.ssm.bean.Emp;
import com.mcc.ssm.mapper.DeptMapper;
import com.mcc.ssm.page.PageUtils;
import com.mcc.ssm.service.DeptService;
import com.mcc.ssm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.function.ServerRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
@Controller
public class EmpController {
@Autowired
private EmpService empService;
@Autowired
private DeptService deptService;
//emps:查询,emp:修改
//显示全部员工信息
@RequestMapping(value="/emps/{pageNum}",method=RequestMethod.GET)
public String getAll(Model model, @PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
//设置页面
PageHelper.startPage(pageNum,2);
List<Emp> emps = empService.getAllEmps();
//设置分页条
PageInfo<Emp> pageInfo = new PageInfo<>(emps, 3);
//为每个分页条绑定超链接
String page = PageUtils.getPage(pageInfo, request);
//数据回显
model.addAttribute("empList", emps);
//分页回显
model.addAttribute("page", page);
return "empList";
}
//点击修改按钮后,跳转到修改页面
@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
public String toUpdatePage(@PathVariable("id")Integer id, Model model){
//查询Emp对象
Emp emp = empService.getEmpById(id);
//将Emp对象保存到Request域中
model.addAttribute("emp",emp);
//将性别信息保存为Map,实现在updatePage上的回显
HashMap<String, String> map = new HashMap<>();
map.put("男","男");
map.put("女","女");
model.addAttribute("sex",map);
//获取全部部门信息
List<Dept> depts = deptService.getAllDepts();
model.addAttribute("depts",depts);
return "updatePage";
}
//修改员工信息
@RequestMapping(value="/emp",method=RequestMethod.PUT)
public String updateEmp(Emp emp, HttpSession session){
//修改员工信息
empService.updateEmp(emp);
//获取Session域中保存的当前页码,将页面重定向到之前页码
Object currentPageNum = session.getAttribute("currentPageNum");
return "redirect:/emps/"+currentPageNum;
}
//删除员工信息
@RequestMapping(value="/emps",method=RequestMethod.DELETE)
public String deleteEmp(String[] deleteId){
System.out.println(1);
empService.deleteEmps(deleteId);
return "redirect:/emps/1";
}
//删除一个员工信息
@RequestMapping(value="/empDel/{id}",method=RequestMethod.DELETE)
public String deleteOneEmp(@PathVariable("id") String id){
empService.deleteOneEmp(id);
return "redirect:/emps/1";
}
}
分页
PageUtils.java
package com.mcc.ssm.page;
import com.github.pagehelper.PageInfo;
import javax.servlet.http.HttpServletRequest;
public class PageUtils {
//为分页之后的列表添加超链接
public static<T> String getPage(PageInfo<T> pageInfo, HttpServletRequest request){
StringBuilder stringBuilder = new StringBuilder();
String path = request.getContextPath() + "/";
//首页
if(pageInfo.isHasPreviousPage()){
stringBuilder.append("<a href='"+path+"emps/1"+"'>"+"首页"+"</a>");
}
stringBuilder.append(" ");
//上一页
if(pageInfo.isHasPreviousPage()){
stringBuilder.append("<a href='"+path+"emps/"+pageInfo.getPrePage()+"'>"+"上一页"+"</a>");
}
stringBuilder.append(" ");
//页码
int[] nums = pageInfo.getNavigatepageNums();
for (int num : nums) {
if(num==pageInfo.getPageNum()){//获取当前页码
//将当前页码保存到Session域中
request.getSession().setAttribute("currentPageNum", pageInfo.getPageNum());
stringBuilder.append("<a href='"+path+"emps/"+num+"' style='color:red;'>"+num+"</a>");
stringBuilder.append(" ");
}else{
stringBuilder.append("<a href='"+path+"emps/"+num+"'>"+num+"</a>");
stringBuilder.append(" ");
}
}
//下一页
if(pageInfo.isHasNextPage()){
stringBuilder.append("<a href='"+path+"emps/"+pageInfo.getNextPage()+"'>"+"下一页"+"</a>");
}
stringBuilder.append(" ");
//尾页
if(pageInfo.isHasNextPage()){
stringBuilder.append("<a href='"+path+"emps/"+pageInfo.getPages()+"'>"+"尾页"+"</a>");
}
//StringBuilder转化为字符串返回
String pageStr = stringBuilder.toString();
return pageStr;
}
}
页面
首页
index.jsp
<%--
Created by IntelliJ IDEA.
User: MCC
Date: 2021/2/27
Time: 11:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<a href="${pageContext.servletContext.contextPath}/emps/1">点击显示全部员工信息</a>
</body>
</html>
显示全部员工信息页面
empList.jsp
<%--
Created by IntelliJ IDEA.
User: MCC
Date: 2021/2/27
Time: 17:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>全部员工</title>
<%-- 最后添加 / 才会被解析为文件夹,没有斜杠被解析为文件 --%>
<base href="${pageContext.servletContext.contextPath}/">
<link rel="stylesheet" href="css/index_work.css"/>
<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(function(){
//删除单个员工信息
$("a.delete").click(function(){
$("#deleteSingleForm").attr("action",$(this).attr("href")).submit();
return false;
});
//点击全选框时将每个员工前面的复选框也选中
$("#checkAll").click(function(){
$(".check").prop("checked",$(this).prop("checked"));
});
//当所有员工的复选框选中之后,全选框自动选中,当有一个复选框没被选中,全选框也不被选中
//为所有的checkbox绑定单击事件,因为不知道最后点击哪个checkbox时,会触发事件
$("input[name='deleteId']:checkbox").click(function(){
//获取checkbox复选框有多少个
var checkboxNum = $(":checkbox[name='deleteId']").length;
//获取复选框中有多少个被选中了
var checkedNum = $(":checkbox[name='deleteId']:checked").length;
$("#checkAll").prop("checked",checkboxNum==checkedNum);
});
//批量删除选中的员工
$("#deleteChecked").click(function(){
$("#deleteCheckedForm").attr("action",$(this).attr("href")).submit();
});
});
</script>
</head>
<body>
<form id="deleteCheckedForm" method="post" action="emps">
<input type="hidden" name="_method" value="delete"/>
<table>
<tr>
<th>
<input id="checkAll" type="checkbox"/>
</th>
<th>ID</th>
<th>eName</th>
<th>age</th>
<th>sex</th>
<th>dName</th>
<th>option</th>
</tr>
<c:forEach items="${empList}" var="emp">
<tr>
<th>
<input class="check" type="checkbox" name="deleteId" value="${emp.id}"/>
</th>
<td>${emp.id}</td>
<td id="eName">${emp.eName}</td>
<td>${emp.age}</td>
<td>${emp.sex}</td>
<td>${emp.dept.dName}</td>
<td><a href="emp/${emp.id}">修改 </a>
<%--<form action="empDel/${emp.id}" method="post">--%>
<%--<input type="hidden" name="_method" value="delete"/>--%>
<%--<input type="submit" value="删除"/>--%>
<%--</form>--%>
<a class="delete" href="empDel/${emp.id}">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
<a id="deleteChecked" href="emps">删除选中</a>
</td>
<td colspan="7">${page}</td>
</tr>
</table>
<input type="submit" value="删除选中"/>
</form>
<form id="deleteSingleForm" method="post">
<input type="hidden" name="_method" value="delete"/>
</form>
</body>
</html>
修改页面
updatePage.jsp
<%--
Created by IntelliJ IDEA.
User: MCC
Date: 2021/2/28
Time: 13:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>修改</title>
<link rel="stylesheet" href="${pageContext.servletContext.contextPath}/css/index_work.css">
<base href="${pageContext.servletContext.contextPath}/"/>
</head>
<body>
<form:form action="emp" method="post" modelAttribute="emp">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" value="${emp.id}">
<table>
<tr>
<th colspan="2">修改员工:${emp.eName}的信息</th>
</tr>
<tr>
<td>姓名</td>
<td>
<form:input path="eName"/>
</td>
</tr>
<tr>
<td>年龄</td>
<td>
<form:input path="age"/>
</td>
</tr>
<tr>
<td>性别</td>
<td>
<form:radiobuttons path="sex" items="${sex}"/>
</td>
</tr>
<tr>
<td>部门</td>
<td>
<form:select path="dept.id">
<form:options items="${depts}" itemLabel="dName" itemValue="id"/>
</form:select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>