SSM整合完整流程

365bet中国 2025-09-15 12:22:51 阅读: 9380

# 1. SSM 整合说明 自从学了 SpringBoot 之后,已经很久没接触到 SSM 以及 SSM 项目相关的内容,这不最近新入职了一家公司,整合使用的是 SSM 相关技术,有很多知识点跟步骤都有些遗忘了,这里特别重复把他捡起来,特此记录一下,分享出来,大家共勉!

# 1.1 各个框架担任的角色SpringMVC:表述层框架,处理浏览器发送给服务器的请求,将一些数据响应到浏览器。MyBatis:持久层框架,连接数据库,访问数据库,操作数据库Spring:整合型框架,通过 IOC 管理对象,比如 MyBatis 中操作数据库的 SqlSession 对象,MyBatis 操作数据库时需要事务功能,也可以使用 Spring 中 Aop 的重要应用,声明式事务实现。Spring 和 SpringMVC 本身就是同源的,作为 Spring 家族的一个框架,整合不整合都行,我们所说的不整合是指 Spring 和 SpringMVC 创建同一个 IOC 容器,整合是指 Spring 和 SpringMVC 各自创建自己的 IOC 容器,管理各自的组件。(建议整合)

既然是两个 IOC 容器,他们如何关联呢?

SpringMVC 是子容器,Spring 是父容器,子容器可以访问父容器的 bean,父容器访问不了子容器。(具体在源码中体现,这里不再赘述)

# 1.2 IOC 容器的创建顺序SpringMVC 管理的是控制层组件,其他的组件交给 Spring 管理,控制层依赖于 Service 组件,又自动装配是在获取 IOC 容器时完成的,即 Controller 装配 Service 组件就是在获取 SpringMVC 的 IOC 容器时完成的,那么 Spring创建IOC容器要先与SpringMVC的创建时间 ,才能完成自动装配 。

# 1. SpringMVC 中 IOC 容器的创建时间 SpringMVC 的 IOC 容器是在 DispatcherServlet 初始化的过程中创建的,又 DispatcherServlet 注册的时候加入了 loadonstartup 标签,DispatcherServlet 初始化就提前到了服务器启动的时候。

# 2. Spring 中 IOC 容器的创建时间服务器启动时三大组件执行顺序:监听器,过滤器,Servlet

SpringMVC 的 IOC 容器是在 Servlet 的初始化方法中执行的 ,那我们把获取 Spring 的 IOC 容器的代码放在过滤器或者监听器的初始化方法中执行,就可以保证在获取 SpringMVC 的 IOC 容器时, Spring 的 IOC 容器是提前创建好的,完成 Controller 中对 Service 自动装配。

但是,过滤器并不适合,过滤器是用来过滤当前的请求和响应,如果我们在过滤器的初始化方法中写了创建 Spring 的 IOC 容器的代码,那在执行过滤的方法中什么都不写,直接放行的话,那创建过滤器的意义就没有了。

创建过滤器的初始化方法,销毁方法我们完全可以不写,最主要的就是 dofilter 方法,但是现在我们的过滤器就执行了一个初始化方法,之后执行过滤的时候 dofilter 没有写任何代码,我们不能为了实现一个目的而忽视了组件的最初目的。

所以创建 Spring 的 IOC 容器只能交给监听器了。

# 3. Spring 提供的监听器 ContextLoaderListener监听器常见的分为三种:ServletContextListener,HttpSesionListener,HttpSessionAttributeListener,后两个是监听 HttpSession 的,只能使用第一种。

ServletContextListener 里面有两个抽象方法 ServletContext 初始化方法,ServletContext 销毁方法。

代码语言:javascript代码运行次数:0运行复制public interface ServletContextListener extends EventListener {

void contextInitialized(ServletContextEvent var1);

void contextDestroyed(ServletContextEvent var1);

}Spring 提供了监听器 ContextLoaderListener,实现 ServletContextListener 接口,可监听

ServletContext 的状态,在 web 服务器的启动,读取 Spring 的配置文件,创建 Spring 的 IOC 容器。

代码语言:javascript代码运行次数:0运行复制public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

public ContextLoaderListener() {

}

public ContextLoaderListener(WebApplicationContext context) {

super(context);

}

public void contextInitialized(ServletContextEvent event) {

this.initWebApplicationContext(event.getServletContext());

}

public void contextDestroyed(ServletContextEvent event) {

this.closeWebApplicationContext(event.getServletContext());

ContextCleanupListener.cleanupAttributes(event.getServletContext());

}

}web 应用中必须在 web.xml 中配置

代码语言:javascript代码运行次数:0运行复制

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:spring.xml

# 2. SSM 整合步骤# 2.1 准备工作# ①导入依赖代码语言:javascript代码运行次数: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">

4.0.0

com.trs

SSMIntegration

1.0-SNAPSHOT

war

5.3.1

org.springframework

spring-context

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

3.5.7

org.mybatis

mybatis-spring

2.0.6

com.alibaba

druid

1.0.9

junit

junit

4.12

test

mysql

mysql-connector-java

8.0.16

log4j

log4j

1.2.17

javax.servlet

javax.servlet-api

3.1.0

provided

ch.qos.logback

logback-classic

1.2.3

com.fasterxml.jackson.core

jackson-databind

