# 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运行复制
# 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">
`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">
代码语言: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运行复制
对于这个问题 Spring MVC 在全局配置文件中提供了一个
只配置
只配置
配置
# 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
}代码语言:javascript代码运行次数:0运行复制
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from t_emp
# 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">
@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
model.addAttribute("emps",emps);
return "emp_list";
}
}# 3. 创建接口 EmpService 及其实现类代码语言:javascript代码运行次数:0运行复制public interface EmpService {
List
}代码语言:javascript代码运行次数:0运行复制@Service
@Transactional //事务注解
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List
List
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其余的配置与单独使用时基本一致。