之前公司用的是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
踩

spring mvc与mybatis事务整合的更多相关文章

  1. spring、spring mvc、mybatis框架整合基本知识

    学习了一个多月的框架知识了,这两天很想将它整合一下.网上看了很多整合案例,基本都是基于Eclipse的,但现在外面公司基本都在用Intellij IDEA了,所以结合所学知识,自己做了个总结,有不足之 ...

  2. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  3. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  4. SSM 三大框架系列:Spring 5 + Spring MVC 5 + MyBatis 3.5 整合(附源码)

    之前整理了一下新版本的 SSM 三大框架,这篇文章是关于它的整合过程和项目源码,版本号分别为:Spring 5.2.2.RELEASE.SpringMVC 5.2.2.RELEASE.MyBatis ...

  5. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  6. Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)

    Spring.Spring MVC.MyBatis整合(未万待续)

  7. Spring、Spring MVC、MyBatis

    Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Sp ...

  8. spring MVC、mybatis配置读写分离

    spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数 ...

  9. spring mvc与mybatis收集到博客

    mybaits-spring 官方教程 http://mybatis.github.io/spring/zh/ SpringMVC 基础教程 框架分析 http://blog.csdn.net/swi ...

随机推荐

  1. 如何使用Python 进行数据可视化

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 在进行数据分析的时候,经常需要将数据进行可视化,以方便我们对数据的认识和理解. 0,Matplotl ...

  2. odoo13之右上角弹出提示框

    前言 在odoo中已经提供好了右上角弹出提示框的接口,我们只需要调用即可: 而提示框的实现又分为前端js实现和后段函数实现,前后端实现的效果相同. 实现效果图 前端实现提示框 在前端中显示提示框最常用 ...

  3. Dotnet Core下的Channel, 你用了吗?

    今天给大家分享一个微软官方的好东西:Channel.   前言 今天给大家分享一个微软官方的生产者/消费者方案的特性解决:Channel. Channel在System.Threading.Chann ...

  4. JZOJ2020年9月5日提高组反思

    JZOJ2020年9月5日提高组反思 T1 考试的时候没有头绪,就打了个暴力,愉快的拿到了10分的\(impossible\) 正解是\(DP\),设\(f[i][j][k]\)表示地\(i\)种币值 ...

  5. 第6.5节 exec函数:一个自说自话的强大Python动态编译器

    在Python动态执行的函数中,exec是用于执行一个字符串内包含的Python源码或其编译后对应的字节码. 一.    语法 1.    exec(Code, globals=None, local ...

  6. 老猿学5G随笔:5G系统构成

    5G系统(5G智能计费方案)简称5GS,由以下部分组成: 用户设备:User Equipment,简写UE,用户访问网络的设备 5G接入网:5G Access NetWork,简写为5G-AN,负责用 ...

  7. PyQt(Python+Qt)学习随笔:QHeaderView.ResizeMode取值及含义

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 关于ResizeMode的使用请参考<PyQt(Python+Qt)学习随笔:QTableWi ...

  8. shell 编程 && bash 简介(shell 变量、shell操作环境、数据流重导向、管线命令、shell script)

    如何学习一门编程语言 数据类型 运算符 关键字 1 认识BASH 这个shell linux是操作系统核心,用户通过shell与核心进行沟通,达到我们想要的目的.硬件.核心.用户之间的关系: 原理:所 ...

  9. sourcetree的使用(配合git)

    主要讲解sourcetree的使用,是一个git提交的可视化软件,在官网上下载(windows,mac都有) 一路下载安装 首先,为了给本地sourcetree一个私钥,我们需要先下载git,然后在g ...

  10. c++11-17 模板核心知识(十五)—— 解析模板之依赖型类型名称与typename Dependent Names of Types

    模板名称的问题及解决 typename规则 C++20 typename 上篇文章c++11-17 模板核心知识(十四)-- 解析模板之依赖型模板名称 Dependent Names of Templ ...