SSM,即 SpringMVC、Spring 與 MyBatis 三個框架
它們在三層架構中所處的位置是不同的,即它們在三層架構中的功能各不相同,各司其職
-
SpringMVC:作為 View 層的實現者,完成用戶的請求接收功能。SpringMVC 的 Controller
作為整個應用的控制器,完成用戶請求的轉發及對用戶的響應
-
MyBatis:作為 Dao 層的實現者,完成對數據庫的增、刪、改、查功能
-
Spring:以整個應用大管家的身份出現。整個應用中所有 Bean 的生命周期行為,均由
Spring 來管理。即整個應用中所有對象的創建、初始化、銷毀,及對象間關聯關系的維
護,均由 Spring 進行管理
持久層:DAO層(mapper)
- DAO層:DAO層主要是做數據持久層的工作,負責與數據庫進行聯絡的一些任務都封裝在此,
- DAO層的設計首先是設計DAO的接口,
- 然后在Spring的配置文件中定義此接口的實現類,
- 然后就可在模塊中調用此接口來進行數據業務的處理,而不用關心此接口的具體實現類是哪個類,顯得結構非常清晰,
- DAO層的數據源配置,以及有關數據庫連接的參數都在Spring的配置文件中進行配置。
業務層:Service層
- Service層:Service層主要負責業務模塊的邏輯應用設計。
- 首先設計接口,再設計其實現的類
- 接着再在Spring的配置文件中配置其實現的關聯。這樣我們就可以在應用中調用Service接口來進行業務處理。
- Service層的業務實現,具體要調用到已定義的DAO層的接口,
- 封裝Service層的業務邏輯有利於通用的業務邏輯的獨立性和重復利用性,程序顯得非常簡潔。
表現層:Controller層(Handler層)
- Controller層:Controller層負責具體的業務模塊流程的控制,
- 在此層里面要調用Service層的接口來控制業務流程,
- 控制的配置也同樣是在Spring的配置文件里面進行,針對具體的業務流程,會有不同的控制器,我們具體的設計過程中可以將流程進行抽象歸納,設計出可以重復利用的子單元流程模塊,這樣不僅使程序結構變得清晰,也大大減少了代碼量。
View層
- View層 此層與控制層結合比較緊密,需要二者結合起來協同工發。View層主要負責前台jsp頁面的表示.
各層聯系
- DAO層,Service層這兩個層次都可以單獨開發,互相的耦合度很低,完全可以獨立進行,這樣的一種模式在開發大項目的過程中尤其有優勢
-
Controller,View層因為耦合度比較高,因而要結合在一起開發,但是也可以看作一個整體獨立於前兩個層進行開發。這樣,在層與層之前我們只需要知道接口的定義,調用接口即可完成所需要的邏輯單元應用,一切顯得非常清晰簡單。
-
Service邏輯層設計
- Service層是建立在DAO層之上的,建立了DAO層后才可以建立Service層,而Service層又是在Controller層之下的,因而Service層應該既調用DAO層的接口,又要提供接口給Controller層的類來進行調用,它剛好處於一個中間層的位置。每個模型都有一個Service接口,每個接口分別封裝各自的業務處理方法。
SSM框架整合說明
整合Dao層
MyBatis配置文件 sqlMapConfig.xml
- 配置別名:用於批量掃描Pojo包
- 不需要配置mappers標簽,但一定要保證
mapper.java
文件與mapper.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="cn.itcast.ssm.po"/>
</typeAliases>
</configuration>
Spring配置文件 applicationContext-dao.xml
- 主要配置內容
- 數據源
- SqlSessionFactory
- mapper掃描器
- 這里使用
sqlSessionFactoryBeanName
屬性是因為如果配置的是sqlSessionFactory
屬性,將不會先加載數據庫配置文件及數據源配置
- 這里使用
<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 ">
<!-- 加載db.properties文件中的內容,db.properties文件中key命名要有一定的特殊規則 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置數據源 ,dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據庫連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<!-- mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描包路徑,如果需要掃描多個包,中間使用半角逗號隔開 -->
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
創建所需的Mapper.java
- 一般不動原始生成的po類,而是將原始類進行集成vo類
public interface ItemsMappperCustom{ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
創建POJO類對應的mapper.xml
<mapper namespace="test.ssm.mapper.ItemsMappperCustom">
<select id="findItemsList" parameterTyep="test.ssm.po.ItemsQueryVo" resultType="test.ssm.po.ItemsCustom"> select items.* from items where items.name like '%${itemsCustom.name}%'
整合service層
- 目標:讓spring管理service接口。
定義service接口
- 一般在ssm.service包下定義接口 eg:ItemsService
public interfae ItemsService{ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
定義ServiceImpl實現類
- 因為在
applicationContext-dao.xml
中已經使用了mapper掃描器,這里可以直接通過注解的方式將itemsMapperCustom自動注入。public class ItemsServiceImpl implements ItemsService{ @Autowired private ItemsMapperCustom itemsMapperCustom; @Override public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception{ return itemsMapperCustom.findItemsList(itemsQueryVo); } }
在spring容器配置service
applicationContext-service.xml
在此文件中配置service。<bean id="itemsService" class="test.ssm.service.impl.ItemsSrviceImpl"/>
事物控制
- 在
applicationContext-transaction.xml
中使用spring聲明式事務控制方法 - 對mybatis操作數據庫事物控制,spring使用jdbc的事物控制類是
DataSourceTransactionManager
- 因為操作了數據庫需要事物控制,所以需要配置數據源
- 定義了切面
<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 "> <!-- 事務管理器 對mybatis操作數據庫事務控制,spring使用jdbc的事務控制類 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 數據源在 dataSource在applicationContext-dao.xml中已經配置--> <property name="dataSource" ref="dataSource"/> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播行為 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/> </aop:config> </beans>
整合springmvc
- 創建
springmvc.xml
文件,配置處理器映射器 、 適配器、視圖解析器<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan> <!-- 使用 mvc:annotation-driven 加載注解映射器和注解適配器配置--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 視圖解析器 解析jsp解析,默認使用jstl標簽,classpath下的得有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的前綴 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置jsp路徑的后綴 --> <property name="suffix" value=".jsp"/> </bean>
配置前端控制器
- 在
web.xml
中加入如下內容 - contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等)
- 如果不配置contextConfigLocation,默認加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml)
- 在url-pattern中
- 填入
*.action
,表示訪問以.action結尾 由DispatcherServlet進行解析 - 填入
/
,所有訪問的地址都由DispatcherServlet進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet進行解析,使用此種方式可以實現RESTful風格的url
- 填入
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>*.action</url-pattern>
</servlet-mapping>
編寫Controller(Handler)
@Congtroller @RequestMapping("/items") //窄化路徑
public class ItemsController { @Autowired private ItemsService itemsService; //商品查詢
@RequestMapping("/queryItems") //實際網址后面跟了.action
public ModelAndView queryItems(HttpServletRequest request) throws Exception { List<ItemsCustom> itemsList = itemsService.findItemsList(null); //返回ModelAndView
ModelAndView modelAndView = new ModelAndView(); //相當於request的setAttribute,在jsp頁面中通過itemsList取數據
modelAndView.addObject("itemsList",itemsList); return modelAndView; } }
編寫JSP頁面
<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 }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
加載spring容器
- 在
web.xml
中,添加spring容器監聽器,加載spring容器<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener>
參考bolg:
http://blog.csdn.net/wj880627/article/details/52973854
http://blog.csdn.net/lutianfeiml/article/details/51864160