工程结构如图所示:
1.Spring系列配置文件
spring-core.xml
spring-mvc.xml
spring-mybstis
2.mybatis系列配置文件
mybstis-config.xml
EmployeeMapper.xml
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
and ${criterion.condition}
#{listItem}
emp_id, emp_name, gender, email, d_id
e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
delete from tbl_emp
where emp_id = #{empId,jdbcType=INTEGER}
delete from tbl_emp
insert into tbl_emp (emp_id, emp_name, gender,
email, d_id)
values (#{empId,jdbcType=INTEGER}, #{empName,jdbcType=VARCHAR},
#{gender,jdbcType=CHAR},
#{email,jdbcType=VARCHAR}, #{dId,jdbcType=INTEGER})
insert into tbl_emp
emp_id,
emp_name,
gender,
email,
d_id,
#{empId,jdbcType=INTEGER},
#{empName,jdbcType=VARCHAR},
#{gender,jdbcType=CHAR},
#{email,jdbcType=VARCHAR},
#{dId,jdbcType=INTEGER},
update tbl_emp
emp_id = #{record.empId,jdbcType=INTEGER},
emp_name = #{record.empName,jdbcType=VARCHAR},
gender = #{record.gender,jdbcType=CHAR},
email = #{record.email,jdbcType=VARCHAR},
d_id = #{record.dId,jdbcType=INTEGER},
update tbl_emp
set emp_id = #{record.empId,jdbcType=INTEGER},
emp_name = #{record.empName,jdbcType=VARCHAR},
gender = #{record.gender,jdbcType=CHAR},
email = #{record.email,jdbcType=VARCHAR},
d_id = #{record.dId,jdbcType=INTEGER}
update tbl_emp
emp_name = #{empName,jdbcType=VARCHAR},
gender = #{gender,jdbcType=CHAR},
email = #{email,jdbcType=VARCHAR},
d_id = #{dId,jdbcType=INTEGER},
where emp_id = #{empId,jdbcType=INTEGER}
update tbl_emp
set emp_name = #{empName,jdbcType=VARCHAR},
gender = #{gender,jdbcType=CHAR},
email = #{email,jdbcType=VARCHAR},
d_id = #{dId,jdbcType=INTEGER}
where emp_id = #{empId,jdbcType=INTEGER}
DepartmentMapper.xml
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
dept_id, dept_name
delete from tbl_dept
where dept_id = #{deptId,jdbcType=INTEGER}
delete from tbl_dept
insert into tbl_dept (dept_id, dept_name)
values (#{deptId,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})
insert into tbl_dept
dept_id,
dept_name,
#{deptId,jdbcType=INTEGER},
#{deptName,jdbcType=VARCHAR},
update tbl_dept
dept_id = #{record.deptId,jdbcType=INTEGER},
dept_name = #{record.deptName,jdbcType=VARCHAR},
update tbl_dept
set dept_id = #{record.deptId,jdbcType=INTEGER},
dept_name = #{record.deptName,jdbcType=VARCHAR}
update tbl_dept
dept_name = #{deptName,jdbcType=VARCHAR},
where dept_id = #{deptId,jdbcType=INTEGER}
update tbl_dept
set dept_name = #{deptName,jdbcType=VARCHAR}
where dept_id = #{deptId,jdbcType=INTEGER}
3.数据源信息
db.properties
jdbc.url=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
4.日志
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
5. web.xml
index.jsp
contextConfigLocation
classpath:spring/spring-core.xml
charsetEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
org.springframework.web.util.IntrospectorCleanupListener
org.springframework.web.context.ContextLoaderListener
mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/spring/spring-mvc.xml
1
mvc
/
6. 通用返回类
package com.hsun.bean;
import java.util.HashMap;
import java.util.Map;
/**
* 通用的返回类
* @author 孙浩
*
*/
public class Msg {
//状态码100-成功 200-失败
private int code;
//提示信息
private String msg;
//要返回给浏览器的数据
private Map extend = new HashMap<>();
public static Msg success() {
Msg result = new Msg();
result.setCode(100);
result.setMsg("处理成功");
return result;
}
public static Msg fail() {
Msg result = new Msg();
result.setCode(200);
result.setMsg("处理失败");
return result;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map getExtend() {
return extend;
}
public void setExtend(Map extend) {
this.extend = extend;
}
public Msg add(String key, Object value) {
this.getExtend().put(key, value);
return this;
}
}
7.首页展示
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
员工列表
<%
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
SSM_CRUD
#
empName
gender
email
deptName
操作
8. pom.xml
4.0.0
com.hsun
spring
0.0.1-SNAPSHOT
ssm-demo
war
log4j
log4j
1.2.14
com.github.pagehelper
pagehelper
5.0.0
org.mybatis.generator
mybatis-generator-core
1.3.5
org.springframework
spring-webmvc
4.3.7.RELEASE
org.springframework
spring-test
4.3.7.RELEASE
test
org.springframework
spring-jdbc
4.3.7.RELEASE
org.springframework
spring-aspects
4.3.7.RELEASE
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
c3p0
c3p0
0.9.1.2
mysql
mysql-connector-java
5.1.42
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
3.0.1
provided
junit
junit
4.12
test
com.fasterxml.jackson.core
jackson-databind
2.8.8
org.hibernate
hibernate-validator
5.4.1.Final
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.5
源码查看