From a93805b09f52dabf05d6f384ee90a5aced1fd339 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Thu, 5 Sep 2019 17:04:51 +0800 Subject: [PATCH] =?UTF-8?q?:bulb:=20=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringBootDemoDynamicDatasourceApplication.java | 3 +++ .../datasource/annotation/DefaultDatasource.java | 2 +- .../datasource/aspect/DatasourceSelectorAspect.java | 6 +++++- .../datasource/config/MybatisConfiguration.java | 2 +- .../datasource/datasource/DynamicDataSource.java | 10 +++++++++- .../xkcoding/dynamic/datasource/mapper/UserMapper.java | 2 ++ 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/SpringBootDemoDynamicDatasourceApplication.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/SpringBootDemoDynamicDatasourceApplication.java index b9253f7..7a84a96 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/SpringBootDemoDynamicDatasourceApplication.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/SpringBootDemoDynamicDatasourceApplication.java @@ -31,9 +31,12 @@ public class SpringBootDemoDynamicDatasourceApplication implements CommandLineRu @Override public void run(String... args) { + // 设置默认的数据源 DatasourceConfigContextHolder.setDefaultDatasource(); + // 查询所有数据库配置列表 List datasourceConfigs = configMapper.selectAll(); System.out.println("加载其余数据源配置列表: " + datasourceConfigs); + // 将数据库配置加入缓存 datasourceConfigs.forEach(config -> DatasourceConfigCache.INSTANCE.addConfig(config.getId(), config)); } } diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/annotation/DefaultDatasource.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/annotation/DefaultDatasource.java index 6d9602e..533b20a 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/annotation/DefaultDatasource.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/annotation/DefaultDatasource.java @@ -4,7 +4,7 @@ import java.lang.annotation.*; /** *

- * 默认数据源 + * 用户标识仅可以使用默认数据源 *

* * @author yangkai.shen diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/aspect/DatasourceSelectorAspect.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/aspect/DatasourceSelectorAspect.java index fbc4330..13da691 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/aspect/DatasourceSelectorAspect.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/aspect/DatasourceSelectorAspect.java @@ -37,7 +37,7 @@ public class DatasourceSelectorAspect { } /** - * 前置通知 用于拦截操作 + * 前置操作,拦截具体请求,获取header里的数据源id,设置线程变量里,用于后续切换数据源 */ @Before("datasourcePointcut()") public void doBefore(JoinPoint joinPoint) { @@ -45,6 +45,7 @@ public class DatasourceSelectorAspect { MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); + // 排除不可切换数据源的方法 DefaultDatasource annotation = method.getAnnotation(DefaultDatasource.class); if (null != annotation) { DatasourceConfigContextHolder.setDefaultDatasource(); @@ -62,6 +63,9 @@ public class DatasourceSelectorAspect { } } + /** + * 后置操作,设置回默认的数据源id + */ @AfterReturning("datasourcePointcut()") public void doAfter() { DatasourceConfigContextHolder.setDefaultDatasource(); diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/config/MybatisConfiguration.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/config/MybatisConfiguration.java index 30d4945..4747ac7 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/config/MybatisConfiguration.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/config/MybatisConfiguration.java @@ -19,7 +19,7 @@ import javax.sql.DataSource; * @date Created in 2019/9/4 16:20 */ @Configuration -@MapperScan(basePackages = "com.xkcoding.dynamicdatasource.mapper", sqlSessionFactoryRef = "sqlSessionFactory") +@MapperScan(basePackages = "com.xkcoding.dynamic.datasource.mapper", sqlSessionFactoryRef = "sqlSessionFactory") public class MybatisConfiguration { /** * 创建会话工厂。 diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/datasource/DynamicDataSource.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/datasource/DynamicDataSource.java index 4b1b2ce..c32bfdc 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/datasource/DynamicDataSource.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/datasource/DynamicDataSource.java @@ -33,17 +33,24 @@ public class DynamicDataSource extends HikariDataSource { return datasource.getConnection(); } + /** + * 初始化数据源 + * @param id 数据源id + * @return 数据源 + */ private HikariDataSource initDatasource(Long id) { HikariDataSource dataSource = new HikariDataSource(); + // 判断是否是默认数据源 if (DatasourceHolder.DEFAULT_ID.equals(id)) { + // 默认数据源根据 application.yml 配置的生成 DataSourceProperties properties = SpringUtil.getBean(DataSourceProperties.class); dataSource.setJdbcUrl(properties.getUrl()); dataSource.setUsername(properties.getUsername()); dataSource.setPassword(properties.getPassword()); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); } else { - // 获取数据库的配置 + // 不是默认数据源,通过缓存获取对应id的数据源的配置 DatasourceConfig datasourceConfig = DatasourceConfigCache.INSTANCE.getConfig(id); if (datasourceConfig == null) { @@ -55,6 +62,7 @@ public class DynamicDataSource extends HikariDataSource { dataSource.setPassword(datasourceConfig.getPassword()); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); } + // 将创建的数据源添加到数据源管理器中,绑定当前线程 DatasourceHolder.INSTANCE.addDatasource(id, dataSource); return dataSource; } diff --git a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/mapper/UserMapper.java b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/mapper/UserMapper.java index 0824d39..02ac04b 100644 --- a/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/mapper/UserMapper.java +++ b/spring-boot-demo-dynamic-datasource/src/main/java/com/xkcoding/dynamic/datasource/mapper/UserMapper.java @@ -2,6 +2,7 @@ package com.xkcoding.dynamic.datasource.mapper; import com.xkcoding.dynamic.datasource.config.MyMapper; import com.xkcoding.dynamic.datasource.model.User; +import org.apache.ibatis.annotations.Mapper; /** *

@@ -11,5 +12,6 @@ import com.xkcoding.dynamic.datasource.model.User; * @author yangkai.shen * @date Created in 2019/9/4 16:49 */ +@Mapper public interface UserMapper extends MyMapper { }