SpringMVC简单案例(EMP,DEPT)
一,布局演示
二,代码演示
2.1 EmpController类代码
package com.qf.controller;
import com.qf.entity.Dept;
import com.qf.entity.Emp;
import com.qf.service.IDeptService;
import com.qf.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping(value = "/empController")
public class EmpController {
@Autowired
private IEmpService empService; // 去Spring容器找去找的
@Autowired
private IDeptService deptService;
@RequestMapping(value = "/getEmpList")
public String getEmpList(ModelMap modelMap){
// 1.查询所有的员工
List<Emp> empList = empService.getEmpList();
// 2.把数据放入到ModelMap中
modelMap.put("empList",empList);
// 3.跳转到显示页面
return "empList";
}
@RequestMapping(value = "/addEmp")
public String addEmp(Emp emp){
empService.addEmp(emp);
return "redirect:getEmpList"; // 这里是有问题的?这里要换成一个重定向
}
@RequestMapping(value = "/getEmpByEmpno")
public String getEmpByEmpno(Integer empno,ModelMap map){
Emp emp = empService.getEmpByEmpno(empno);
map.put("emp",emp);
return "updateEmp";
}
@RequestMapping(value = "/updateEmp")
public String updateEmp(Emp emp){
empService.updateEmp(emp);
return "redirect:getEmpList"; // 还是重定向
}
@RequestMapping(value = "/deleteEmpByEmpno")
public String deleteEmpByEmpno(Integer empno){
empService.deleteEmp(empno);
return "redirect:getEmpList"; // 还是重定向
}
@RequestMapping(value = "/toAddEmp")
public String toAddEmp(ModelMap modelMap){
// 1.查询所有的部门
List<Dept> deptList = deptService.getDeptList();
// 2.把部门放到一个ModelMap中
modelMap.put("deptList",deptList);
// 3.跳转到添加页面
return "addEmp";
}
}
2.2 dao层的类
2.2.1 IDeptDao类代码
package com.qf.dao;
import com.qf.entity.Dept;
import java.util.List;
public interface IDeptDao {
public List<Dept> getDeptList();
}
2.2.2 IEmpDao类代码
package com.qf.dao;
import com.qf.entity.Emp;
import org.omg.CORBA.INTERNAL;
import java.util.List;
public interface IEmpDao {
public Emp getEmpByEmpno(Integer empno);
public List<Emp> getEmpList();
public int addEmp(Emp emp);
public int updateEmp(Emp emp);
public int deleteEmp(Integer empno);
}
2.3 entity包下的类
2.3.1 Dept类的代码
package com.qf.entity;
import lombok.Data;
@Data
public class Dept {
private Integer deptno;
private String dname;
}
2.3.2 Emp类的代码
package com.qf.entity;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
public class Emp {
private Integer empno;
private String ename;
private String job;
private Double sal;
private Double comm;
private Integer mgr;
private Integer deptno;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date hiredate;
}
2.4 service层下的类
2.4.1 impl包下的类
2.4.1.1 DeptServiceImpl类的代码
package com.qf.service.impl;
import com.qf.dao.IDeptDao;
import com.qf.entity.Dept;
import com.qf.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements IDeptService {
@Autowired
private IDeptDao deptDao;
@Override
public List<Dept> getDeptList() {
return deptDao.getDeptList();
}
}
2.4.1.2 EmpServiceImpl类的代码
package com.qf.service.impl;
import com.qf.dao.IEmpDao;
import com.qf.entity.Emp;
import com.qf.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private IEmpDao empDao;
@Override
public Emp getEmpByEmpno(Integer empno) {
return empDao.getEmpByEmpno(empno);
}
@Override
public List<Emp> getEmpList() {
return empDao.getEmpList();
}
@Override
public int addEmp(Emp emp) {
return empDao.addEmp(emp);
}
@Override
public int updateEmp(Emp emp) {
return empDao.updateEmp(emp);
}
@Override
public int deleteEmp(Integer empno) {
return empDao.deleteEmp(empno);
}
}
2.4.2 IDeptService接口的代码
package com.qf.service;
import com.qf.entity.Dept;
import java.util.List;
public interface IDeptService {
public List<Dept> getDeptList();
}
2.4.3 IEmpService接口的代码
package com.qf.service;
import com.qf.entity.Emp;
import java.util.List;
public interface IEmpService {
public Emp getEmpByEmpno(Integer empno);
public List<Emp> getEmpList();
public int addEmp(Emp emp);
public int updateEmp(Emp emp);
public int deleteEmp(Integer empno);
}
2.5 resources包下mapper中的类
2.5.1 IDeptDao代码
<?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.qf.dao.IDeptDao">
<select id="getDeptList" resultType="dept">
select * from dept
</select>
</mapper>
2.5.2 IEmpDao代码
<?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.qf.dao.IEmpDao">
<select id="getEmpByEmpno" resultType="emp">
select * from emp where empno = #{empno}
</select>
<select id="getEmpList" resultType="emp">
select * from emp
</select>
<insert id="addEmp">
insert into emp
(
ename,
job,
sal,
comm,
mgr,
deptno,
hiredate
)
VALUES
(
#{ename},
#{job},
#{sal},
#{comm},
#{mgr},
#{deptno},
#{hiredate}
)
</insert>
<update id="updateEmp">
update emp
<set>
ename = #{ename},
job = #{job},
sal = #{sal},
comm = #{comm},
mgr = #{mgr},
deptno = #{deptno},
hiredate = #{hiredate}
</set>
<where>
empno = #{empno}
</where>
</update>
<delete id="deleteEmp">
delete from emp where empno = #{empno}
</delete>
</mapper>
2.6 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: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
">
<!-- 0.开启包扫描-->
<context:component-scan base-package="com.qf.service"/>
<!-- 1.加载jdbc.properties属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.创建数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="20"/>
<property name="minIdle" value="10"/>
<property name="maxActive" value="20"/>
<property name="maxWait" value="60000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
</bean>
<!-- 3.MyBatis和Spring整合 创建SQLSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.qf.entity"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 4.事务管理器-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5.开启注解事务驱动-->
<tx:annotation-driven transaction-manager="tx"/>
<!-- 6.创建Dao层代理(给com.qf.dao包下面的所有接口创建一个代理,并且天极爱到spring容器里面) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
2.7 jdbc.properties配置代码
jdbc.url=jdbc:mysql://localhost:3306/2001
jdbc.username=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
2.8 log4j.properties配置代码
# Global logging configuration
log4j.rootLogger=debug, stdout
# MyBatis logging configuration...
#log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
## 日志输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
## 设置日志的布局(流布局)
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
## 日志的格式化参数
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2.9 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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 1.开启包扫描 -->
<context:component-scan base-package="com.qf.controller"/>
<!-- 2.开启注解驱动 -->
<mvc:annotation-driven/>
<!-- 3.配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.10 web.xml配置代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 解决表单乱码的问题-->
<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>
<!-- 在Tomcat启动的时候初始化Spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC前端控制器-->
<servlet>
<servlet-name>springmvc</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>
<!-- Tomcat启动的时候调用这个servlet(SpringMVC容器也就初始化了)-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三,前端页面代码
3.1 addEmp.jsp页面代码
<%--
Created by IntelliJ IDEA.
User: dashixin
Date: 2020/5/12
Time: 15: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</title>
<base href="<%=request.getContextPath()+"/"%>">
</head>
<body>
<form method="post" action="empController/addEmp">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="ename" value="qfAdmin"></td>
</tr>
<tr>
<td>工资</td>
<td><input type="text" name="sal" value="10000.0"></td>
</tr>
<tr>
<td>奖金</td>
<td><input type="text" name="comm" value="500.0"></td>
</tr>
<tr>
<td>职位</td>
<td><input type="text" name="job" value="Java讲师"></td>
</tr>
<!-- 时间的问题先暂时放一放-->
<tr>
<td>入职时间</td>
<td><input type="date" name="hiredate"></td>
</tr>
<tr>
<td>部门</td>
<td>
<select name="deptno">
<option value="0">==请选择==</option>
<c:forEach items="${deptList}" var="dept">
<option value="${dept.deptno}">${dept.dname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>领导</td>
<td><input type="text" name="mgr" value="7369"></td>
</tr>
<tr>
<td>
<input type="submit" value="添加">
</td>
</tr>
</table>
</form>
</body>
</html>
3.2 empList.jsp页面代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
<!-- 全局设置,当前页面中所有的请求前面都会添加base的内容-->
<base href="<%=request.getContextPath()+"/"%>">
</head>
<body>
<a href="empController/toAddEmp">添加员工</a>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
<th>工资</th>
<th>奖金</th>
<th>部门</th>
<th>入职时间</th>
<th>领导</th>
<th>操作</th>
</tr>
<c:forEach items="${empList}" var="emp">
<tr>
<td>${emp.empno}</td>
<td>${emp.ename}</td>
<td>${emp.job}</td>
<td>${emp.sal}</td>
<td>${emp.comm == null?0.0:emp.comm}</td>
<td>${emp.deptno}</td>
<td>
<fmt:formatDate value="${emp.hiredate}" pattern="yyyy-MM-dd"/>
</td>
<td>${emp.mgr == null?"老板":emp.mgr}</td>
<td>
<a href="empController/getEmpByEmpno?empno=${emp.empno}">编辑</a>
<a href="empController/deleteEmpByEmpno?empno=${emp.empno}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
3.3 updateEmp.jsp页面代码
<%--
Created by IntelliJ IDEA.
User: dashixin
Date: 2020/5/12
Time: 15:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<base href="<%=request.getContextPath()+"/"%>">
</head>
<body>
<form method="post" action="empController/updateEmp">
<table>
<tr>
<td>姓名</td>
<td>
<input type="hidden" name="empno" value="${emp.empno}">
<input type="text" name="ename" value="${emp.ename}">
</td>
</tr>
<tr>
<td>工资</td>
<td><input type="text" name="sal" value="${emp.sal}"></td>
</tr>
<tr>
<td>奖金</td>
<td><input type="text" name="comm" value="${emp.comm}"></td>
</tr>
<tr>
<td>职位</td>
<td><input type="text" name="job" value="${emp.job}"></td>
</tr>
<tr>
<td>部门</td>
<td><input type="text" name="deptno" value="${emp.deptno}"></td>
</tr>
<tr>
<td>领导</td>
<td><input type="text" name="mgr" value="${emp.mgr}"></td>
</tr>
<tr>
<td>
<input type="submit" value="添加">
</td>
</tr>
</table>
</form>
</body>
</html>
3.4 index.jsp页面代码
<%--
Created by IntelliJ IDEA.
User: dashixin
Date: 2020/5/12
Time: 10:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="empController/getEmpList">查询所有的员工</a>
</body>
</html>
四,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.qf.2001</groupId>
<artifactId>day10-07-springmvc-emp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>day10-07-springmvc-emp 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>
<!-- 标签的名字可以自定义-->
<spring-version>4.3.6.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Web相关的依赖 开始-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Web相关的依赖 结束-->
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- MyBatis整合Spring的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!-- spring依赖 开始 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- Spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- SpringAOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- Spring整合Web,Tomcat启动的初始化Spring容器(监听器就在这个依赖里面-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- spring依赖 结束 -->
<!-- SpringMVC依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- Spring整合Junit依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.5</version>
</dependency>
<!--解决Spring使用slf4j输出日志与log4j冲突的问题-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.13</version>
</dependency>
</dependencies>
<build>
<finalName>day10-07-springmvc-emp</finalName>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<contextReloadable>true</contextReloadable>
<port>8080</port>
<path>/EmpManager</path>
</configuration>
</plugin>
</plugins>
</build>