ssm:springmvc、spring、mybatis這三個框架的整合,有耐心一步步走。
--WZY
一、SSM框架整合
1.1、整合思路
從底層整合起,也就是先整合mybatis與spring,然后在編寫springmvc。
1.2、開發需求
查詢商品列表(從數據庫中查詢)
1.3、創建web工程
現在ssm的工程創建就有區別於原先的dao、service、web這樣的三層目錄了,現在是mapper、service、controller這樣的目錄,mapper就相當於以前的dao、controller相當於以前的web,改變了名稱而已。不要因此看不懂了。
1.4、添加jar包
這種jar包,上網直接百度ssm整合的jar包即可
數據庫驅動、Mybatis的核心、依賴包、Mybatis與spring的整合包、Dbcp連接池包、Spring的包(包括springmvc的包)、Aop的依賴包、Jstl包、Common-logging包
1.5、開始整合mapper(mybatis與spring的整合)
詳細的整合思路講解:mybatis與spring的整合 這里我直接上代碼。步驟
1.5.1、SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 取別名,或者別的其他全局配置信息,就在這里編寫 --> <typeAliases> <!-- 別名為類名首字母大小寫都可以 --> <package name="com.wuhao.ms.domain"/> </typeAliases> <!-- 原先這里還有連接數據庫的一些配置,與spring整合后,都交由spring來管理 --> <!-- 加載mapper映射文件,使用通用的配置 --> <mappers> <package name="com.wuhao.ssm.mapper"/> </mappers> </configuration>
1.5.2、applicationContext-dao.xml的配置
這里需要注意一點,在指定mybatis的全局配置文件的路徑的時候,也就是在value="classpath:SqlMapConfig.xml"時,如果在創建的config的配置文件目錄下還有層級目錄,則這里需要加上,比如,config下面分為了mybatis和spring,那么這里就需要寫value="classpath:mybatis/SqlMapConfig.xml",看根據你自己的需求來編寫

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 引用java配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis的全局配置文件的路徑 --> <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> <!-- 數據源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 批量配置,這里是批量配置mapper代理,那么下面就不用配置id了。 我們想要獲取哪個mapper代理用這個格式:類名首字母小寫 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wuhao.ssm.mapper"></property> <!-- 默認不需要配置,但是如果有多個數據源的配置,那么就需要配置此項 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
1.5.3、db.properties配置

db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 db.username=root db.password=root
1.5.4、開發mapper,將逆向工程生成的添加進來
注意:Mapper開發時,先要根據需求進行分析,是否匹配逆向工程生成的代碼,如果匹配成功,則不需要再開發mapper;如果不匹配,再去擴展一個新的mapper接口和mapper映射文件來處理該需求,通俗點講,就是逆向工程生成的mapper接口中的定義的功能是否滿足我們開發的需求,因為逆向工程生成的都是對於單表進行操作的,而我們有時候需要的是更復雜的查詢,所以如果有需要我們在自己創建mapper接口和mapper映射文件,其實就是擴展功能。
1.6、整合service
添加applicationContext-service.xml配置文件,用來處理事務,
applicationContext-service.xml:如果不懂其中的代碼的意思,就查看之前講解spring管理事務的文章。這里直接復制粘帖即可,修改一些包名稱等

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 組件掃描service --> <context:component-scan base-package="com.wuhao.ssm.service"></context:component-scan> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置數據源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播特性 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="select*" read-only="true" /> <tx:method name="get*" read-only="true" /> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wuhao.ssm.service.*.*(..))"/> </aop:config> </beans>
1.7、整合controller
也就是使用springmvc了。非常簡單。
1.7.1、在web.xml中配置前端控制器DispatcherServlet

<!-- springmvc 的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springmvc的配置文件的地址 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 這里有三種配置url-pattern方案 1、*.do:后綴為.do的請求才能夠訪問到該servlet[用這個] 2、/ :所有請求都能夠訪問到該servlet(除jsp),包括靜態請求(處理會有問題,不用) 3、/* :有問題,因為訪問jsp也會到該servlet,而訪問jsp時,我們不需要這樣,也不用 --> <url-pattern>*.do</url-pattern> </servlet-mapping>
1.7.2、配置springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 掃描 --> <context:component-scan base-package="com.wuhao.ssm.controller"></context:component-scan> <!-- 配置處理器映射器和處理器適配器 --> <mvc:annotation-driven /> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
1.8、整合spring配置文件
就是將所有的spring的配置文件都進行加載啟動。也就是在web.xml中配置spring的監聽器

<!-- 加載spring容器 --> <!-- 配置監聽器,用於加載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>
1.9、總結所有的配置如下圖
1.10、部署測試
1.10.1、查詢商品列表(從數據庫中查詢)
1、編寫service層
ItemsService 接口
ItemsServiceImpl 實現類 不使用注解開發
applicationContext-service.xml中配置該service的bean
ItemsServiceImpl 實現類 使用注解的話,就不需要在applicationContext-service.xml中配置該service的bean了
2、編寫controller層
該層的編寫有很多中方式,我記得前一節講解過,比如實現Controller接口,使用注解等,一般直接使用注解。
ItemsController
3、添加jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查詢商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post"> 查詢條件: <table width="100%" border=1> <tr> <td><input type="submit" value="查詢"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名稱</td> <td>商品價格</td> <td>生產日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/editItems.do?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
4、測試
http://localhost:8080/ssm_test01/queryItems.do 如下圖,即成功
二、總結
這樣,ssm的框架整合就結束了,非常簡單,按步驟,先整合mybatis與spring,然后在整合springmvc。自己練習幾遍就會了。接下來的文章就會以此為基礎,講解springmvc的各種小功能,比如,springmvc的參數綁定、springmvc的校驗器,圖片的上傳等。