• spring mvc与mybatis事务整合


    之前公司用的是mybatis,但事务管理这块是用ejb的CMT容器管理的事务。基本原理是ejb请求进来,业务代码会创建一个mybatis的session然后放入当前线程,之后所有的方法操作涉及到数据库的都从当前线程取session。当所有service层代码完成后,退出ejb时,根据是否有异常来决定是否回退事务,这部分由拦截器来做(回退时,只在事务状态实体上设置rollback为true),等整个ejb退出时,容器再根据标记最终提交或回退事务。

     相比现在公司用的ejb事务,一个请求一个事务,有些场景就不太灵活了,而且还必须用支持ejb的容器,我们用的是jboss。

     这几天,将mybatis与mybatis-spring进行结合,用spring来管理事务。发现在整合过程中,碰到了事务不起作用。这里记录下。

     整理步聚如下:

      1.首先需要在maven里引入以下jar:

            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.2</version>
            </dependency>
          

         spring相关的jar,用的版本是3.0.0.RELEASE

      

      2.接着需要配置事务文件,这里对service层做事务代理,所有除spring mvc Controller中相关的bean定义放在:applicationContext.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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p"
        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/aop
        http://www.springframework.org/schema/aop/spring-aop-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/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

        <context:component-scan base-package="com.video.*">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>

        <tx:annotation-driven transaction-manager="transactionManager" />

        <context:property-placeholder location="classpath:MySQL.properties" />
        
        <!-- 这个数据库需要引用apache的commons-dbcp jar包 -->
        <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="${username}" />
            <property name="password" value="${password}" />
        </bean>
        
        <!-- 事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        
        <!-- sqlSessionFactory的datasource必须与上面transactionManager的datasource相同 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath*:com/video/mapper/**/*.xml" />
        </bean>
        
        <!-- 创建一个sqlSession实例,线程安全的,可以在所有DAO实例共享,原理是将sqlSession,事务与当前线程挂钩 -->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory" />
        </bean>
        
        <!-- 创建dao层,注入一个sqlSession -->
        <bean id="userDAO" class="com.video.dao.UserDAOImpl">
            <property name="sqlSession" ref="sqlSession" />
        </bean>

    </beans>


    这里需要特别注意配置中的:

        <context:component-scan base-package="com.video.*">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>

        <tx:annotation-driven transaction-manager="transactionManager" />

      tx:annotation-driver,这个标记,主要是这里用的是注解事务,这个开启后spring会在  "com.video.下面所有类中扫描含有@Transactional的标识,并生成相应的代理(默认是基于接口的JDK动态代理),也可以加上proxy-target-class="true",使用cglib类代理。这时需要加入cglib jar。

         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 

       这个标记是表示不要扫描spring mvc相关的controller

      spring mvc相关controller实例的扫描生成,由web容器启动时加载webrequest-servlet.xml的内容时进行处理。

     下面是spring mvc组件定义的webrequest-servlet.xml文件

    3.webrequest-servlet.xml 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:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        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/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">

        <!-- 启用spring mvc 注解 -->
        <context:annotation-config />

        <!-- 设置使用注解的类所在的jar包,这里必须排除扫描service层的组件 -->
        <context:component-scan base-package="com.video.*">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>

        <!-- 完成请求和注解POJO的映射 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

        <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/jsp/" p:suffix=".jsp" />

    </beans>

    注意这里的:

     <context:component-scan base-package="com.video.*">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>

    排除了扫描@service的bean。如果不排除,这里由于web启动时,首先加载这个webrequest-servlet.xml文件,这时将生成一个没有事务的service实例注入到controller。


      所有的配置已经完成,注意上面提到的扫描排除的问题。

    4
  • 相关阅读:
    ATM项目分析
    Python常用模块大全
    一文了解@Conditional注解说明和使用
    Spring IOC源码分析之-刷新前的准备工作
    Spring Cloud Zuul API服务网关之请求路由
    ArrayList相关方法介绍及源码分析
    记一次序列化的JSON解析问题
    大型网站架构演化发展历程
    Spring Cloud Ribbon负载均衡
    Spring Cloud Hystrix 服务容错保护
  • 原文地址:https://www.cnblogs.com/root429/p/9251399.html
Copyright © 2020-2023  润新知