當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot框架DataSource多数据源配置
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot框架DataSource多数据源配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot框架DataSource多數據源配置
- 一、新增數據源填入xml文件
- 1、導入依賴
- 2、在配置文件中添加數據源信息
- 3、新建 自定義注解 DataSource
- 4、新建 DynamicDataSource
- 5、新建DynamicDataSourceContextHolder 做動態數據源切換處理
- 6、新建DataSourceType 添加對應的數據庫枚舉
- 7、新增 DruidConfig 配置類 配置讀配置源方法
- 8、新建 DruidProperties druid配置屬性
- 9、多數據源處理
- 10、配置完成 使用方法
- 2、完成
一、新增數據源填入xml文件
這里使用的是新搭建的一個架子,依賴什么的 就自己Maven工廠去下載吧
我是按照若依的架子搭建的,之前弄過一次,失敗了哈哈哈
進入正題
1、導入依賴
2、在配置文件中添加數據源信息
3、新建 自定義注解 DataSource
4、新建 DynamicDataSource
繼承Spring boot提供的AbstractRoutingDataSource 根據用戶定義的規則選擇當前的數據源
它的抽象方法 determineCurrentLookupKey() 決定使用哪個數據源。
全部代碼
5、新建DynamicDataSourceContextHolder 做動態數據源切換處理
public class DynamicDataSourceContextHolder {public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);/*** 使用ThreadLocal維護變量,ThreadLocal為每個使用該變量的線程提供獨立的變量副本,* 所以每一個線程都可以獨立地改變自己的副本,而不會影響其它線程所對應的副本。*/private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();/*** 設置數據源的變量*/public static void setDataSourceType(String dsType){log.info("切換到{}數據源", dsType);CONTEXT_HOLDER.set(dsType);}/*** 獲得數據源的變量*/public static String getDataSourceType(){return CONTEXT_HOLDER.get();}/*** 清空數據源變量*/public static void clearDataSourceType(){CONTEXT_HOLDER.remove();} }6、新建DataSourceType 添加對應的數據庫枚舉
新增數據源的話 在下方繼續添加枚舉
全部代碼
7、新增 DruidConfig 配置類 配置讀配置源方法
有多個數據源可以在下方照葫蘆畫瓢 哈哈哈
配置類全部代碼
嘶~~~~少了倆配置,補到后邊吧
8、新建 DruidProperties druid配置屬性
@Configuration public class DruidProperties {@Value("${spring.datasource.druid.initialSize}")private int initialSize;@Value("${spring.datasource.druid.minIdle}")private int minIdle;@Value("${spring.datasource.druid.maxActive}")private int maxActive;@Value("${spring.datasource.druid.maxWait}")private int maxWait;@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")private int timeBetweenEvictionRunsMillis;@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")private int minEvictableIdleTimeMillis;@Value("${spring.datasource.druid.maxEvictableIdleTimeMillis}")private int maxEvictableIdleTimeMillis;@Value("${spring.datasource.druid.validationQuery}")private String validationQuery;@Value("${spring.datasource.druid.testWhileIdle}")private boolean testWhileIdle;@Value("${spring.datasource.druid.testOnBorrow}")private boolean testOnBorrow;@Value("${spring.datasource.druid.testOnReturn}")private boolean testOnReturn;public DruidDataSource dataSource(DruidDataSource datasource) {/** 配置初始化大小、最小、最大 */datasource.setInitialSize(initialSize);datasource.setMaxActive(maxActive);datasource.setMinIdle(minIdle);/** 配置獲取連接等待超時的時間 */datasource.setMaxWait(maxWait);/** 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 */datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);/** 配置一個連接在池中最小、最大生存的時間,單位是毫秒 */datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);/*** 用來檢測連接是否有效的sql,要求是一個查詢語句,常用select 'x'。如果validationQuery為null,testOnBorrow、testOnReturn、testWhileIdle都不會起作用。*/datasource.setValidationQuery(validationQuery);/** 建議配置為true,不影響性能,并且保證安全性。申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。 */datasource.setTestWhileIdle(testWhileIdle);/** 申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。 */datasource.setTestOnBorrow(testOnBorrow);/** 歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。 */datasource.setTestOnReturn(testOnReturn);return datasource;} }9、多數據源處理
/*** @program: LanAn* 多數據源處理* @description: DataSourceAspect* @author: LanAn* @create: 2022-08-17 14:21**/ @Aspect @Order(1) @Component public class DataSourceAspect {protected Logger logger = LoggerFactory.getLogger(getClass());@Pointcut("@annotation(com.lanan.common.annotation.DataSource)"+ "|| @within(com.lanan.common.annotation.DataSource)")public void dsPointCut() {}@Around("dsPointCut()")public Object around(ProceedingJoinPoint point) throws Throwable {DataSource dataSource = getDataSource(point);if (StringUtils.isNotNull(dataSource)) {DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());}try {return point.proceed();} finally {// 銷毀數據源 在執行方法之后DynamicDataSourceContextHolder.clearDataSourceType();}}/*** 獲取需要切換的數據源*/public DataSource getDataSource(ProceedingJoinPoint point) {MethodSignature signature = (MethodSignature) point.getSignature();DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);if (Objects.nonNull(dataSource)) {return dataSource;}return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);} }10、配置完成 使用方法
在實現類上直接使用@DataSource注解
可以在方法上指定使用某個數據源
2、完成
ok!到這里就完成了,啟動頁沒有報錯
碼農界的一個小學生 歡迎哥哥們指點
總結
以上是生活随笔為你收集整理的SpringBoot框架DataSource多数据源配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于梅花雪 MzTreeView2.0
- 下一篇: MATLAB多方法车牌识别系统(bp+模