Sring MVC整合SSM框架配置文件及单表增删改查的实现

** Java开发SSM框架配置及增删改查实现(单表)**
主要步骤(流程)
一、 配置文件步骤:

    pom.xml文件(依赖),
	resources包下的文件:
			dbmysql.properties
			SqlMapConfig.xml
			spring-mvc.xml
			applicationContext.xml
			EmpMapper.xml(sql语句)
	webapp包下的文件:
            jQuery导入(版本)jquery-1.11.0.js
		   web.xml配置
           页面
			      首页index.jsp
			      展示页面list.jsp
			      修改页面edit.jsp
				  添加页面save.jsp

二、功能实现步骤:
实体类,
查询和分页属性设置(类),
mapper接口,
服务层service,
控制层controller
(最后一页附上IDEA视图区截图)

-------------------------------重要配置 (1、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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.k9501</groupId>
  <artifactId>Day10_SSM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Day10_SSM 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.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
<!--spring 4个核心-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--aop三件套-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>aopalliance</groupId>
  <artifactId>aopalliance</artifactId>
  <version>1.0</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.2</version>
</dependency>
<!--事物-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--测试-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--jdbc-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--oxm-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.6</version>
</dependency>
<!--mysql-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
</dependency>
<!--spring - mybatis-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.2</version>
</dependency>
<!--c3p0-->
<dependency>
  <groupId>com.mchange</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.5.2</version>
</dependency>
<!--MVC-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.3.21.RELEASE</version>
</dependency>
<!--servlet api-->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <!--作用域为除了运行时,其他时候都需要-->
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>javax.servlet.jsp-api</artifactId>
  <version>2.3.1</version>
  <scope>provided</scope>
</dependency>
<!--文件上传-->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.1</version>
</dependency>
<!--JSON-->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.8</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.4</version>
</dependency>
<!--jstl-->
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

   <!--pageHelper-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.9</version>
    </dependency>
  </dependencies>
</project>

-------------------------------重要配置 (2、resources配置)------------------------------------
a,db包中dbmysql.properties配置(数据库连接四要素)

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.user=root
jdbc.password=root

b,mapper包中SqlMapConfig.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>
    <typeAliases>
        <typeAlias type="com.k9501.util.Params" alias="Params"/>
        <package name="com.k9501.entity"/>
    </typeAliases>

    <!--分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 分页参数合理化,默认false禁用-->
            <!--pageNum表示当前页  pages表示尾页-->
            <!--true启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页-->
            <!--false禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据-->
            <property name="reasonable"   value="true"/>
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

c,mvc包中spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   
    <!--扫描注解-->
    <context:component-scan base-package="com.k9501.controller"/>
    <!--适配器和映射器注解-->
    <mvc:annotation-driven/>
    <!--静态资源放行-->
    <mvc:default-servlet-handler/>
    <!--试图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

d,spring包中核心配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--启动注解-->
    <context:component-scan base-package="com.k9501"/>
    <!--读取配置文件properties-->
    <context:property-placeholder location="classpath:mybatis/db/dbmysql.properties"/>
    <!--配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--mapper.xml-->
        <property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"/>
        <!--主配置文件-->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    </bean>
    <!--接口和mapper连接-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--接口扫描-->
        <property name="basePackage" value="com.k9501.mapper"/>
        <!--sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

    </bean>
</beans>

e, mapper包中EmpMapper.xml文件(sql语句)

  <?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.k9501.mapper.EmpMapper">

    <resultMap id="empResultMap" type="Emp">
        <result property="hiredate" column="hiredate" javaType="java.sql.Date"/>
    </resultMap>
   <sql id="baseSql">
       empno,ename,job,mgr,hiredate,sal,comm,deptno
   </sql>

    <select id="selectEmpAll" parameterType="Params" resultMap="empResultMap">
        select
        <include refid="baseSql"/>
        from emp
        <where>
            <if test="ename!=null and ename!=''">
                and ename like CONCAT(CONCAT('%',#{ename},'%'))
            </if>
            <if test="job!=null and job!=''">
                and job like CONCAT(CONCAT('%',#{job},'%'))
            </if>
            <if test="hiredateFrom!=null and hiredateFrom !=''">
                and hiredate &gt; #{hiredateFrom}
            </if>
            <if test="hiredateTo!=null and hiredateTo !=''">
                and hiredate &lt; #{hiredateTo}
            </if>
        </where>
    </select>
    <delete id="deleteEmpById">
        delete from emp where empno = #{empno}
    </delete>

    <select id="selectEmpById" resultMap="empResultMap">
        select
        <include refid="baseSql"/>
        from emp
        where empno = #{empno}
    </select>
    <update id="updateEmp" parameterType="Emp">
        update emp
        <set>
            <if test="ename!=null and ename!=''">
                ename = #{ename},
            </if>
            <if test="job!=null and job!=''">
                job = #{job},
            </if>
            <if test="mgr!=null and mgr!=''">
                mgr = #{mgr},
            </if>
            <if test="hiredate!=null">
                hiredate = #{hiredate},
            </if>
            <if test="sal!=null and sal!=''">
                sal = ${sal},
            </if>
            <if test="comm!=null and comm!=''">
                comm = ${comm},
            </if>
            <if test="deptno!=null and deptno!=''">
                deptno = ${deptno},
            </if>
        </set>
        where empno = #{empno}
    </update>
    <insert id="saveEmp" parameterType="emp">
        insert into emp(ename,job,mgr,hiredate,sal,comm,deptno)
        values (#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
    </insert>
</mapper>

-------------------------------重要配置 (3、webapp包下文件)----------------------------------
3a,导入jQuery文件放在js包中,文件(版本)jquery-1.11.0.js
3b, WEB-INF的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">

    <!--加载spring配置文件-->
    <!--关联到spring主配置文件的路径地址  classpath根目录-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
    <!--配置监听器加载spring的配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--异步支持-->
        <async-supported>true</async-supported>
        <!--初始化 编码格式为UTF-8 ,只适用于post提交-->
        <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>
    <!--加载springmvc配置文件-->
    <!--配置DispatcherServlet,来加载springmvc的配置-->
    <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:mvc/spring-mvc.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>
    
</web-app>

3c,页面(jsp页面都在webapp包下,但不在WEB-INF包下)
3c-1 首页index.jsp

 <%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/7
  Time: 10:28
  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.request.contextPath}/emp/selectEmpAll">查询全部员工</a>
</body>
</html>

3c-2 条件查询、展示、分页(页面list.jsp)
因有前后缀配置,这里说明下list,edit,save三个页面位置:webapp-jsp包-emp包下(首页index直接在webapp下)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/7
  Time: 11:23
  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>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery-1.11.0.js"></script>
</head>
<body>
<%--条件查询--%>
<div align="center">
    <form id="myform" action="${pageContext.request.contextPath}/emp/selectEmpAll" method="post">
        姓名:<input type="text" name="ename" value="${params.ename}" size="6"/>
        职务:<input type="text" name="job" value="${params.job}" size="6"/>
        雇用时间:<input type="date" name="hiredateFrom" value="${params.hiredateFrom}" size="6"/>
        -<input type="date" name="hiredateTo" value="${params.hiredateTo}" size="6"/>
        <input id="pn" type="hidden" name="pageNum" value="${params.pageNum}"/>
        <input id="ps" type="hidden" name="pageSize" value="${params.pageSize}"/>
        <input type="submit" value="搜索">
    </form>
</div>
<div align="center" width="100%" height="50%">
<table border="1" width="80%">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>职务</th>
        <th>级别</th>
        <th>入职日期</th>
        <th>工资</th>
        <th>奖金</th>
        <th>部门</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${pageInfo.list}" var="emp">
        <tr>
            <td>${emp.empno}</td>
            <td>${emp.ename}</td>
            <td>${emp.job}</td>
            <td>${emp.mgr}</td>
            <td>${emp.hiredate}</td>
            <td>${emp.sal}</td>
            <td>${emp.comm}</td>
            <td>${emp.deptno}</td>
            <td align="center">
            <a href="${pageContext.request.contextPath}/emp/deleteEmpById?empno=${emp.empno}">删除</a>
            <a href="${pageContext.request.contextPath}/emp/updatePageEmp/${emp.empno}">修改</a>
            </td>
        </tr>
    </c:forEach>
</table>
</div>

<%--分页--%>
<div align="center" width="100%">
<table align="center" width="85%">
    <tr>
        <td><a href="javascript:toPage(1);">首页</a></td>
        <td><a href="javascript:toPage(${pageInfo.pageNum-1});">上一页</a></td>
        <td><a href="javascript:toPage(${pageInfo.pageNum+1});">下一页</a></td>
        <td><a href="javascript:toPage(${pageInfo.pages});">尾页</a></td>
        <td>第${pageInfo.pageNum}页/共${pageInfo.pages}页</td>
        <td>设置每页条数:
            <select id="pageSize" onchange="toPage(1)">
                <option value="2"<c:if test="${pageInfo.pageSize==2}">selected</c:if> >2</option>
                <option value="3"<c:if test="${pageInfo.pageSize==3}">selected</c:if> >3</option>
                <option value="5"<c:if test="${pageInfo.pageSize==5}">selected</c:if> >5</option>
            </select>
        </td>
        <td><a href="${pageContext.request.contextPath}/emp/savePageEmp">添加一条</a></td>
    </tr>
</table>
</div>
</body>
<script type="text/javascript">
    function toPage(pageNum) {
        //给pageSize设值,获取每页显示条数
        var ps = $("#pageSize").val();
        $("#ps").val(ps);
        //给pageNum设值
        $("#pn").val(pageNum);
        //提交表单
        $("#myform").submit();
    }
</script>
</html>

3c-3, 修改编辑页面(edit.jsp)

  <%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/7
  Time: 17:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>员工修改</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/emp/updateEmp" method="post">
    <input type="hidden" name="empno" value="${emp.empno}"/>
    <p>姓名:<input type="text" name="ename" value="${emp.ename}"/></p>
    <p>职务:<input type="text" name="job" value="${emp.job}"/></p>
    <p>级别:<input type="text" name="mgr" value="${emp.mgr}"/></p>
    <p>入职日期:<input type="date" name="hiredate" value="${emp.hiredate}"/></p>
    <p>工资:<input type="text" name="sal" value="${emp.sal}"/></p>
    <p>奖金:<input type="text" name="comm" value="${emp.comm}"/></p>
    <p>部门:<input type="text" name="deptno" value="${emp.deptno}"/></p>
    <p><input type="submit" value="保存"/></p>
</form>
</body>
</html>

3c-4,添加页面(save.jsp)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/7
  Time: 17:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加员工</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/emp/saveEmp" method="post">

    <p>姓名:<input type="text" name="ename"></p>
    <p>职务:<input type="text" name="job" /></p>
    <p>级别:<input type="text" name="mgr" /></p>
    <p>入职日期:<input type="date" name="hiredate" /></p>
    <p>工资:<input type="text" name="sal" /></p>
    <p>奖金:<input type="text" name="comm" /></p>
    <p>部门:<input type="text" name="deptno" /></p>
    <p><input type="submit" value="添加"/></p>
</form>
</body>
</html>

=功能实现步骤=
(实体类、查询和分页属性设置、mapper接口,服务层,控制层)
一,数据库中添加员工表emp, idea中创建实体类:
package com.k9501.entity包下类名为Emp

public class Emp {
private Integer empno;  //员工编号
private String ename; //员工姓名
private String job;  //级别
private Integer mgr;  //
@DateTimeFormat(pattern = "yyyy-MM-dd")   //日期注解
private Date hiredate;  //入职日期
private double sal;  //工资
private double comm;  //奖金
private Integer deptno; //部门
//构造方法,getter和setter省略
}

二,为条件查询及分页属性新建类:
package com.k9501.util包下类名为Params

public class Params {
//分页条件
private int pageNum;//当前页
private int pageSize=5;//每页条数
//条件查询
private String ename;  //查询的姓名
private String job;  //查询的职务
private String hiredateFrom;  //入职时间开始值
private String hiredateTo;    //入职时间截止值
 //getter和setter省略
 }

三、mapper接口

public interface EmpMapper {
 List<Emp> selectEmpAll(Params params);  //查询所有员工
 int deleteEmpById(int empno);  //根据编号删除一条
 Emp selectEmpById(int empno);  //根据编号选择一条(修改时用)
 void updateEmp(Emp emp);  //修改功能
 void saveEmp(Emp emp); //添加功能
}

四、服务层
接口 对应mapper接口的功能:

     public interface EmpService {
        List<Emp> selectEmpAll(Params params);
        int deleteEmpById(int empno);
        Emp selectEmpById(int empno);
        void updateEmp(Emp emp);
        void saveEmp(Emp emp);
}

服务层接口的功能实现:

      @Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public List<Emp> selectEmpAll(Params params) {    //查询所有员工
        if (params.getPageNum()==0){
            params.setPageNum(1);
        }
        //每页条数如果没有初始值也需要非空判断
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        return empMapper.selectEmpAll(params);
    }
    @Override
    public int deleteEmpById(int empno) {   //根据编号删除一条
    return empMapper.deleteEmpById(empno);
    }
    @Override
    public Emp selectEmpById(int empno) {    //根据编号选择一条(修改时用)
        return empMapper.selectEmpById(empno);
    }
    @Override
    public void updateEmp(Emp emp) {   //修改功能
        empMapper.updateEmp(emp);
    }
    @Override
    public void saveEmp(Emp emp) {  //添加功能
        empMapper.saveEmp(emp);
    }
}

五、控制层

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
/**
 *查询全部
 */
    @RequestMapping("/selectEmpAll")
    public String selectEmpAll(Model model, Params params){
            // 服务方法查询全部
        List<Emp> empList=empService.selectEmpAll(params);
        //用pagehelper的对象来封装数据
        PageInfo<Emp> pageInfo=new PageInfo<>(empList);
        model.addAttribute("pageInfo",pageInfo);
        //回显数据
        model.addAttribute("params",params);
        //转发到页面
        return "emp/list";

    }

/*
* 根据id删除
* */
    @RequestMapping("/deleteEmpById")
    public String deleteEmpById(int empno){
      //调用服务层的删除方法
       empService.deleteEmpById(empno);
       //重定向到查询全部
        return "redirect:/emp/selectEmpAll";
    }

    /*
     * 根据id查询
     * */
    @RequestMapping("/updatePageEmp/{empno}")
    public String updatePageEmp(Model model, @PathVariable int empno){
        //调用服务层的单条查询方法
        Emp emp=empService.selectEmpById(empno);
        //向域中设置参数
        model.addAttribute("emp",emp);
        //转发到修改页面
        return "/emp/edit";

    }
    /*
     * 修改保存
     * */
    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp){
        //调用服务层的修改方法
        empService.updateEmp(emp);
        //重定向到查询全部
        return "redirect:/emp/selectEmpAll";


    }
    /*
     * 添加页面
     * */
    @RequestMapping("/savePageEmp")
    public String savePageEmp(){
        //转发到添加页面
        return "/emp/save";

    }
    /*
     * 保存添加的
     * */
    @RequestMapping("/saveEmp")
    public String saveEmp(Emp emp){
        empService.saveEmp(emp);
        return "redirect:/emp/selectEmpAll";

    }
}

在这里插入图片描述
至此,单表增删改查的SSM框架配置、功能实现已完毕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰少2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值