一、概述:
所謂ssm框架,就是Spring、SpringMVC和Mybatis框架的集合。
二、對web.xml的配置
1、配置樣式如下:
<?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_3_1.xsd"
version="3.1">
<!--Spring核心監聽器-->
<!--負責啓動spring容器的監聽器,它將引用上下文參數獲得sprng配置文件applicationContext.xml的地址,-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置由Spring提供的針對中文亂碼的編碼過濾器-->
<!--編碼過濾器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置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:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置首頁-->
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、啓動spring容器的具體步驟,如下:
- 當web容器啓動時,監聽器開始自動運行
- 它會根據contextConfigLocation參數獲取spring配置文件,並啓動spring容器
3、web.xml的加載過程
- 啓動一個web項目的時候,web項目會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個標籤節點。
- 緊接着,容器創建一個ServletContext(即Servlet上下文),這個web項目的所有代碼以及配置文件都將共享這個ServletContext(可以是mybatis配置文件,也可以是servlet配置文件)
- 容器將<context-param>轉換為鍵值對,並將其交給servletContext,即加載這個配置文件
- 容器創建<listener>中的類實例,創建監聽器。
三、對ApplicationContext.xml的配置
1、對數據源以及數據源文件的配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 數據庫的驅動程序 -->
<property name="driverClass" value="${db.driverClass}" />
<!-- 數據庫的連接地址 -->
<property name="jdbcUrl" value="${db.url}" />
<!-- 數據庫的用户名 -->
<property name="user" value="${db.user}" />
<!-- 數據庫的連接密碼 -->
<property name="password" value="${db.password}" />
<!-- 數據庫最大的可用連接數目 -->
<property name="maxPoolSize" value="1" />
<!-- 數據庫最小的可用連接數目 -->
<property name="minPoolSize" value="1" />
<!-- 初始化的連接數目 -->
<property name="initialPoolSize" value="1" />
<!-- 連接如果已經滿了之後等待空閒連接的時間 -->
<property name="maxIdleTime" value="20" />
</bean>
<context:property-placeholder location="classpath:database.properties" />
2、配置基於數據源的DataSourceTransactionManager事務管理器,該事務管理器負責基於註解的聲明式事務的管理
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定義所有事務的傳統屬性,這些方法都屬於服務層的操作方法 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="login*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="exists*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<aop:pointcut expression="execution(* cn.ylcto..service..*.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
備註: 1、配置數據事務管理,還有編程式的配置方式。故事務管理作為一個切面織入目標業務方法的周圍,業務方法完全從事務代碼中解脱出來,代碼的複雜度大大降低。
2、配置數據事務管理,Spring還提供了基於xml的事務配置。
3、@Transactional 註解一般被建議在業務實現類上使用,因為註解不能被繼承。
4、基於註解的配置方式涉及到三個角色:通過這種aop/tx定義的聲明式事務配置信息、業務bean和spring容器。IOC容器中的bean進行事務管理配置定義,再由spring將這些配置織入對應的bean中。
3、配置事務工廠
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>