SSM整合之CRUD增删改查案例(非ajax版)

最终页面效果:

在这里插入图片描述

最终项目的目录结构

在这里插入图片描述

在这里插入图片描述

数据库表

在这里插入图片描述

表的设计:

在这里插入图片描述

pom.xml

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

<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">
    <parent>
        <artifactId>ssmProject</artifactId>
        <groupId>com.lbl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ssm04</artifactId>
    <packaging>war</packaging>

    <name>ssm04 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.9.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>

    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>


</project>

applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.lbl">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 四大信息-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://47.115.79.211:3308/crud?useUnicode=true&amp;characterEncoding=utf8"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="admin123"></property>
    </bean>
    <!-- session工厂-->
    <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- com.wzx.domain.Person  person-->
        <property name="typeAliasesPackage" value="com.lbl.domain"/>
    </bean>
    <!-- IPersonDao.xml  IPersonDao.java-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lbl.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--配置Spring框架声明式事务管理-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!--配置AOP增强-->
    <aop:config>
        <aop:pointcut id="service" expression="execution(* com.lbl.service.impl.*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
    </aop:config>

</beans>

log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=F:/ssmTest/log4j/ssmTest.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

springmvc.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--只扫@Controller注解-->
    <context:component-scan base-package="com.lbl">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--设置配置文件的路径  service dao-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--解决中文乱码的过滤器-->
  <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>

  <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置前端控制器 controller-->
  <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.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

部门

TestDepartmentService

/**
 * Created by 李柏霖
 * 2020/10/16 19:37
 */

package com.lbl.service;

import com.lbl.domain.Department;
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 java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartmentService {

    @Autowired
    IDepartmentService departmentService;

    @Test
    public void findAll(){
        List<Department> result = departmentService.findAll();
        System.out.println(result);
    }

    @Test
    public void save(){
        Department department = new Department("研发");
        departmentService.save(department);
    }

    @Test
    public void savePersons(){
        ArrayList<Department> list = new ArrayList<>();
        list.add(new Department("测试1"));
        list.add(new Department("测试2"));
        list.add(new Department("测试3"));
        departmentService.saveDepartments(list);
    }

    @Test
    public void deleteById(){
        departmentService.deleteById(6);
    }

    @Test
    public void findById(){
        Department department = departmentService.findById(5);
        System.out.println(department);
    }
    @Test
    public void update(){
        Department department = departmentService.findById(5);
        department.setDname("研发");
        departmentService.update(department);
    }



}

IDepartmentDaoTest

package com.lbl.dao;

import com.lbl.domain.Department;
import junit.framework.TestCase;
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 java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IDepartmentDaoTest extends TestCase {

    @Autowired
    IDepartmentDao departmentDao;

    @Test
    public void testFindAll() {
        List<Department> result = departmentDao.findAll();
        System.out.println(result);
    }
    @Test
    public void testFindById() {
        Department result = departmentDao.findById(5);
        System.out.println(result);
    }

    @Test
    public void testSave() {
        Department department = new Department("测试1");
        departmentDao.save(department);
    }

    @Test
    public void testDelete(){
        departmentDao.deleteById(7);
    }

    @Test
    public void testUpdate(){
        Department department = departmentDao.findById(5);
        department.setDname("研发");
        departmentDao.update(department);
    }


}

Department

/**
 * Created by 李柏霖
 * 2020/10/18 20:33
 */

package com.lbl.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer did;
    private String dname;

    public Department(String dname) {
        this.dname = dname;
    }
}

IDepartmentService

/**
 * Created by 李柏霖
 * 2020/10/18 20:34
 */

package com.lbl.service;

import com.lbl.domain.Department;

import java.util.List;

public interface IDepartmentService {
    List<Department> findAll();

    Department findById(Integer did);

    void save(Department department);

    void saveDepartments(List<Department> departments);

    void deleteById(Integer did);

    void update(Department department);


}

DepartmentServiceImpl

/**
 * Created by 李柏霖
 * 2020/10/18 20:35
 */

package com.lbl.service.Impl;

