SSM(Spring+SpringMVC+Mybatis)整合
1. 创建 maven 工程

pom.xml 文件添加依赖
1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
2. config 包下的Spring配置
2.1 springConfig
1 | // 配置类 |
事务处理步骤:
- 开启注解式事务驱动
- 配置事务管理器 (
JdbcConfig中添加) - 添加事务 (
@Transactional添加到接口上)
2.2 jdbc.properties
1 | jdbc.driver=com.mysql.jdbc.Driver |
2.3 jdbcConfig
1 | public class JdbcConfig { |
2.4 MybatisConfig
1 | public class MybatisConfig { |
2.5 spring整合SpringMVC配置( ServletConfig 和 SpringMvcConfig )
1 | public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { |
getRootConfigClasses 根配置,getServletConfigClasses 专门应对 Web 请求处理的。两个造出来的容器不一样 ,称为父子容器 (Spring 的容器为父容器,SpringMVC 的容器为子容器),SpringMVC 的容器可以访问 Spring 的容器,但是 Spring 的容器不能访问 SpringMVC 的容器。
1 |
|
3. 数据库表
1 | create table tbl_book ( |

4. 模块
4.1 domain
1 | public class Book { |
4.2 Dao 接口
1 | public interface BookDao { |
4.3 Service 接口
1 | // 声明式事务 |
4.4 Service 层的实现类
1 |
|
4.5 Controller 类
1 |
|
5. 测试业务层
1 |
|
6.定义 Controller 返回结果
6.1 Result
1 | public class Result { |
6.2 Code 状态码
1 | public class Code { |
7. 异常处理器
7.1 @RestControllerAdvice
1 |
|
7.2 自定义系统类异常
1 | public class SystemException extends RuntimeException{ |
7.3 自定义业务类异常
1 | public class BusinessException extends RuntimeException{ |
8. SpringMvcSupport
过滤访问静态资源
1 |
|
9. 拦截器(Interceptor)
1 |
|