使用IDEA工具创建maven项目,这边就不多说了,不会的请见链接:http://blog.csdn.net/sanluo11/article/details/54136847
项目目录
jar包(只做参考)
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.voicecodes</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<!--spring包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-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-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b02</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<!-- jstl标签 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jsp-api -->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b02</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
</dependencies>
<build>
<finalName>ssm</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Emp.java
package com.voicecodes.entity;
import java.util.Date;
/**
* Created by Apps09 on 2017/1/11.
*/
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
public Integer getEmpno() {
return empno;
}
public Emp setEmpno(Integer empno) {
this.empno = empno;
return this;
}
public String getEname() {
return ename;
}
public Emp setEname(String ename) {
this.ename = ename;
return this;
}
public String getJob() {
return job;
}
public Emp setJob(String job) {
this.job = job;
return this;
}
public Integer getMgr() {
return mgr;
}
public Emp setMgr(Integer mgr) {
this.mgr = mgr;
return this;
}
public Date getHiredate() {
return hiredate;
}
public Emp setHiredate(Date hiredate) {
this.hiredate = hiredate;
return this;
}
public Double getSal() {
return sal;
}
public Emp setSal(Double sal) {
this.sal = sal;
return this;
}
public Double getComm() {
return comm;
}
public Emp setComm(Double comm) {
this.comm = comm;
return this;
}
public Integer getDeptno() {
return deptno;
}
public Emp setDeptno(Integer deptno) {
this.deptno = deptno;
return this;
}
}
EmpDao.java
package com.voicecodes.dao;
import com.voicecodes.entity.Emp;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface EmpDao {
public List<Emp> findAll(); // 查询全部
public void save(Emp emp); // 添加
public Emp findById(int id); // 按id查询
public void update(Emp emp); // 更新
public void delete(int id); // 删除
}
EmpMapper.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.voicecodes.dao.EmpDao" >
<!-- 查询全部的员工 -->
<select id="findAll" resultType="com.voicecodes.entity.Emp">
select * from t_emp order by empno
</select>
<!-- 保存一条员工数据 -->
<insert id="save" parameterType="com.voicecodes.entity.Emp">
insert into t_emp values(
emp_seq.nextval,
#{ename,jdbcType=VARCHAR},
#{job,jdbcType=VARCHAR},
#{mgr,jdbcType=NUMERIC},
#{hiredate,jdbcType=DATE},
#{sal,jdbcType=NUMERIC},
#{comm,jdbcType=NUMERIC},
#{deptno,jdbcType=NUMERIC}
)
</insert>
<!-- 根据ID查询员工 -->
<select id="findById" parameterType="java.lang.Integer" resultType="com.voicecodes.entity.Emp">
select * from t_emp where empno=#{id}
</select>
<!-- 修改 -->
<update id="update" parameterType="com.voicecodes.entity.Emp">
update t_emp set
ename = #{ename,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
sal = #{sal,jdbcType=NUMERIC}
where empno=#{empno,jdbcType=NUMERIC}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="java.lang.Integer">
delete from t_emp where empno=#{id}
</delete>
</mapper>
EmpController.java
package com.voicecodes.controller;
import com.voicecodes.dao.EmpDao;
import com.voicecodes.entity.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class EmpController {
@Resource
private EmpDao empDao;
public EmpController setEmpDao(EmpDao empDao) {
this.empDao = empDao;
return this;
}
//查询员工
@RequestMapping("/find")
public String find(Model model) {
List<Emp> list = empDao.findAll();
model.addAttribute("emps", list);
return "emp_list";
}
//打开新增页面
@RequestMapping("/toAdd")
public String toAdd(){
return "add_emp";
}
//新增保存
@RequestMapping("/add")
public String add(Emp emp){
empDao.save(emp);
return "redirect:find";
}
//打开修改页面
@RequestMapping("/toUpdate")
public String toUpdate(@RequestParam("id") int id, Model model){
Emp e=empDao.findById(id);
model.addAttribute("emp", e);
return "update_emp";
}
//修改保存
@RequestMapping("/update")
public String update(Emp emp){
empDao.update(emp);
return "redirect:find";
}
//删除
@RequestMapping("/delete")
public String delete(@RequestParam("id") int id){
empDao.delete(id);
return "redirect:find";
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 读取属性文件 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>-->
<!-- 配置DataSource数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.6.25:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!--配置工厂, 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/voicecodes/entity/*.xml"></property>
</bean>
<!-- 配置mybatis -->
<bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.voicecodes.dao"></property>
</bean>
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.voicecodes"/>
<!-- 开启RequestMapping 注解 -->
<mvc:annotation-driven/>
<!-- 处理请求转发 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/emp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
emp_list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'emp_list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="../../assets/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function delete_emp(id){
var str = window.confirm("are you sure ?");
if(str){
location.href="delete?id="+id;
}
}
</script>
</head>
<body>
<div align="center">
<input type="button" value="新增" οnclick="location.href='toAdd'" >
</div>
<table width="80%" border="1" cellpadding="2" cellspacing="0" align="center">
<tr>
<th>EMPNO</th>
<th>ENAME</th>
<th>JOB</th>
<th>MGR</th>
<th>HIREDATE</th>
<th>SAL</th>
<th>COMM</th>
<th>DEPTNO</th>
</tr>
<c:forEach items="${emps}" 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>
<input type="button" value="修改" οnclick="location.href='toUpdate?id=${emp.empno}'" />
<input type="button" value="删除" οnclick="delete_emp(${emp.empno});" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
add_emp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'add_emp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="add" method="post">
<table width="40%" border="1" cellpadding="2" cellspacing="0" align="center">
<tr>
<td>姓名:</td>
<td><input type="text" name="ename"/></td>
</tr>
<tr>
<td>岗位:</td>
<td><input type="text" name="job"/></td>
</tr>
<tr>
<td>工资</td>
<td><input type="text" name="sal"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="保存" /></td><!-- colspan列合并 -->
</tr>
</table>
</form>
</body>
</html>
update_emp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'update_emp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="update" method="post">
<table width="40%" align="center" border="1" cellpadding="2" cellspacing="0" >
<tr>
<td>EMPNO:</td>
<td><input type="text" name="empno" value="${emp.empno }" > </td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="ename" value="${emp.ename }" > </td>
</tr>
<tr>
<td>岗位:</td>
<td><input type="text" name="job" value="${emp.job }" > </td>
</tr>
<tr>
<td>工资:</td>
<td><input type="text" name="sal" value="${emp.sal }" > </td>
</tr>
<tr>
<td> <input type="submit" value="保存" > </td>
</tr>
</table>
</form>
</body>
</html>
配置一下tomcat
运行结果如下