学习ssm也有段时间了,一直以来没有完整系统的过一遍项目的流程,今天就自己做一个,准备工作比较繁琐,在网上找了一些他人的流程进行参考,也结合自己的实际情况做一个整理。也为自己以后的查漏补缺。只是自己总结,不作分享之用。也是以后再次使用可以直接查看自己的流程文档,省得麻烦。
eclipse-oxypgen
apache-maven-3.5.0
apache-tomcat-7.0.70
jdk 1.8.0_131
maven
将eclipse中自带的maven版本换为自己安装的版本
修改maven的user setting配置
1、鼠标右击--new--other--maven--maven project--next-next
2、maven项目的 工程结构
https://blog.csdn.net/dm1314oooooooo/article/details/70859189 之后如果在测试index.jsp报错就将web.xml文件删除重新生成
右击项目--build path -- configure build path --
maven项目的完整目录
项目成功后的目录结构
四、测试index.jsp可以运行。我们就可以正式搭建ssm(spring、springmvc、mybatis)+maven+bootstrap+MySQL项目了。在pox.xml中配置项目所需的jar包。之后会自动下载到自己配置的maven仓库中。然后进行相关的xml文件的配置
1、先展示项目所需的相关jar包
spring-core-4.3.7.RELEASE.jar
commons-logging-1.2.jar
spring-context-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-aop-4.3.7.RELEASE.jar
spring-jdbc-4.3.7.RELEASE.jar
spring-aspects-4.3.7.RELEASE.jar
aspectjweaver-1.8.9.jar
spring-web-4.3.7.RELEASE.jar
spring-test-4.3.8.RELEASE.jar
spring-webmvc-4.3.7.RELEASE.jar
spring-tx-4.3.7.RELEASE.jar
mybatis-3.4.5.jar
mybatis-spring-1.3.1.jar
mybatis-generator-core-1.3.5.jar
pagehelper-5.1.3.jar
jsqlparser-1.0.jar
javax.servlet-api-3.0.1.jar
jackson-databind-2.8.8.jar
jackson-annotations-2.8.0.jar
ackson-core-2.8.8.jar
fastjson-1.2.47.jar
jackson-mapper-asl-1.9.13.jar
jackson-core-asl-1.9.13.jar
hibernate-validator-5.4.1.Final.jar
validation-api-1.1.0.Final.jar
jboss-logging-3.3.0.Final.jar
classmate-1.3.1.jar
mysql-connector-java-5.1.41.jar
c3p0-0.9.5.2.jar
mchange-commons-java-0.2.11.jar
jstl-1.2.jar
junit-4.12.jar
hamcrest-core-1.3.jar
2、在maven项目中的pom.xml文件中配置导入的jar(spring,springmvc,mybatis,junit,c3p0,jsr303数据校验,分页,逆向工程)可去http://mvnrepository.com/中央仓库去寻找依赖配置
pom.xml文件配置好这些后,会自动进行下载。存放到之前配置好的maven的本地仓库中
ssmcrud-demo
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
4.3.7.RELEASE
3.4.5
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
4.3.8.RELEASE
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
org.mybatis.generator
mybatis-generator-core
1.3.5
com.github.pagehelper
pagehelper
5.1.3
javax.servlet
javax.servlet-api
3.0.1
provided
com.fasterxml.jackson.core
jackson-databind
2.8.8
com.alibaba
fastjson
1.2.47
org.codehaus.jackson
jackson-mapper-asl
1.9.13
org.hibernate
hibernate-validator
5.4.1.Final
mysql
mysql-connector-java
5.1.41
com.mchange
c3p0
0.9.5.2
javax.servlet
jstl
1.2
junit
junit
4.12
test
3、配置相关的xml文件
a、在src/main/resources下添加配置文件:applicationContext.xml 配置信息:
b、配置数据库连接池:db.properties
#和数据库的链接配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_crud
jdbc.user=root
jdbc.password=root
#数据库连接池的配置
jdbc.initialPoolSize=20
#当连接池中 的连接耗尽是,c3p0一次同时获取的连接数。
jdbc.acquireIncrement=20
#定义连接池中保留的最大连接数
jdbc.maxPoolSize=30
#定义连接池中保留的最小连接数
jdbc.minPoolSize=10
#定义最大空闲,多少秒内未使用,则连接池被丢弃
jdbc.maxIdleTime=60
c、配置log4j日志,虽然不用,基本设置还是要知道的 log4j.properties
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
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
d、配置spring-db.xml
e、配置spring的事务管理:spring-tx.xml
f、配置springmvc的整合:spring-mvc.xml
g、配置和mybati的整合:spring-mybatis-dao.xml
h、mybatis中还有一些关于分页,命名规则的设置,所以还是需要单独配置:mybatis-config.xml
4、配置web.xml
对spring和springmvc进行监听,字符编码集进行设置,使用Rest风格的增删改查url 设置PUT和Delete
ssm-crud
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springDispatcherServlet
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceRequestEncoding
true
forceResponseEncoding
true
CharacterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
HttpPutFormContentFilter
org.springframework.web.filter.HttpPutFormContentFilter
HttpPutFormContentFilter
/*
5、设置包名 根据功能模块进行包命名设置
6、使用逆向工程 生成数据库和pojo对象之间的关联
a、数据库表结构和数据 tbl_emp 和 tbl_dept
b、设置逆向工程配置mbg.xml
c、运行逆向工程主程序,进行文件生成 在cn.qyl.ssmcrud.test包里面运行Mbg.java
package cn.qyl.ssmcrud.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Mbg {
public static void main(String[] args) throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
d、生成的效果图
e、修改mapper.dao和mapper.xml文件,添加自定义功能件 比如带有部门名字的员工信息查询
1、package cn.qyl.ssmcrud.dao
package cn.qyl.ssmcrud.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.qyl.ssmcrud.pojo.Employee;
import cn.qyl.ssmcrud.pojo.EmployeeExample;
public interface EmployeeMapper {
long countByExample(EmployeeExample example);
int deleteByExample(EmployeeExample example);
int deleteByPrimaryKey(Integer empId);
int insert(Employee record);
int insertSelective(Employee record);
List selectByExample(EmployeeExample example);
Employee selectByPrimaryKey(Integer empId);
// 带上部门信息的查询,需要自定义在mapper.xml文件中
List selectByExampleWithDpet(EmployeeExample example);
// 带上部门信息的查询,需要自定义在mapper.xml文件中
Employee selectByPrimaryKeyWithDept(Integer empId);
int updateByExampleSelective(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByExample(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record);
}
2、修改mapper.xml文件 里面大部分功能可由逆向工程来为我们生成,修改添加自定义功能模块 比如:带有部门名字的员工信息查询
emp_id,emp_name,gender,email,dept_name
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
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}
f、数据库里面的数据,手动添加太麻烦,可设置自动添加数据 在cn.qyl.ssmcrud.test做一个MbgTest.java类,帮我们实现员工数据的大量添加
package cn.qyl.ssmcrud.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.qyl.ssmcrud.dao.EmployeeMapper;
//使用spring提供的junit测试工具类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class MbgTest {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
SqlSession sqlSession;
//批量为我们生成一千个员工数据
@Test
public void testCRUD() {
for(int i=0;i<1000;i++){
String uid = UUID.randomUUID().toString().substring(0, 5)+i;
mapper.insertSelective(new Employee(null, uid, "F", uid+"@qyl.com", 1));
}
System.out.println("批量完成");
}
}
五.前期准备工作完成后,进行业务逻辑代码的编写
1、导入bootstrap和jQuery,在maven工程下的src目录下的wabapp目录下创建static文件夹放入,如下:
2、根据功能模块创建jsp页面 在views文件夹下放入jsp页面
3、项目启动会直接打开index.jsp页面。如果有心情可以在index.jsp做一个登陆页面,或者其他的,我做了个计时跳转。emmmm不是因为懒。。。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
//将当前项目的路径上下文获取。 在下面需要路径的地方直接使用 不因为路径的改变而出错
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
4、直接根据不同的功能模块在不同的jsp页面中实现不同的功能,通过controller判断请求找到service使用dao操作数据mysql
controller 和 service 如下:
下面放jsp页面 controller和service的代码
1、 员工信息展示页面 也是主页面 还需要通过selectEmp.jsp将查询到的数据进行分页的操作 所以有两步
welcome.jsp中有一些功能代码是给其他的jsp调用而存在的共有模块:例:部门信息获取 校验提示模块
a、welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ include file="deleteEmp.jsp"%>
<%-- --%>
<%-- --%>
员工信息
<%
//将当前项目的路径上下文获取。 在下面需要路径的地方直接使用 不因为路径的改变而出错
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
SSM_CRUD
#
empName
gender
email
deptName
操作
b、selectEmp.jsp 将员工数据查询出来,分页展示在welcome.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());
%>
2、insertEmp.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());
%>
3、deleteEmp.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());
%>
4、updateEmp.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());
%>
5、selectByempName.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());
String name = request.getParameter("name");
%>
模糊查询
#
empName
gender
email
deptName
操作
${employee.empId }
${employee.empName }
${employee.gender=="M"?"男":"女" }
${employee.email }
${employee.dept.deptName }
当前${pageInfo.pageNum }页总${pageInfo.pages }页总${pageInfo.total }条记录
6、error.jsp根据名字查不到员工数据,一个提示页面。然后进行计时跳转主页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
//将当前项目的路径上下文获取。 在下面需要路径的地方直接使用 不因为路径的改变而出错
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>