SSM整合
SSM整合
在这之前我们已经学习了如何整合Spring和web项目,若要整合Spring,SpringMVC,Mybatis时,只需要在其基础上加入SpringMVC和MyBatis就可以了;
整合步骤:
1.准备空的web项目
2.在web.xml中配置Spring容器
3.在web.xml中配置SpringMVC核心控制器
4.整合MyBatis与Spring
pom依赖:
主要依赖类别:,webmvc(会自动依赖Spring其他核心jar) , web基础的(jstl,jsp,servlet),mybatis ,事务管理, AspectJ
<dependencies>
<!-- spring + springwebmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--事务管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--AspectJ-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
</dependency>
<!-- jstl jsp servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--扫描Controller所在的包-->
<context:component-scan base-package="com.kkb.controller"/>
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--mybatis -->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
<bean name="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="driverClassName" value="${driver}"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSouce"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kkb.dao"/>
</bean>
<!--mybatis END -->
<!--注解扫描 -->
<context:component-scan base-package="com.kkb.service"/>
</beans>
<!--别忘记自行准备一个jdbc.properties数据库相关配置文件-->
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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">
<!-- Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring END-->
<!-- SpringMVC-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- SpringMVC END-->
</web-app>
MyController.java
@Controller
public class MyController {
@Autowired
private CourseService courseService;
@RequestMapping("/courseList.action")
public ModelAndView courseList(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("courses.jsp");
modelAndView.addObject("courses",courseService.selectCourseList());
return modelAndView;
}
}
//别忘记CourseService注册到容器,注入Mapper 进行查询 篇幅太大不放了
courses.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<th>名称</th>
<th>讲师</th>
<th>开课日期</th>
<th>学分</th>
<th>课时</th>
<th>操作</th>
</tr>
<c:forEach items="${courses}" var="course">
<tr>
<td>${course.name}</td>
<td>${course.teachName}</td>
<td>
<fmt:formatDate value="${course.startTime}" pattern="yyyy-MM-dd"/>
</td>
<td>${course.score}</td>
<td>${course.hours}</td>
<td>
<a href="editCourse?id=${course.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
配置完成后启动服务器,访问:http://localhost:8080/SSMTest/courseList.action
案例中设计到的mapper,pojo以及SQL使用MyBatis逆向生成的;
到此SSM的整合就完成了; (事务配置和其他配置均与Spring项目没有区别)
事务AOP等配置与单独的Spring没有区别,但不过要注意现在有两个Spring配置文件,别写错位置;
Spring与SpringMVC容器关系:
SpringMVC容器固然是一个拥有Spring所有功能的容器,但是为了分离关注点,通常SpringMVC容器只处理与请求响应相关的内容,即Controller层;
Spring框架采用了父子容器的方式来实现分离关注点;实际上SpringMVC被作为了Spring的子容器;如下所示:
外层的Spring容器为父容器,里面的SpringMVC为子容器,要强调的是其他的框架并不是容器,只是容器中的一堆Bean;
职责分配:
- Spring容器主要负责集成其他框架,配置AOP,事务
- SpringMVC容器负责处理请求响应即Controller层
特点:
- 子容器可直接访问父容器中的Bean,而父容器不能访问子容器的Bean
- 子容器优先从自身里查找Bean,找不到时才会找父容器
容易出的问题:
事务处理通常在父容器中配置,因为MyBatis相关Bean是由父容器管理的,假设已经在父容器为Service层配置了事务,子容器没有配置事务,但是子容器却扫描了Service层,这时候事务就不生效了,因为子容器中有ServiceBean,但是却没有事务;