1. SSM 整合
1.1 搭建整合环境
1.1.1 整合说明
SSM整合可以使用多种方式,选择XML + 注解的方式
1.1.2 整合的思路
- 先搭建整合的环境
- 先把Spring的配置搭建完成
- 再使用Spring整合SpringMVC框架
- 最后使用Spring整合MyBatis框架
1.1.3 创建数据库和表结构
1 | create database ssm; |
1.1.4 创建maven的工程
1 | <groupId>com.wgy</groupId> |
1.1.5 导入依赖
1 | <properties> |
1.1.6 编写实体类
1 | /** |
1.1.7 编写dao接口
1 | /** |
1.1.8 编写service接口和实现类
接口:
1 | /** |
实现类:
1 | /** |
1.2 整合步骤
1.2.1 保证 Spring 框架在 web 工程中独立运行
1.2.1.1 编写 spring 配置文件并导入约束
1 |
|
1.2.1.2 测试 spring
1 | /** |
1.2.2 保证 SpringMVC 在 web 工程中独立运行
1.2.2.1 在 web.xml 中配置核心控制器
1 | <!--配置前端控制器--> |
1.2.2.2 编写 SpringMVC 的配置文件
1 |
|
1.2.2.3 编写 Controller 和 和 jsp 页面
JSP:
1 | <a href="account/findAll">查询所有</a> |
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> |
controller:
1 | /** |
1.2.3 整合 Spring 和 SpringMVC
目的:在controller中能成功的调用service对象中的方法。
1.2.3.1 配置监听器实现启动服务创建容器
web.xml
1 | <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件--> |
1.2.3.2 在controller中注入service对象
1 | /** |
1.2.4 保证 MyBatis 框架在 web 工程中独立运行
1.2.4.1 编写SqlMapConfig.xml的配置文件
1 |
|
1.2.4.2 AccountDao使用注解编写SQL语句
1 | /** |
1.2.4.3 测试MyBatis
1 | /** |
1.2.5 整合 Spring 和 MyBatis
目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中,SqlMapConfig.xml文件删除。
1.2.5.1 Spring 接管 MyBatis 的 Session 工厂
1 | <!--Spring整合MyBatis框架--> |
1.2.5.2 配置自动扫描所有 Mapper 接口
1 | <!--配置AccountDao接口所在包--> |
1.2.5.3 配置 spring 的事务
1 | <!--配置Spring框架声明式事务管理--> |
1.2.5.4 在service中注入dao对象
1 | /** |
1.2.6 测试 SSM 整合结果
1.2.6.1 编写测试 jsp
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
1.2.6.2 修改控制器中的方法
1 | /** |
-------------本文结束感谢您的阅读-------------
本文作者:
Wgy
本文标题: SpringMVC(三)
本文链接: https://wgy1993.gitee.io/archives/94a25162.html
版权声明: 本作品采用 CC BY-NC-SA 4.0 进行许可。转载请注明出处!
本文标题: SpringMVC(三)
本文链接: https://wgy1993.gitee.io/archives/94a25162.html
版权声明: 本作品采用 CC BY-NC-SA 4.0 进行许可。转载请注明出处!
