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" />
以上都是些基本的配置,可根据不同的需求对配置进行修改
其实如果对这三个框架理解透一点,整合的配置文件怎么写就一定也不成问题了,但是通过整合后的配置文件的编写,也有助于理解这三个框架