首先看一下项目结构
第一步先导入jar包
pom.xml
<properties> <spring.version>4.3.18.RELEASE</spring.version> </properties> <dependencies> <!--spring的核心的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</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-tx</artifactId> <version>${spring.version}</version> </dependency> <!--spring mvc的jar包--> <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> <!--spring jdbc的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--mybatis的jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--spring 和mybatis整合的中间件jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!--orace的驱动包--> <dependency> <groupId>com.oracle</groupId> <artifactId>oracle6</artifactId> <version>6</version> </dependency> <!--spring 实现文件上传的jar包--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> </dependency> <!--解析json数据的jar包--> <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-annotations</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <!--数据库连接池的jar包--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--jstl的jar包--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--j2ee的jar包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <resources> <!--指定src main java下所有的目录下的xml文件都编译--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
导入jar包之后下载到本地仓库,这里Oracle的jar包是下载不了的,需要自己下载引入到本地仓库才可以使用
直接window+r回车输入mvn install:install-file -Dfile=jar包的位置+jar包名 -DgroupId=上面的groupId(自定义) -DartifactId=上面的artifactId(自定义) -Dversion=版本号 -Dpackaging=jar
例mvn install:install-file -Dfile=D:\jar包\ojdbc6\ojdbc6.jar -DgroupId=com.oracle -DartifactId=oracle6 -Dversion=6 -Dpackaging=jar
第二步:创建dao层
EmpDao.java
package com.aaa.ssm.dao; import com.aaa.ssm.entity.Emp; import java.util.*; public interface EmpDao { /** * 查询所有员工信息 * @return */ public List<Emp> findAllEmp(); /** * 根据ID删除员工信息 * @param empno * @return */ public int deleteEmp(int empno); /** * 根据ID查询要修改的员工信息 * @param empno * @return */ public List<Emp> toUpdate(int empno); /** * 添加员工信息 * @param emp * @return */ public int insertEmp(Emp emp); /** * 修改员工信息 * @param emp * @return */ public int updateEmp(Emp emp); }
EmpDaoMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.aaa.ssm.dao.EmpDao"> <select id="findAllEmp" resultType="com.aaa.ssm.entity.Emp"> select * from emp </select> <delete id="deleteEmp" parameterType="int"> delete from emp where empno=#{empno} </delete> <select id="toUpdate" resultType="com.aaa.ssm.entity.Emp"> select * from emp where empno=#{empno} </select> <insert id="insertEmp"> <selectKey resultType="int" keyProperty="empno" order="BEFORE"> select seq_emp.nextval from dual </selectKey> insert into emp(empno,ename,job,mgr,sal,comm,deptno) VALUES(#{empno},#{ename},#{job},#{mgr},#{sal},#{comm},#{deptno}) </insert> <update id="updateEmp"> update emp set ename=#{ename},job=#{job},mgr=#{mgr},sal=#{sal},comm=#{comm},deptno=#{deptno} where empno=#{empno} </update> </mapper>
第三步:创建service层
EmpService.java接口
package com.aaa.ssm.service; import com.aaa.ssm.entity.Emp; import java.util.*; public interface EmpService { /** * 查询所有员工信息 * @return */ public List<Emp> findAllEmp(); /** * 根据ID删除员工信息 * @param empno * @return */ public int deleteEmp(int empno); /** * 根据ID查询要修改的员工信息 * @param empno * @return */ public List<Emp> toUpdate(int empno); /** * 添加员工信息 * @param emp * @return */ public int insertEmp(Emp emp); /** * 修改员工信息 * @param emp * @return */ public int updateEmp(Emp emp); }
EmpServiceImpl.java实现类
package com.aaa.ssm.service.impl; import com.aaa.ssm.dao.EmpDao; import com.aaa.ssm.entity.Emp; import com.aaa.ssm.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class EmpDaoImpl implements EmpService { @Autowired private EmpDao dao; public List<Emp> findAllEmp() { return dao.findAllEmp(); } public int deleteEmp(int empno) { return dao.deleteEmp(empno); } public List<Emp> toUpdate(int empno) { return dao.toUpdate(empno); } public int insertEmp(Emp emp) { return dao.insertEmp(emp); } public int updateEmp(Emp emp) { return dao.updateEmp(emp); } }
第四步:创建实体
Emp.java
package com.aaa.ssm.entity; public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private String hiredate; private Double sal; private Integer comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } }
第五步:创建controller层
EmpController.java
package com.aaa.ssm.controller; import com.aaa.ssm.entity.Emp; import com.aaa.ssm.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/emp") public class EmpController { @Autowired private EmpService ser; @RequestMapping("/findallemp") public String findAllEmp(Model model){ List<Emp> list = ser.findAllEmp(); model.addAttribute("list",list); return "allemp"; } @RequestMapping("/deleteemp") public String deleteEmp(int empno){ ser.deleteEmp(empno); return "redirect:/emp/findallemp.do"; } @RequestMapping("/toupdate") public String toUpdate(Integer empno,Model model){ if(empno!=null){ List<Emp> list = ser.toUpdate(empno); model.addAttribute("emp",list.get(0)); } return "ioru"; } @RequestMapping("/insertorupdateemp") public String InsertOrUpdateEmp(Emp emp){ if (emp.getEmpno()!=null){ ser.updateEmp(emp); }else{ ser.insertEmp(emp); } return "redirect:/emp/findallemp.do"; } }
第六步:创建配置文件
spring.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"> <!--第一步,扫描service --> <context:component-scan base-package="com.aaa.ssm.service.impl"></context:component-scan> <!--第二步,加载jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--第四步,创建mybatis的工厂类对象--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定数据源--> <property name="dataSource" ref="dataSource"></property> <!--加载mybatis的映射文件 在value中可以使用*号通配符--> <property name="mapperLocations" value="classpath:com/aaa/ssm/dao/*.xml"></property> <!--加载mybatis中的配置文件--> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> <!--第五步,在spring 的工厂中生成dao接口的实现类对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要扫描哪个包下面所有的dao接口--> <property name="basePackage" value="com.aaa.ssm.dao"></property> </bean> <!--第六步,创建spring的事物管理器--> <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> </beans>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--第一步,扫描控制器包--> <context:component-scan base-package="com.aaa.ssm.controller"></context:component-scan> <!--第二步,声明以注解的方式使用spring mvc--> <mvc:annotation-driven></mvc:annotation-driven> <!--第三步,配置spring mvc 内置的视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--打印日志可以看执行的sql语句--> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> </configuration>
jdbc.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
password=tiger
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_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <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> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
到这里后台代码就写完了,接下来是前台的界面
WEB-INF目录下新建一个jsp文件夹
这里如果新建的maven项目没有web目录那么这样添加
然后在jsp文件夹下创建jsp文件
allemp.jsp
<%-- Created by IntelliJ IDEA. User: ASUS Date: 2019/6/3 Time: 16:02 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" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>员工信息</title> </head> <body> <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="${list}" var="m"> <tr> <td>${m.empno}</td> <td>${m.ename}</td> <td>${m.job}</td> <td>${m.mgr}</td> <td>${m.hiredate}</td> <td>${m.sal}</td> <td>${m.comm}</td> <td>${m.deptno}</td> <td> <a href="<%=path%>/emp/deleteemp.do?empno=${m.empno}">删除</a> <a href="<%=path%>/emp/toupdate.do?empno=${m.empno}">修改</a> </td> </tr> </c:forEach> <a href="<%=path%>/emp/toupdate.do">添加</a> </body> </html>
ioru.xml
<%-- Created by IntelliJ IDEA. User: ASUS Date: 2019/6/3 Time: 16:57 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> <form method="post" action="/emp/insertorupdateemp.do"> <input type="hidden" value="${emp.empno}" name="empno" > 员工姓名:<input type="text" name="ename" value="${emp.ename}" ><br/> 员工职位:<input type="text" name="job" value="${emp.job}" ><br/> 员工上级领导:<input type="text" name="mgr" value="${emp.mgr}" ><br/> 员工基本工资:<input type="text" name="sal" value="${emp.sal}" ><br/> 员工奖金:<input type="text" name="comm" value="${emp.comm}" ><br/> 部门编号:<input type="text" name="deptno" value="${emp.deptno}" ><br/> <input type="submit" value="保存"> </form> </body> </html>
到这来就结束了。测试一下查询