網站首頁 編程語言 正文
1簡介
dynamic-datasource-spring-boot-starter 是一個基于springboot的快速集成多數據源的啟動器。
其支持 Jdk 1.7+, SpringBoot 1.4.x 1.5.x 2.x.x。
2使用方式
2.1依賴準備
在pom.xml文件中添加maven依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
2.2配置數據源
修改application.yml文件,添加數據源:
spring:
datasource:
dynamic:
primary: master #設置默認的數據源或者數據源組,默認值即為master
datasource:
master:
url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
slave_1:
url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
slave_2:
diver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@xx.x.x.xxx:1521/orcl
username: ENC(5xRxUfgT71VlnqnNNyA==)
password: ENC(qcs40ScFvIlfK6X8UvBKg==)
#......省略
#以上會配置一個默認庫master,一個組slave下有兩個子庫slave_1,slave_2
2.3配置使用 @DS 切換數據源。
@DS 可以注解在方法上或類上,同時存在就近原則:方法上注解 優先于類上注解。
如果沒有@DS,就是默認數據源,即2.2中的“master”。
2.3.1 在Mapper中使用
某個Mapper對應的表只在確定的一個庫,建議直接注解在mapper上。
@Repository
@DS("slave_1")
public interface Test2Mapper extends BaseMapper {
HashMap testQuery(Map<String, Object> query);
void testUpdate(Map<String, Object> query);
}
2.3.2 在BLO的實現類或者方法上
注解在BLO的實現類的方法或類上,BLO主要是對業務的處理, 在復雜的場景涉及連續切換不同的數據庫。
@Slf4j
@Service
@Resource
@DS("slave_2")
public class TestBLOImpl extends BLOImpl implements TestBLO {
@Resource
private TestMapper testMapper;
@Resource
private Test2Mapper test2Mapper;
@Override
public Map<String, Object> testQuery(Map<String, Object> map) throws Exception {
HashMap result = new HashMap<>();
Map<String, Object> resultDB1 = testMapper.testQuery(map);
Map<String, Object> resultDB2 = test2Mapper.testQuery(map);
result.put("resultDB1",resultDB1);
result.put("resultDB2",resultDB2);
return result;
}
@DS("slave_1")
@Override
public Map<String, Object> testQuery2(Map<String, Object> map) throws Exception {
return testMapper.testQuery(map);
}
@Override
public Map<String, Object> testUpdate(Map<String, Object> map) {
testMapper.testUpdate(map);
return null;
}
}
2.4分頁攔截器配置
修改類MybatisPlusConfig中的mybatisPlusInterceptor方法,將分頁攔截器指定的數據庫類型去掉,框架會根據數據庫連接自動設置sql方言類型。
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
3事務控制
3.1使用方式
在最外層的方法添加 @DSTransactional,底下調用的各個類該切數據源就正常使用DS切換數據源即可。
@Service
public class TestBPOImpl extends BPOImpl implements TestBPO {
@Resource
TestBLO testBLO;
@Override
@DSTransactional
public Map<String, Object> testUpdate(Map<String, Object> map) {
return testBLO.testUpdate(map);
}
}
3.2注意事項
3.2.1 不能使用@Transactional進行事務控制
不能使用@Transactional進行事務控制,否則會導致@DS注解切換數據源失敗。
原文鏈接:https://blog.csdn.net/Jay_fanwj/article/details/129420925
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-02-28 CommonsChunkPlugin 插件使用方法 、 出現報錯 : Error: webpa
- 2022-05-26 基于Python實現將列表數據生成折線圖_python
- 2022-02-22 Element-UI二次封裝實現TreeSelect 樹形下拉選擇組件
- 2022-07-12 strcpy、strncpy與memcpy的區別你了解嗎?
- 2023-04-20 for循環生成表單,表單校驗失效
- 2022-11-12 PostgreSQL邏輯復制解密原理解析_PostgreSQL
- 2022-03-29 C#算法之兩數之和_C#教程
- 2022-07-03 Android使用ViewPager實現翻頁效果_Android
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支