springMVC+Mybatis框架搭建与 事务管理

来源:互联网 发布:淘宝寄到澳洲 编辑:程序博客网 时间:2024/04/19 09:42

一直很少弄事务管理,今天花了一天的时间,弄了下事务管理,发现其中遇到很多问题,就是事务不起作用。后面终于知道原因了。
在mvc的配置文件中,如果扫描时,不去除service的扫描,那么在容器进行扫描时,直接注入service类,此时事务不能再加入到service类上。具体配置如下:

<!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。   -->       <context:component-scan base-package="com.zhuzhu.controller" use-default-filters="false">          <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />     </context:component-scan>

具体的框架事务配置:
1、目录结构
这里写图片描述

2、具体配置:
①applicationContext-DB.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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.xsd"    >    <!--此处扫描除controller以外的类,如@service,@component -->    <context:component-scan base-package="com.zhuzhu">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />    </context:component-scan>    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        destroy-method="close">        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />        <!--数据库连接url -->        <property name="jdbcUrl" value="xxxxxxx" />        <property name="user" value="xxx" />        <property name="password" value="xxx" />    </bean>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="configLocation" value="" />        <property name="dataSource" ref="dataSource"/>    </bean>    <bean id="transactioManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <tx:annotation-driven transaction-manager="transactioManager"/>    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->      <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.zhuzhu.dao" />        <property name="sqlSessionFactory" ref="sqlSessionFactory" />    </bean> </beans>

②spring-mvc.xml配置文件

<?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:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        <!-- MVC注解驱动 -->    <mvc:annotation-driven>    </mvc:annotation-driven>    <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。   -->       <context:component-scan base-package="com.zhuzhu.controller" use-default-filters="false">          <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />     </context:component-scan>     <mvc:default-servlet-handler/>    <!-- 定义视图解释器 -->    <!-- <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/" />        <property name="suffix" value=".jsp" />    </bean>     --></beans>

③ web.xml配置:

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" 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>demo0103</display-name>    <!-- spring -->    <!-- web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet     1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。    2)它的值必须是一个整数,表示servlet应该被载入的顺序    2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;    3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。    4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。    5)当值相同时,容器就会自己选择顺序来加载。    所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。    -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath:spring/applicationContext-DB.xml        </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- springmvc -->    <servlet>        <servlet-name>springDispatcher</servlet-name>        <servlet-class>            org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- map all requests for /* to the dispatcher servlet -->    <servlet-mapping>        <servlet-name>springDispatcher</servlet-name>        <url-pattern>/*</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index1.jsp</welcome-file>    </welcome-file-list></web-app>

详情请查看代码:
代码下载地址

0 0