网上创建Maven的教程有很多,在这里就不再累赘了,有以下几个注意点:
pom.xml
中的打包方式一定要为war
包,不配置默认是jar
包,在后面配置tomcat
的时候会找不到Artifacts
情况;Maven
项目是没有web.xml
的,需要进入Project Structure
进行配置,如下图所示;pom.xml
依赖如下所示,如果有需要可以直接copy
使用:
注意: 如果在项目启动过程中出现:validateConnection false
等错误,通常原因是因为druid
数据源的版本与MySql 8.0
版本不匹配导致的,可以参考文章:MySQL 8.0 驱动与阿里druid版本兼容操作
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>day25_ssmartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<spring.version>5.3.1spring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.11version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.2.0version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.12.1version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>3.0.12.RELEASEversion>
dependency>
dependencies>
project>
web.xml
配置的文件内容:
CharacterEncodingFilter
;PUT
和DELETE
的过滤器HiddenHttpMethodFilter
;DispatcherServlet
;ContextLoaderListener
; 1、2、3
点都很容易理解,在SpringMVC配置中都需要,那么整合的时候也必须可少。那下面就分析一下为什么还需要4、5
点。一个web
项目启动起来至少有两个IOC
容器:一个是SpringMVC管理的IOC
容器,一个是Spring管理的IOC
容器。这两个容器有什么关系吗?
答案是肯定的,根据MVC
三层架构知,controller层依赖service层,service层依赖mapper层;而SpringMVC管理的IOC
容器只管理controller层组件bean,Spring管理的组件IOC
容器管理除controler层的所有组件bean。
所以这两个容器之间肯定是存在关系的,Spring管理的IOC
容器必须要早于SpringMVC管理的IOC
容器生成。并结合官方文档可知:SpringMVC管理的IOC
容器是一个父容器,Spring管理的IOC
容器是一个子容器,所以Spring管理的IOC
容器必须早于SpringMVC管理的IOC
容器生成,所以就设置了4、5
条内容。
Spring提供了监听器ContextLoaderListener
,实现ServletContextListener
接口,可监听ServletContext
的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC
容器。web
应用中必须在web.xm
l中配置
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
context-param>
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web.xml
全部内容:
<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">
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
springmvc.xml
可以配置的文件内容:
multipartResolver
;(multipartResolver
是一个接口,实现类为CommonsMultipartResolver
)webapp/WEB-INF/templates
目录下创建 一个index.html
页面)springmvc.xml
全部内容,可以直接copy使用:
<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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="qc.lsm.controller">context:component-scan>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
bean>
property>
bean>
property>
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:default-servlet-handler>mvc:default-servlet-handler>
beans>
Spring配置文件中的内容
DataSourceTransactionManager
;SqlSessionFactory
的工程bean,这样就可以简化mybatis配置文件;mybatis-spring
提供,可以将指定包下所有的mapper接口创建动态代理并将这些代理动态作为IOC容器的bean管理;在SSM整合的时候添加了7-8
的原因在于简化mybatis
, 之前学习mybaits的时候很麻烦,要先加载mybaits-config.xml
配置文件,然后创建SqlSessionFactory
,获取Session
以及对应的mapper
等。
在SSM整合的时候进行了简化,第7项:可以直接在spring.xml
配置文件里面进行创建SqlSessionFactory
的工程bean,该组件里面的属性包括加载mybatis配置文件的路径,设置数据源,设置类型别名所对应的包以及映射文件的路径等,大大简化的mybatis-config.xml
的配置。在spring.xml
中进行配置后就不需要再mybatis-config.xml
中进行配置了。
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml">property>
<property name="dataSource" ref="dataSource">property>
<property name="typeAliasesPackage" value="qc.lsm.pojo">property>
bean>
第8项个人感觉非常牛皮,配置之后可以将指定包下所有的mapper接口创建动态代理并将这些代理动态作为IOC容器的bean管理。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="qc.lsm.mapper">property>
bean>
spring.xml
全部内容,可以直接copy使用:
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="qc.lsm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml">property>
<property name="dataSource" ref="dataSource">property>
<property name="typeAliasesPackage" value="qc.lsm.pojo">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="qc.lsm.mapper">property>
bean>
beans>
在学习mybatis的时候,mybatis-config.xml
配置文件内容如下:
DOCTYPE configuration
PUBLIC "-//MyBatis.org//DTD Config 3.0//EN"
"http://MyBatis.org/dtd/MyBatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
plugins>
<typeAliases>
<package name="com.atguigu.bean"/>
typeAliases>
<environments default="mysql_test">
<environment id="mysql_test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.atguigu.mapper"/>
mappers>
configuration>
但是为了简化mybaits,咱们在spring.xml
配置文件里面设置了SqlSessionFactory
的工厂bean,并在里面配置了下面的内容:
所以mybatis-config.xml
中的内容如下所示:
DOCTYPE configuration
PUBLIC "-//MyBatis.org//DTD Config 3.0//EN"
"http://MyBatis.org/dtd/MyBatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
plugins>
configuration>
log4j 8种日志级别:
日志级别等级按照从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF
Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG。
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>
public class Employee {
private Integer empId;
private String empName;
private Integer age;
private String sex;
private String email;
// 无参构造器,有参构造器,set/get方法,toString方法...
}
package qc.lsm.mapper;
import qc.lsm.pojo.Employee;
import java.util.List;
public interface EmployeeMapper {
/**
* 根据id查询单个员工的信息
* @param id
* @return
*/
Employee selectEmployeeById(Integer id);
/**
* 查询所有员工的信息
* @return
*/
List<Employee> selectAllEmployee();
/**
* 添加员工信息
* @param employee
* @return
*/
int addEmployee(Employee employee);
/**
* 根据员工id删除员工信息
* @param empId
* @return
*/
int deleteEmployee(Integer empId);
/**
* 根据员工id修改员工信息
* @param employee
* @return
*/
int updateEmployee(Employee employee);
}
注意:由于表t_emp
的字段名与类Employee
中的属性名不一致,所以在查询的时候使用了resultMap
标签;
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="qc.lsm.mapper.EmployeeMapper">
<resultMap id="employeeResultMap" type="Employee">
<id property="empId" column="emp_id">id>
<result property="empName" column="emp_name">result>
<result property="age" column="age">result>
<result property="sex" column="sex">result>
<result property="email" column="email">result>
resultMap>
<select id="selectEmployeeById" resultMap="employeeResultMap">
select * from t_emp where emp_id = #{id};
select>
<select id="selectAllEmployee" resultMap="employeeResultMap">
select * from t_emp;
select>
<insert id="addEmployee" parameterType="qc.lsm.pojo.Employee">
insert into t_emp values(null, #{empName}, #{age}, #{sex},#{email})
insert>
<delete id="deleteEmployee" parameterType="Integer">
delete from t_emp where emp_id = #{id}
delete>
<update id="updateEmployee" parameterType="Employee">
update t_emp set emp_name = #{empName}, age = #{age}, sex = #{sex}, email = #{email} where emp_id = #{empId};
update>
mapper>
EmployeeService接口
public interface EmployeeService {
/**
* 根据id查询单个员工的信息
* @param id
* @return
*/
Employee selectEmployeeById(Integer id);
/**
* 查询所有员工的信息
* @return
*/
List<Employee> selectAllEmployee();
/**
* 添加员工信息
* @param employee
* @return
*/
int addEmployee(Employee employee);
/**
* 根据员工id删除员工信息
* @param empId
* @return
*/
int deleteEmployee(Integer empId);
/**
* 根据员工id修改员工信息
* @param employee
* @return
*/
int updateEmployee( Employee employee);
}
EmployeeService接口的实现类EmployeeServiceImpl
package qc.lsm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.lsm.mapper.EmployeeMapper;
import qc.lsm.pojo.Employee;
import qc.lsm.service.EmployeeService;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public Employee selectEmployeeById(Integer id) {
System.out.println("employeeMapper: " + employeeMapper);
return employeeMapper.selectEmployeeById(id);
}
@Override
public List<Employee> selectAllEmployee() {
System.out.println("employeeMapper: " + employeeMapper);
return employeeMapper.selectAllEmployee();
}
@Override
public int addEmployee(Employee employee) {
return employeeMapper.addEmployee(employee);
}
@Override
public int deleteEmployee(Integer empId) {
return employeeMapper.deleteEmployee(empId);
}
@Override
public int updateEmployee(Employee employee) {
return employeeMapper.updateEmployee(employee);
}
}
响应码Code类
package qc.lsm.utils;
public class Code {
public static final Integer ADD_OK = 10001;
public static final Integer ADD_ERROR = 10000;
public static final Integer SELETE_OK = 20001;
public static final Integer SELETE_ERROR = 20000;
public static final Integer UPDATE_OK = 30001;
public static final Integer UPDATE_ERROR = 30000;
public static final Integer DELETE_OK = 40001;
public static final Integer DELETE_ERROR = 40000;
}
统一返回结果类Result
package qc.lsm.controller;
/**
* 定义统一返回格式类
*/
public class Result {
// 描述统一格式中的数据
private Object data;
// 描述统计格式中的编码,用于区分操作,可以简化配置0/1表示成功失败
private Integer code;
// 描述统一格式中的消息,可选属性
private String msg;
public Result() {
}
public Result(Object data, Integer code) {
this.data = data;
this.code = code;
}
public Result(Object data, Integer code, String msg) {
this.data = data;
this.code = code;
this.msg = msg;
}
// set/get方法
@Override
public String toString() {
return "Result{" +
"data=" + data +
", code=" + code +
", msg='" + msg + '\'' +
'}';
}
}
package qc.lsm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import qc.lsm.pojo.Employee;
import qc.lsm.service.EmployeeService;
import qc.lsm.utils.Code;
import java.util.List;
/**
* @author Mr.Lu
* @version 1.0
* @date 2023-09-08 20:13
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/selectEmployeeById/{id}")
public Result selectEmployeeById(@PathVariable("id") Integer id){
Employee employee = employeeService.selectEmployeeById(id);
if(employee == null){
return new Result(null, Code.SELETE_ERROR, "查询失败");
}
return new Result(employee,Code.SELETE_OK, "查询成功");
}
@RequestMapping("/selectAll")
public Result selectAllEmployee(){
List<Employee> employees = employeeService.selectAllEmployee();
if(employees == null){
return new Result(null, Code.SELETE_ERROR, "查询失败");
}
return new Result(employees, Code.SELETE_OK, "查询成功");
}
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public Result addEmployee(@RequestBody Employee employee){
System.out.println("employee: " + employee);
int count = employeeService.addEmployee(employee);
if(count > 0) {
List<Employee> employees = employeeService.selectAllEmployee();
return new Result(employees, Code.ADD_OK, "添加成功");
}
return new Result(null, Code.ADD_ERROR, "添加失败");
}
@RequestMapping("/deleteEmployeeById/{id}")
public Result deleteEmployeeById(@PathVariable("id") Integer id){
int count = employeeService.deleteEmployee(id);
if(count > 0){
List<Employee> employees = employeeService.selectAllEmployee();
return new Result(employees, Code.DELETE_OK, "删除成功");
}
return new Result(null, Code.DELETE_ERROR, "删除失败");
}
@RequestMapping("/updateEmployee")
public Result updateEmployee(@RequestBody Employee employee){
int count = employeeService.updateEmployee(employee);
if(count > 0){
return new Result(employeeService.selectEmployeeById(employee.getEmpId()), Code.UPDATE_OK, "更新成功"); }
return new Result(null, Code.UPDATE_ERROR, "更新失败");
}
}
测试1:根据id
查询员工信息http://localhost/ssm/employee/selectEmployeeById/2
@RequestMapping("/selectEmployeeById/{id}")
public Result selectEmployeeById(@PathVariable("id") Integer id){
Employee employee = employeeService.selectEmployeeById(id);
if(employee == null){
return new Result(null, Code.SELETE_ERROR, "查询失败");
}
return new Result(employee,Code.SELETE_OK, "查询成功");
}
测试2:查询所有员工信息http://localhost/ssm/employee/selectAll
@RequestMapping("/selectAll")
public Result selectAllEmployee(){
List<Employee> employees = employeeService.selectAllEmployee();
if(employees == null){
return new Result(null, Code.SELETE_ERROR, "查询失败");
}
return new Result(employees, Code.SELETE_OK, "查询成功");
}
测试3:添加员工http://localhost/ssm/employee/addEmployee
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public Result addEmployee(@RequestBody Employee employee){
System.out.println("employee: " + employee);
int count = employeeService.addEmployee(employee);
if(count > 0) {
List<Employee> employees = employeeService.selectAllEmployee();
return new Result(employees, Code.ADD_OK, "添加成功");
}
return new Result(null, Code.ADD_ERROR, "添加失败");
}
测试4:根据id删除员工信息http://localhost/ssm/employee/deleteEmployeeById/2
@RequestMapping("/deleteEmployeeById/{id}")
public Result deleteEmployeeById(@PathVariable("id") Integer id){
int count = employeeService.deleteEmployee(id);
if(count > 0){
List<Employee> employees = employeeService.selectAllEmployee();
return new Result(employees, Code.DELETE_OK, "删除成功");
}
return new Result(null, Code.DELETE_ERROR, "删除失败");
}
测试5:更新员工信息http://localhost/ssm/employee/updateEmployee
@RequestMapping("/updateEmployee")
public Result updateEmployee(@RequestBody Employee employee){
int count = employeeService.updateEmployee(employee);
if(count > 0){
return new Result(employeeService.selectEmployeeById(employee.getEmpId()), Code.UPDATE_OK, "更新成功"); }
return new Result(null, Code.UPDATE_ERROR, "更新失败");
}