SSM整合之配置檔案的編寫
web.xml的編寫
web.xml的編寫
web.xml中主要包含springMVC相關的配置和Spring相關配置
-
springMVC相關的配置:通過配置<servlet>來配置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:config/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- Spring相關配置:
<!-- 配置spring配置檔案 路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationcontext.xml</param-value> </context-param> <!-- 應用程式啟動則建立了IOC容器 : 通過ContextLoaderListener 監聽器來建立 IOC容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Mybatis的配置檔案
Mybatis主要用于實作實體類和資料庫表的映射。整合過後mybatis的配置檔案是比較簡潔的,用來為實體類取别名<typeAliases> <package name="com.etc.entity"/> </typeAliases>
SpringMVC的配置檔案
SpringMVC主要用于控制層,負責視圖、資料的轉發。以下為 SpringMVC相關配置<!-- 掃描控制器對象 --> <context:component-scan base-package="com.etc.controller" /> <mvc:annotation-driven /> <!-- 靜态資源通路 --> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/css/" mapping="/css/**" /> <!-- 視圖分解 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean>
Spring配置檔案
Spring主要用于對bean的管理。以下是 Spring相關配置<!-- 掃描包中的類 --> <context:component-scan base-package="com.etc.service" /> <mvc:annotation-driven/> <!-- 加載資料庫連接配接資訊的屬性檔案 jdbc.properties --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:config/jdbc.properties" /> </bean> <!-- 配置資料源,資料源提供了與資料庫連接配接的技術及連接配接池技術 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="initialSize" value="5" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="10" /> <property name="minIdle" value="5" /> <property name="maxWait" value="1200" /> </bean> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置資料庫連接配接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加載mybatis配置檔案 --> <property name="configLocation" value="classpath:config/mybatis-config.xml"/> <!-- 自動掃描dao下的Xx.xml檔案 --> <property name="mapperLocations" value="classpath:com/etc/dao/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- dao接口所在包名,Spring會自動查找其下的類并注入到Spring的容器中 --> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> <property name="basePackage" value="com.etc.dao"/> </bean> <!-- 聲明事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 綁定資料源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 啟動事務 --> <tx:annotation-driven transaction-manager="transactionManager" />
以上都是些基本的配置,可根據不同的需求對配置進行修改
其實如果對這三個架構了解透一點,整合的配置檔案怎麼寫就一定也不成問題了,但是通過整合後的配置檔案的編寫,也有助于了解這三個架構