1. 程式人生 > >spring mvc與mybatis事務整合

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。

  所有的配置已經完成,注意上面提到的掃描排除的問題。

相關推薦

spring mvcmybatis事務整合

  之前公司用的是mybatis,但事務管理這塊是用ejb的CMT容器管理的事務。基本原理是ejb請求進來,業務程式碼會建立一個mybatis的session然後放入當前執行緒,之後所有的方法操作涉及到資料庫的都從當前執行緒取session。當所有service層程式碼完成

(2)、Spring BootMybatis整合( 多資料來源)

1、背景的介紹有時候我們需要做分庫分表,那麼肯定存在多資料來源,Spring Boot和Mybatis的多資料來源是如何整合的呢?比如說我們現在做了一個浪跡天涯管理的後臺系統,商品資訊是存在itemCenter資料來源中的,而與使用者相關的資訊是存在account資料來源中,

IDEA下創建Maven項目,並整合使用SpringSpring MVCMybatis框架

varchar bat 連接 pom.xml文件 http mave eat supported 分享 項目創建 本項目使用的是IDEA 2016創建。項目使用Spring 4.2.6,Mybatis3.4.0,Tomcat使用的是Tomcat8,數據庫為MySQL。 首

SpringMVC系列(十五)Spring MVCSpring整合時實例被創建兩次的解決方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的關系

問題 nbsp frame ota 展示 not als pri exc 一、Spring MVC與Spring整合時實例被創建兩次的解決方案 1.問題產生的原因 Spring MVC的配置文件和Spring的配置文件裏面都使用了掃描註解<context:compon

Spring整合Spring MVCMybatis進行Junit單元測試

我們可以在不啟動服務的情況下,進行單元測試,以便提交出高質量的程式碼。本文以一個小例子,說明在Spring中如何進行單元測試。 一:測試Controller 1:在pom.xml檔案中引入相關依賴 <properties> <!-- 設定專案編碼編碼 --&

6、Spring Boot MyBatis整合

1.6 Spring Boot 與MyBatis整合 簡介 詳細介紹如何在Spring Boot中整合MyBatis,並通過註解方式實現對映。 完整原始碼: 1.6.1 建立 spring-boot-mybatis 專案 pom檔案如下 <?xml version="1

JavaEE SpringMyBatis整合之傳統DAO方式整合(教材學習筆記)

在實際開發中MyBatis都是與Spring整合在一起使用的,在之前學習了MyBatis與Spring,現在來學習如何使他們整合 首先建立一個名為chapter10的web專案 一、環境搭建 1.準備好所有的有關jar包,具體如下: 將上面所有jar包新增到專案lib目錄下

SpringMybatis整合流程

Spring與Mybatis的整合 1、什麼是Spring和Mybatis整合的原理 Mybatis是一個持久層框架,它將我們平時用的jdbc進行封裝,從而進一步地簡化程式碼,不過Mybatis要依賴於資料來源,這個資料來源可以是mybatis自帶的,也可以引入第三方的資料來源,第三

Spring boot 整合 ssm(springspring MVCMybatis

Spring boot 簡介:     1.內建了tomcat, 主語內部整合 ssm打包是一個war包 web工程,boot是jar包,就是一個主函式執行。     2.配置簡化       boot是一種主流的微服務的實現。     3.boot的使用:建議前

springmybatis oracle 整合

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

spring boot mybatis整合

本專案使用的環境: 開發工具:Intellij IDEA 2018.1.3 springboot: 2.0.5.RELEASE jdk:1.8.0_161 maven:3.5.3 首先先建立springboot專案 選擇需要的模組 填寫包名 1.開始在專案

Springmybatis整合實踐之SqlSessionTemplate持久化模板詳解

今天用SqlSessionTemplate持久化模板來整合spring和mybatis,其實差別不大,就是spring的配置檔案裡改一下,測試類改一下就可以了,如下 這是spring控制檔案的主要內容,需要注意的就是不要忘了把sqlsession注入測試類 <!--

maven構建spring mvc專案 + Mybatis整合

一、新建專案 1、File>New>Other>Maven>Maven Project, 點選Next   2、選擇專案儲存路徑。然後點選Next。   3、選擇專案型別,在Artifact Id中選擇maven-archetype-webapp,然

SpringMybatis框架整合——資料分頁

1.建立結構如下圖的Maven專案 2.引入所需jar包的依賴座標 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XML

(四)Spring mvc Mybais整合

轉自:https://blog.csdn.net/yerenyuan_pku/article/details/72231763 為了更好的學習SpringMVC和MyBatis整合開發的方法,需要將SpringMVC和MyBatis進行整合。整合目標:控制層採用Spring

spring boot Mybatis整合(*)

業務層 tomcat ng- quest map big selectall esp 連接 在pom.xml文件中加入數據庫、spring-mybatis整合 <!-- spring boot 整合mybatis --> <de

spring mvc 配置 mybatis sql攔截器

mybatis直接上代碼:mybatis配置中 添加 <property name="plugins"> 如下: <bean id="sqlSessionFactory" class="com.hotent.core.mybatis.SqlSessionFactoryFactoryBea

spring bootjdbcTemplate的整合案例2

database bean n) ret struct mapping rri ott mode 簡單入門了spring boot後,接下來寫寫跟數據庫打交道的案例。博文采用spring的jdbcTemplate工具類與數據庫打交道。 下面是搭建的springbo

SpringbootMyBatis簡單整合

方法 jar 啟動 over resultmap prop toc 系統 服務器 之前搭傳統的ssm框架,配置文件很多,看了幾天文檔才把那些xml的邏輯關系搞得七七八八,搭起來也是很麻煩,那時我完全按網上那個demo的版本要求(jdk和tomcat),所以最後是各種問題沒成

Apache Shiro(三)——Spring Boot Shiro的 整合

在瞭解了Apache Shiro的架構、認證、授權之後,我們來看一下Shiro與Web的整合。下面以Spring Boot為例,介紹一下Spring Boot 與 Shiro的 整合。 一、建立一個Spring Boot專案 可以使用IDEA快速建立一個Spring Boot專