2.12.1

commons-fileupload

commons-fileupload

1.3.1

org.thymeleaf

thymeleaf-spring5

3.0.12.RELEASE

com.github.pagehelper

pagehelper

5.2.0

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

# ②创建表代码语言:javascript代码运行次数:0运行复制CREATE TABLE `t_emp` (

`emp_id` int(11) NOT NULL AUTO_INCREMENT,

`emp_name` varchar(20) DEFAULT NULL,

`age` int(11) DEFAULT NULL,

`sex` char(1) DEFAULT NULL,

`email` varchar(50) DEFAULT NULL,

PRIMARY KEY (`emp_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8# ③包结构# 2.2 配置 xml相比单独的 SpringMVC,整合时多配置了 ****ContextLoaderListener****,主要作用是 Spring 的监听器,在服务器启动的时候加载 Spring 的配置文件。

代码语言:javascript代码运行次数:0运行复制

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_4_0.xsd"

version="4.0">

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

CharacterEncodingFilter

/*

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter

/*

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

DispatcherServlet

/

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:spring.xml

# 2.3 创建 SpringMVC 的配置文件并配置需要什么组件在里面注册即可。

代码语言:javascript代码运行次数:0运行复制

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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

class="org.thymeleaf.spring5.view.ThymeleafViewResolver">

这里解释一下以下三者的关系,顺便复习一下前面的知识

代码语言:javascript代码运行次数:0运行复制

配置 原因:SpringMVC 将接收到的所有请求都看作是一个普通的请求,包括对于静态资源的请求。这样以来,所有对于静态资源的请求都会被看作是一个普通的后台控制器请求,导致请求找不到而报 404 异常错误。

对于这个问题 Spring MVC 在全局配置文件中提供了一个 标签。在 WEB 容器启动的时候会在上下文中定义一个 DefaultServletHttpRequestHandler,它会对 DispatcherServlet 的请求进行处理,如果该请求已经作了映射,那么会接着交给后台对应的处理程序,如果没有作映射,就交给 WEB 应用服务器默认的 Servlet 处理,从而找到对应的静态资源,只有再找不到资源时才会报错。

只配置 表示浏览器发送的请求都由默认的 servlet 处理,控制层所写的请求映射就处理不了了。

只配置 ,只有视图控制器所配置的路径才会被解析,控制层以及静态资源所写的请求映射就处理不了了。

配置 ,让控制层所写的请求映射被处理。

# 2.4 搭建 MyBatis 环境# 2.4.1 创建属性文件 jdbc.properties代码语言:javascript代码运行次数:0运行复制jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&characterEncoding=utf-8

jdbc.username=root

jdbc.password=root# 2.4.2 创建 MyBatis 的核心配置文件 mybatis-config.xml其余的配置都在 Spring 配置文件中,这里面的所有内容都可以在 Spring 配置文件中配置,我配置了一部分

代码语言:javascript代码运行次数:0运行复制

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

# 2.4.3 创建 Mapper 接口和映射文件代码语言:javascript代码运行次数:0运行复制public interface EmpMapper {

List getAllEmp();

}代码语言:javascript代码运行次数:0运行复制

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

# 2.4.4 创建日志文件 log4j.xml代码语言:javascript代码运行次数:0运行复制

# 2.4.5 创建 Spring 的配置文件并配置SpringMVC 扫描的是 controller 包,Spring 就要把其他的包扫描了

代码语言:javascript代码运行次数:0运行复制

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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

# 2.4.6 测试功能# 1. 创建组件代码语言:javascript代码运行次数:0运行复制@Data

@AllArgsConstructor

@NoArgsConstructor

public class Emp {

private Integer empId;

private String empName;

private Integer age;

private Integer sex;

private String email;

}# 2. 创建控制层组件 EmpController代码语言:javascript代码运行次数:0运行复制@Controller

public class EmpController {

@Autowired

private EmpService empService;

@RequestMapping(value = "/emps",method = RequestMethod.GET)

public String getAllEmp(Model model){

List emps = empService.getAllEmp();

model.addAttribute("emps",emps);

return "emp_list";

}

}# 3. 创建接口 EmpService 及其实现类代码语言:javascript代码运行次数:0运行复制public interface EmpService {

List getAllEmp();

}代码语言:javascript代码运行次数:0运行复制@Service

@Transactional //事务注解

public class EmpServiceImpl implements EmpService {

@Autowired

private EmpMapper empMapper;

@Override

public List getAllEmp() {

List emps = empMapper.getAllEmp();

return emps;

}

}# 4. 创建页面index.html

代码语言:javascript代码运行次数:0运行复制

首页

首页

emp_list.html

代码语言:javascript代码运行次数:0运行复制

员工列表

员工列表
流水号 员工姓名 年龄 姓别 邮箱 操作

删除

修改

# 5. 访问测试功能# 3. 总结SSM 整合核心有以下几点:

Spring 的 IOC 容器创建先于 SpringMVC 的 IOC 容器SpringMVC 扫描的是 controller 包,Spring 要把其他的包扫描了,两个框架管理各自的组件了解 ContextLoaderListener 监听器的作用,在服务器启动的时候加载 Spring 的配置文件。Spring 配置文件代替原来的 mybatis-config.xml其余的配置与单独使用时基本一致。