简单的spring--mvc整合
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 指定扫描包,并注入注解类 --> <context:component-scan base-package="com.chinasoft.ssm" /> <!--读入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath*:jdbc.properties</value> </property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 文件映射器,指定类文件 --> <!-- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> --> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/chinasoft/ssm/mapper/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.chinasoft.ssm.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SSM</display-name> <!-- 项目从此处开始进入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 项目启动的时候读取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 当页面访问时请求经过DispatcherServlet --> <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-mybatis.xml</param-value> </init-param> </servlet> <!-- 加载一系列的servlet类 --> <servlet> <servlet-name>SafeCodeImageServlet</servlet-name> <servlet-class>com.chinasoft.ssm.servlet.SafeCodeImageServlet</servlet-class> </servlet> <servlet> <servlet-name>CheckCode</servlet-name> <servlet-class>com.chinasoft.ssm.servlet.CheckCode</servlet-class> </servlet> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>com.chinasoft.ssm.servlet.ImageServlet</servlet-class> </servlet> <servlet> <servlet-name>graphicsImage</servlet-name> <servlet-class>com.chinasoft.ssm.servlet.graphicsImage</servlet-class> </servlet> <!-- servlet类的映射文件 --> <servlet-mapping> <servlet-name>SafeCodeImageServlet</servlet-name> <url-pattern>/servlet/SafeCodeImageServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.mvc</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CheckCode</servlet-name> <url-pattern>/servlet/CheckCode</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/ImageServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>graphicsImage</servlet-name> <url-pattern>/servlet/graphicsImage</url-pattern> </servlet-mapping> <!-- 加载拦截器,此处为一个编码格式拦截器 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 自己配置的验证登录的拦截器 --> <filter> <filter-name>checkLogin</filter-name> <filter-class>com.chinasoft.ssm.util.CheckLogin</filter-class> </filter> <filter-mapping> <filter-name>checkLogin</filter-name> <url-pattern>/index.jsp</url-pattern> </filter-mapping> </web-app>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>