在這以前咱們已經學習瞭如何整合Spring和web項目,若要整合Spring,SpringMVC,Mybatis時,只須要在其基礎上加入SpringMVC和MyBatis就能夠了;javascript
1.準備空的web項目html
2.在web.xml中配置Spring容器java
3.在web.xml中配置SpringMVC核心控制器mysql
4.整合MyBatis與Springweb
主要依賴類別:,webmvc(會自動依賴Spring其餘核心jar) , web基礎的(jstl,jsp,servlet),mybatis ,事務管理, AspectJspring
<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.xmlsql
<?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.xmlapi
<?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配置文件,別寫錯位置;
SpringMVC容器當然是一個擁有Spring全部功能的容器,可是爲了分離關注點,一般SpringMVC容器只處理與請求響應相關的內容,即Controller層;
Spring框架採用了父子容器的方式來實現分離關注點;實際上SpringMVC被做爲了Spring的子容器;以下所示:
外層的Spring容器爲父容器,裏面的SpringMVC爲子容器,要強調的是其餘的框架並非容器,只是容器中的一堆Bean;
職責分配:
特色:
容易出的問題:
事務處理一般在父容器中配置,由於MyBatis相關Bean是由父容器管理的,假設已經在父容器爲Service層配置了事務,子容器沒有配置事務,可是子容器卻掃描了Service層,這時候事務就不生效了,由於子容器中有ServiceBean,可是卻沒有事務;