import com.lbl.dao.IDepartmentDao;
import com.lbl.domain.Department;
import com.lbl.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {



    @Autowired
    IDepartmentDao departmentDao;

    @Override
    public List<Department> findAll() {
        return departmentDao.findAll();
    }

    @Override
    public void save(Department department) {
        departmentDao.save(department);
    }



    @Override
    public void saveDepartments(List<Department> departments) {
        for (int i = 0; i <departments.size() ; i++) {
            departmentDao.save(departments.get(i));
        }

    }

    @Override
    public void deleteById(Integer did) {
        departmentDao.deleteById(did);
    }

    @Override
    public Department findById(Integer did) {
        return departmentDao.findById(did);
    }

    @Override
    public void update(Department department) {
        departmentDao.update(department);
    }
}

IDepartmentDao

/**
 * Created by 李柏霖
 * 2020/10/18 20:33
 */

package com.lbl.dao;

import com.lbl.domain.Department;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IDepartmentDao {
    List<Department> findAll();

    Department findById(Integer did);

    void save(Department department);

    void deleteById(Integer did);

    void update(Department department);
}

IDepartment.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.lbl.dao.IDepartmentDao">


    <select id="findAll" resultType="department">
        select * from department;
    </select>

    <select id="findById" parameterType="int" resultType="department">
        select * from department where did=#{did};
    </select>

    <insert id="save" parameterType="department">
        insert into department values(null,#{dname})
    </insert>

    <delete id="deleteById" parameterType="int">
        delete from department where did = #{did}
    </delete>

    <update id="update" parameterType="department">
        update department set dname=#{dname} WHERE did=#{did}
    </update>

</mapper>

员工

TestEmployeeService

/**
 * Created by 李柏霖
 * 2020/10/16 19:37
 */

package com.lbl.service;

import com.lbl.domain.Employee;
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 java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestEmployeeService {

    @Autowired
    IEmployeeService employeeService;

    @Test
    public void findAll(){
        List<Employee> list = employeeService.findAll();
        System.out.println(list);
    }

    @Test
    public void save(){
        Employee employee = new Employee("李柏霖save",1,1);
        employeeService.save(employee);
    }

    @Test
    public void testSavePersons() {
        ArrayList<Employee> list = new ArrayList<>();
        Employee employee1 = new Employee("李柏霖1",1,1);
        Employee employee2 = new Employee("李柏霖2",1,1);
        Employee employee3 = new Employee("李柏霖3",1,1);
        list.add(employee1);
        list.add(employee2);
        list.add(employee3);
        employeeService.savePersons(list);
    }

    @Test
    public void testDeleteById(){
        employeeService.deleteById(6);
    }

}

IEmployeeDaoTest

package com.lbl.dao;

import com.lbl.domain.Employee;
import junit.framework.TestCase;
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 java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeDaoTest extends TestCase {

    @Autowired
    IEmployeeDao employeeDao;

    @Test
    public void testFindAll() {
        List<Employee> list = employeeDao.findAll();
        System.out.println(list);
    }

    @Test
    public void testFindById() {
        Employee employee = employeeDao.findById(1);
        System.out.println(employee);
    }

    @Test
    public void testSave() {
        Employee employee = new Employee("李柏霖",1,1);
        employeeDao.save(employee);
    }

    @Test
    public void deleteById(){
        employeeDao.deleteById(7);
    }

    @Test
    public void update(){
        Employee employee = employeeDao.findById(5);
        employee.setEname("苏沙");
        employee.setGander(0);
        employeeDao.update(employee);
    }
    @Test
    public void update2(){
        Employee employee = new Employee();
        employee.setEid(5);
        employee.setEname("苏沙");
        employee.setGander(0);
        employeeDao.update(employee);
    }

}

IEmployeeDao

/**
 * Created by 李柏霖
 * 2020/10/18 20:33
 */

package com.lbl.dao;

import com.lbl.domain.Employee;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IEmployeeDao {
    List<Employee> findAll();

    Employee findById(Integer eid);

    void save(Employee employee);

    void deleteById(Integer eid);

    void update(Employee employee);
}

IEmployeeService

/**
 * Created by 李柏霖
 * 2020/10/18 20:34
 */

package com.lbl.service;

import com.lbl.domain.Employee;

import java.util.List;

public interface IEmployeeService {
    List<Employee> findAll();

    Employee findById(Integer eid);

    void save(Employee employee);

    void savePersons(List<Employee> employees);

    void deleteById(Integer eid);

    void update(Employee employee);
}

EmployeeServiceImpl

/**
 * Created by 李柏霖
 * 2020/10/18 20:35
 */

package com.lbl.service.Impl;

import com.lbl.dao.IEmployeeDao;
import com.lbl.domain.Employee;
import com.lbl.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    IEmployeeDao employeeDao;

    @Override
    public List<Employee> findAll() {
        return employeeDao.findAll();
    }

    @Override
    public Employee findById(Integer eid) {
        return employeeDao.findById(eid);
    }

    @Override
    public void save(Employee employee) {
        employeeDao.save(employee);
    }



    @Override
    public void savePersons(List<Employee> employees) {
        for (int i = 0; i <employees.size() ; i++) {
            employeeDao.save(employees.get(i));
        }
    }

    @Override
    public void deleteById(Integer eid) {
        employeeDao.deleteById(eid);
    }

    @Override
    public void update(Employee employee) {
        employeeDao.update(employee);
    }
}

