Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解
項目場景:
Spring Boot集成com.baomidou,引入dynamic-datasource依賴,實現(xiàn)多數(shù)據(jù)源,這里說下事務(wù)問題:
1、一個方法中使用同一個數(shù)據(jù)源;
2、一個方法中使用了多個數(shù)據(jù)源;
解決方案:
這里把dao、service列出來
1、dao層
package com.test.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; //數(shù)據(jù)源1 @DS("db1") @Mapper public interface Test1Dao { @Update("update test1 set name = #{name} where id = #{id}") void updateById(@Param("id")Integer id, @Param("name")String name); }
package com.test.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.test.datasources.DataSourceNames; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; //數(shù)據(jù)源2 @DS(“db2”) @Mapper public interface Test2Dao { @Update("update test2 set name = #{name} where id = #{id}") void updateById(@Param("id")Integer id, @Param("name")String name); }
2、service層
package com.test.service; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.test.mapper.Test1Dao; import com.test.mapper.Test2Dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class TestService { @Autowired private Test1Dao test1Dao; @Autowired private Test2Dao test2Dao; /** * 同一個數(shù)據(jù)源中的事務(wù),都是數(shù)據(jù)源2 * 這里用的是spring的事務(wù)注解Transactional * 這里必須加上注解多數(shù)據(jù)源注解@DS("db2"),否則使用的是默認數(shù)據(jù)源 */ @DS("db2") @Transactional public void theSame() { test2Dao.updateById(2,"第一次修改"); test2Dao.updateById(2,"第二次修改"); //這里報錯回滾 int i = 1/0; } /** * 多數(shù)據(jù)源中的事務(wù),同時使用數(shù)據(jù)源1、2 * 如果這里用spring的事務(wù)注解Transactional,那么使用的是默認數(shù)據(jù)源 * 這里不需要加上注解@DS */ @DSTransactional public void notAlike() { test1Dao.updateById(1,"第一次修改"); test2Dao.updateById(2,"第二次修改"); //這里報錯回滾 int i = 1/0; } }
到此這篇關(guān)于Spring Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot @DSTransactional內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 用SpringBoot框架來接收multipart/form-data文件方式
- Springboot接收?Form?表單數(shù)據(jù)的示例詳解
- springboot2中使用@JsonFormat注解不生效的解決
- spring boot項目使用@JsonFormat失效問題的解決
- springboot如何接收application/x-www-form-urlencoded類型的請求
- spring中@Transactional?注解失效的原因及解決辦法
- Spring的編程式事務(wù)TransactionTemplate的用法詳解
- Spring模塊詳解之Spring ORM和Spring Transaction詳解
相關(guān)文章
如何在 Spring Boot 中配置和使用 CSRF 保護
CSRF是一種網(wǎng)絡(luò)攻擊,它利用已認證用戶的身份來執(zhí)行未經(jīng)用戶同意的操作,Spring Boot 提供了內(nèi)置的 CSRF 保護機制,可以幫助您防止這種類型的攻擊,這篇文章主要介紹了Spring?Boot?中的?CSRF?保護配置的使用方法,需要的朋友可以參考下2023-09-09Springboot @Validated和@Valid的區(qū)別及使用詳解
這篇文章主要介紹了Springboot @Validated和@Valid的區(qū)別及使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Java實現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl)
這篇文章主要介紹了Java實現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Java8默認方法Default Methods原理及實例詳解
這篇文章主要介紹了Java8默認方法Default Methods原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01SpringBoot接收參數(shù)所有方式總結(jié)
這篇文章主要介紹了SpringBoot接收參數(shù)所有方式總結(jié),文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07詳解設(shè)計模式在Spring中的應(yīng)用(9種)
這篇文章主要介紹了詳解設(shè)計模式在Spring中的應(yīng)用(9種),詳細的介紹了這9種模式在項目中的應(yīng)用,具有一定的參考價值,感興趣的可以了解一下2019-04-04