一、三者职责分工
框架 | 核心职责 | 对应代码/配置 |
---|---|---|
Spring | 管理组件生命周期、依赖注入(IoC)、事务管理、整合数据源与 MyBatis。 | spring.xml (数据源、SqlSessionFactory、包扫描)、@Service 、@Autowired 等注解。 |
Spring MVC | 处理 HTTP 请求与响应,通过控制器(Controller)协调业务逻辑与视图渲染。 | springmvc.xml (控制器扫描、静态资源处理)、@RestController 、@RequestMapping 等注解。 |
MyBatis | 实现数据库操作,通过 Mapper 接口与 XML 文件定义 SQL,简化 JDBC 操作。 | UserMapper.java (接口)、UserMapper.xml (SQL 映射)、MyBatis-Spring 整合配置。 |
二、整合流程与代码解析
1. 请求处理全流程
graph TD
A[客户端请求 /user] --> B[DispatcherServlet]
B --> C[HandlerMapping 匹配控制器]
C --> D[UserContreoller.selectAll()]
D --> E[UserService.selectAll()]
E --> F[UserMapper.selectAll()]
F --> G[MyBatis 执行 SQL 查询]
G --> H[返回 JSON 数据]
H --> I[客户端接收响应]
2. Spring 的核心作用
- 依赖注入(DI)与组件管理
- 代码示例 :
UserService
中通过@Autowired
注入UserMapper
:
- 代码示例 :
java
@Service
public class UserService {
@Autowired
private UserMapper userMapper; // Spring 自动注入 MyBatis 代理对象
}
- **配置支持**:
* `spring.xml` 中通过 `<context:component-scan>` 扫描 `cn.cjxy` 包(排除 Controller),管理 Service 和 Mapper 组件。
* `<bean>` 定义数据源和 MyBatis 工厂:
xml
<!-- 数据源配置 -->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${name}"/>
</bean>
<!-- MyBatis 工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
</bean>
3. Spring MVC 的核心作用
- 控制器与请求映射
- 代码示例 :
UserContreoller
处理/user
请求:
- 代码示例 :
java
@RestController // 表示返回 JSON 数据
@RequestMapping("/user")
public class UserContreoller {
@GetMapping
public List<User> selectAll() { ... }
}
- **配置支持**:
* `springmvc.xml` 中通过 `<context:component-scan>` 仅扫描 `@Controller` 注解。
* `<mvc:annotation-driven>` 启用注解驱动的控制器(如 `@GetMapping`)。
4. MyBatis 的核心作用
- 数据持久化与 SQL 映射
- 代码示例 :
UserMapper.xml
定义 SQL:
- 代码示例 :
xml
<!-- UserMapper.xml -->
<mapper namespace="cn.cjxy.mapper.UserMapper">
<select id="selectAll" resultType="User">
SELECT * FROM user
</select>
</mapper>
- **整合 Spring**:
* `spring.xml` 中配置 `MapperScannerConfigurer`,自动扫描 Mapper 接口并生成代理对象:
xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.cjxy.mapper"/>
</bean>
三、关键整合点剖析
1. 分层架构与依赖传递
- Controller → Service → Mapper
- 依赖关系:
java
// Controller 依赖 Service
@Autowired
private UserService userService;
// Service 依赖 Mapper
@Autowired
private UserMapper userMapper;
- **Spring 保障**:通过 IoC 容器自动注入依赖,无需手动创建对象。
2. 配置分离与职责清晰
配置文件 | 作用 |
---|---|
spring.xml | 管理数据源、MyBatis 工厂、Service/Mapper 组件扫描(排除 Controller)。 |
springmvc.xml | 管理控制器、静态资源处理、JSON 序列化(通过 <mvc:annotation-driven> )。 |
web.xml | 配置 Spring 监听器、DispatcherServlet、字符编码过滤器,启动 Spring 和 Spring MVC 容器。 |
3. 数据源与事务管理
- Druid 数据源:
xml
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${name}"/> <!-- 从 jdbc.properties 读取 -->
</bean>
- 事务管理(需补充) :
当前配置缺少事务管理器,建议在spring.xml
中添加:
xml
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!-- 启用注解驱动事务 -->
<tx:annotation-driven/>
四、潜在问题与改进建议
1. 配置问题
- 属性占位符错误 :
spring.xml
中引用jdbc.properties
的键名应为username
,但配置中使用了${name}
。
修正:
properties
# jdbc.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root # 原配置中键名是 name,应改为 username
password=xxxx
2. 依赖兼容性
- MyBatis-Spring 版本 :
MyBatis 3.5.10 需搭配 MyBatis-Spring 2.0.7+,当前版本为 2.0.5,建议升级:
xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
五、总结
- Spring 是"粘合剂",管理所有 Java 组件(Bean)和资源(数据源、事务)。
- Spring MVC 是"交通指挥员",负责将 HTTP 请求分发给对应的控制器。
- MyBatis 是"数据库操作员",通过接口和 XML 实现 SQL 与 Java 代码的解耦。
通过分层架构和依赖注入,SSM 实现了高内聚低耦合,使各层职责清晰、易于维护。理解三者协作流程后,可快速定位配置错误(如属性注入失败、Mapper 未扫描)并优化代码结构。