From 475b0ecb3ad3dc18704b613ce9b64219f66105d6 Mon Sep 17 00:00:00 2001 From: chxlay Date: Mon, 9 Dec 2024 14:56:01 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AF=B9=E5=8A=A8=E6=80=81=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90@UseDataSource=E7=9A=84value=E5=80=BC?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=8A=A8=E6=80=81=E8=A7=A3=E6=9E=90=E5=8C=BA?= =?UTF-8?q?=E5=88=86Spring=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=92=8C=E9=9D=9ESpr?= =?UTF-8?q?ing=E6=A8=A1=E5=BC=8F=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/datasource/DataSourceKey.java | 4 ++-- .../core/datasource/DataSourceManager.java | 4 ++-- .../datasource/processor/DataSourceProcessor.java | 6 +++--- .../core/mybatis/binding/FlexMapperProxy.java | 10 +++++----- .../spring/datasource/DataSourceInterceptor.java | 13 +++++++++---- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java index a7f43231..a753ce11 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java @@ -78,8 +78,8 @@ public class DataSourceKey { lookup = threadLocal; } - public static String processDataSourceKey(String dataSourceKey, Object mapper, Method method, Object[] arguments) { - String dsKey = DataSourceManager.processDataSourceKey(dataSourceKey, mapper, method, arguments); + public static String processDataSourceKey(String dataSourceKey, Object targetOrProxy, Method method, Object[] arguments) { + String dsKey = DataSourceManager.processDataSourceKey(dataSourceKey, targetOrProxy, method, arguments); return dsKey != null ? dsKey : dataSourceKey; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java index ce68caba..79d962bd 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java @@ -109,9 +109,9 @@ public class DataSourceManager { return null; } - static String processDataSourceKey(String dataSourceKey, Object mapper, Method method, Object[] arguments) { + static String processDataSourceKey(String dataSourceKey, Object targetOrProxy, Method method, Object[] arguments) { // 如果没有配置 DataSourceProcessor 实例,则不做处理,返回原始值 - return dataSourceProcessor == null ? dataSourceKey : dataSourceProcessor.process(dataSourceKey, mapper, method, arguments); + return dataSourceProcessor == null ? dataSourceKey : dataSourceProcessor.process(dataSourceKey, targetOrProxy, method, arguments); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/processor/DataSourceProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/processor/DataSourceProcessor.java index 59f4c5b9..a0737b0a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/processor/DataSourceProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/processor/DataSourceProcessor.java @@ -16,12 +16,12 @@ public interface DataSourceProcessor { /** * 数据源key解析扩展 * - * @param dataSourceKey 注解UseDataSource的value 值,调用process时不会为null,不会为空{@link FlexMapperProxy#invoke(Object, Method, Object[])} - * @param mapper Mapper对象(代理对象) + * @param dataSourceKey 注解UseDataSource的value 值,调用process时不会为null,可能为空字符{@link FlexMapperProxy#invoke(Object, Method, Object[])}And{@link com.mybatisflex.spring.datasource.DataSourceInterceptor#getDataSourceKey(Object, Method, Object[])} + * @param targetOrProxy AOP对象this或Mapper代理对象(当注解@UseDataSource使用到Mapper上时为proxy) * @param method Mapper当前执行的方法函数 * @param arguments Mapper当前执行的函数参数 * @return 数据源名称(可能为null 为 null 时表示不符合当前处理器的处理) */ - String process(String dataSourceKey, Object mapper, Method method, Object[] arguments); + String process(String dataSourceKey, Object targetOrProxy, Method method, Object[] arguments); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/binding/FlexMapperProxy.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/binding/FlexMapperProxy.java index bff2c044..05e13556 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/binding/FlexMapperProxy.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/binding/FlexMapperProxy.java @@ -59,12 +59,12 @@ public class FlexMapperProxy extends MybatisMapperProxy { try { if (StringUtil.noText(finalDsKey)) { + // Mapper 方法上获取 UseDataSource的value值 finalDsKey = getMethodDsKey(method, proxy); - } - - // 对数据源取值进行动态取值处理 - if (!StrUtil.isBlank(finalDsKey)) { - finalDsKey = DataSourceKey.processDataSourceKey(finalDsKey, proxy, method, args); + // 对数据源取值进行动态取值处理 + if (!StrUtil.isBlank(finalDsKey)) { + finalDsKey = DataSourceKey.processDataSourceKey(finalDsKey, proxy, method, args); + } } // 通过自定义分配策略去获取最终的数据源 diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/datasource/DataSourceInterceptor.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/datasource/DataSourceInterceptor.java index e25d4d4b..2e72d532 100644 --- a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/datasource/DataSourceInterceptor.java +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/datasource/DataSourceInterceptor.java @@ -19,6 +19,7 @@ package com.mybatisflex.spring.datasource; import com.mybatisflex.annotation.UseDataSource; import com.mybatisflex.core.datasource.DataSourceKey; import com.mybatisflex.core.util.StringUtil; +import com.mybatisflex.processor.util.StrUtil; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.core.MethodClassKey; @@ -44,7 +45,7 @@ public class DataSourceInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { - String dsKey = getDataSourceKey(invocation.getMethod(), invocation.getThis().getClass()); + String dsKey = getDataSourceKey(invocation.getThis(), invocation.getMethod(), invocation.getArguments()); if (StringUtil.noText(dsKey)) { return invocation.proceed(); } @@ -56,11 +57,15 @@ public class DataSourceInterceptor implements MethodInterceptor { } } - private String getDataSourceKey(Method method, Class targetClass) { - Object cacheKey = new MethodClassKey(method, targetClass); + private String getDataSourceKey(Object target, Method method, Object[] arguments) { + Object cacheKey = new MethodClassKey(method, target.getClass()); String dsKey = this.dsCache.get(cacheKey); if (dsKey == null) { - dsKey = determineDataSourceKey(method, targetClass); + dsKey = determineDataSourceKey(method, target.getClass()); + // 对数据源取值进行动态取值处理 + if (!StrUtil.isBlank(dsKey)) { + dsKey = DataSourceKey.processDataSourceKey(dsKey, target, method, arguments); + } this.dsCache.put(cacheKey, dsKey); } return dsKey;