<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SpringMVC Mybatis整合</display-name> <!-- web.xml的加载过程是context-param >> listener >> fileter >> servlet--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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 1.在applicationContext.xml后加载 防止mvc-dispatcher-config.xml里面的注册Bean冲掉了前面已经注册过了的Service和Dao ,保证了事务的可用性 2. 因为Controller 也依赖于Service和Bean 所以要晚于他们进行加载 --> <servlet> <servlet-name>springMvcDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc-dispatcher-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMvcDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- context-param 该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。 加载日志文件(log4j.xml|log4j.properties) --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- initialization Spring container BEGIN 加载除SpringMVC的组件以外的Bean实例--> <!-- Context Configuration locations for Spring XML files --> <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> <!-- initialization Spring container END--> </web-app>
<?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:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 加载除SpringMvc用的Bean --> <!-- 打开 注解annotation --> <context:annotation-config/> <!-- 扫描符合@Service @Repository的类 作为Spring容器管理的Bean --> <!-- Spring MVC启动时的配置文件,包含组件扫描、url映射以及设置freemarker参数, 让spring不扫描带有@Service注解的类。 为什么要这样设置?因为mvc-dispatcher-config.xml与applicationContext.xml不是同时加载, 如果不进行这样的设置,那么,spring就会将所有带@Service注解的类都扫描到容器中,等到加载applicationContext.xml的时候, 会因为容器已经存在Service类,使得cglib将不对Service进行代理,直接导致的结果就是在applicationContext.xml中的事务配置不起作用, 发生异常时,无法对数据进行回滚。 如果在springmvc配置文件,不使用com.ding.controller前缀, 而是使用com.ding,则service、dao层的bean可能也重新加载了, 但事务的AOP代理没有配置在springmvc配置文件中,从而造成新加载的bean覆盖了老的bean, 造成事务失效。只要使用use-default-filters=“false”禁用掉默认的行为就可以了。 --> <!-- user-default-filters解释:http://jinnianshilongnian.iteye.com/blog/1762632 --> <context:component-scan base-package="com.ding" use-default-filters="false"> <context:include-filter type="regex" expression="com.ding.(service|dao).*"/> </context:component-scan> <!-- 加载数据库配置文件 db.Properties --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:db.properties" /> </bean> <!-- DBCP数据源 --> <bean id="MyDataSource" 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}" /> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="maxWait" value="${dbcp.maxWait}" /> </bean> <!-- SqlSessionFactory 生成会话工厂加入配置文件和mapper映射xml --> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-configuration.xml" /> <property name="dataSource" ref="MyDataSource" /> <!-- 加入 Mybatis 映射文件mapper.xml --> <property name="mapperLocations" value="/WEB-INF/mapper/*Mapper.xml"/> </bean> <!-- SqlSessionTemplate --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="SqlSessionFactory" /> <!-- <constructor-arg index="1" value="BATCH" />如果想要进行批量操作可加入这个属性 --> </bean> <!-- 注册所有的Mapper接口 可用注解--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ding.mapper" /> </bean> <!-- 注册事务管理类--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="MyDataSource"/> </bean> <!-- 开启事务行为 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
<?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:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- ViewResolver InternalResourceViewResolver:用于支持Servlet、JSP视图解析; viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包; prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“/page/hello.jsp" --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/page/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 开启支持@Controller 和 @RequestMapping注解的处理器。 --> <!-- Spring3.1之前的注解 HandlerMapping <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> --> <!-- Spring3.1之前的注解 HandlerAdapter <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>--> <!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter --> <mvc:annotation-driven /> <!-- 加载静态资源 --> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/css/**" location="/css/"/> <!-- 自动扫描注解 只要控制器 :user-default-filters解释:http://jinnianshilongnian.iteye.com/blog/1762632 --> <context:component-scan base-package="com.ding.controller" use-default-filters="false"> <context:include-filter type="regex" expression="com.ding.controller.*"/> </context:component-scan> <!-- 共通异常处理 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView"> <value>error/error</value> </property> <property name="exceptionMappings"> <props> <prop key="java.sql.SQLException">error/sqlException</prop> </props> </property> </bean> </beans>
大致流程就是这样
项目源代码在:http://download.csdn.net/detail/dingsai88/8440673