IEmployeeDao.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.lbl.dao.IEmployeeDao">


    <select id="findAll" resultType="employee">
            select * from employee;
    </select>

    <select id="findById" parameterType="int" resultType="employee">
            select * from employee where eid=#{eid};
    </select>

    <insert id="save" parameterType="employee">
        insert into employee values(null,#{ename},#{gander},#{did})
    </insert>

    <delete id="deleteById" parameterType="int">
        delete from employee where eid=#{cid}
    </delete>

    <update id="update" parameterType="employee">
        update employee
        <set>
            <if test="ename!=null">ename=#{ename},</if>
            <if test="gander!=null">gander=#{gander},</if>
            <if test="did!=null">did=#{did},</if>
        </set>
        where eid=#{eid}
    </update>
</mapper>

页面

list.jsp

<%--
  Created by IntelliJ IDEA.
  User: Carlos
  Date: 2020/10/18
  Time: 20:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table border="1px" width="100%">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>部门ID</th>
        <th>操作1</th>
        <th>操作2</th>

    </tr>
    <c:forEach items="${employeeList}" var="employee">
        <tr>
            <td>${employee.eid}</td>
            <td>${employee.ename}</td>
            <td>${employee.gander}</td>
            <td>${employee.did}</td>
            <td><a href="${pageContext.request.contextPath}/employee/delete?eid=${employee.eid}">删除</a></td>
            <td><a href="${pageContext.request.contextPath}/employee/updateUI?eid=${employee.eid}">修改</a></td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

##updateUI.jsp

<%--
  Created by IntelliJ IDEA.
  User: Carlos
  Date: 2020/10/18
  Time: 21:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>updateUI</title>
</head>
<body>
${employee}
<form method="post" action="${pageContext.request.contextPath}/employee/update">
    <input type="text" name="eid" value="${employee.eid}" hidden>
    eid:<input type="text" name="eid" value="${employee.eid}" disabled><br>
    ename:<input type="text" name="ename" value="${employee.ename}"><br>
    gander:<input type="text" name="gander" value="${employee.gander}"><br>
    did:<input type="text" name="did" value="${employee.did}"><br>
    <input type="submit" value="保存">
</form>
</body>
</html>

EmployeeController

/**
 * Created by 李柏霖
 * 2020/10/18 20:49
 */

package com.lbl.Controller;

import com.lbl.domain.Department;
import com.lbl.domain.Employee;
import com.lbl.service.IDepartmentService;
import com.lbl.service.IEmployeeService;
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.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping(path = "/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String list(Model model){
        System.out.println("employee....list.....run");
        //显示所有的person数据
        List<Employee> employeeList = employeeService.findAll();
        System.out.println(employeeList);
        model.addAttribute("employeeList",employeeList);
        return "list";
    }

    @RequestMapping(path = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    public String delete(Integer eid){
        System.out.println("employee....delete.....run");
        //显示所有的person数据
        employeeService.deleteById(eid);
        return "redirect:list";
    }


    @RequestMapping(path = "/updateUI",method = {RequestMethod.GET,RequestMethod.POST})
    public String updateUI(Integer eid,Model model){
        System.out.println("employee....updateUI.....run");
        //显示所有的person数据
        Employee employee = employeeService.findById(eid);
        model.addAttribute("employee",employee);
        return "updateUI";
    }

    @RequestMapping(path = "/update",method = {RequestMethod.GET,RequestMethod.POST})
    public String update(Employee employee){
        System.out.println("employee....update.....run");
        //显示所有的person数据
        employeeService.update(employee);
        return "redirect:list";
    }
}

©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:上身试试 返回首页