chenkq před 3 týdny
rodič
revize
cc3f340bac

+ 4 - 4
java/storlead-api/src/main/resources/application-dev.yml

@@ -95,10 +95,10 @@ spring:
           password: rCgRgLjH99Xvg5BN
           driver-class-name: com.mysql.jdbc.Driver
         trade:
-            url: jdbc:mysql://mysql.test.storlead.com:39091/sp_trade_platform_test?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
-            username: root
-            password: rCgRgLjH99Xvg5BN
-            driver-class-name: com.mysql.jdbc.Driver
+          url: jdbc:mysql://mysql.test.storlead.com:39091/sp_trade_platform_test?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
+          username: root
+          password: rCgRgLjH99Xvg5BN
+          driver-class-name: com.mysql.jdbc.Driver
         sales:
           driver-class-name: com.mysql.jdbc.Driver
           url: jdbc:mysql://mysql.test.storlead.com:39091/sp_sales_test?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true

+ 36 - 11
java/storlead-framework/storlead-mybatis/src/main/java/com/storlead/framework/mybatis/datasource/ModuleDefaultDataSourceAspect.java

@@ -17,7 +17,10 @@ import org.springframework.util.StringUtils;
 import java.lang.reflect.Method;
 
 /**
- * 子模块默认数据源:按配置包前缀 push 数据源;若目标已标注 {@link DS} 则跳过,由官方 {@code @DS} 切面处理。
+ * 子模块默认数据源:按配置包前缀 push 数据源。
+ * <p>
+ * 若目标已标注 {@link DS},由本切面直接 push 其 value(不再仅“跳过”依赖官方切面,
+ * 避免官方 @DS 未织入时落到 primary/master)。
  */
 @Slf4j
 @Aspect
@@ -37,17 +40,17 @@ public class ModuleDefaultDataSourceAspect {
         if (!properties.isModuleDefaultEnabled()) {
             return joinPoint.proceed();
         }
-        if (hasDsAnnotation(joinPoint)) {
-            return joinPoint.proceed();
+        String datasource = resolveExplicitDs(joinPoint);
+        if (!StringUtils.hasText(datasource)) {
+            datasource = resolveDatasource(joinPoint);
         }
-        String datasource = resolveDatasource(joinPoint);
         if (!StringUtils.hasText(datasource)) {
             return joinPoint.proceed();
         }
         DynamicDataSourceContextHolder.push(datasource);
         try {
             if (log.isDebugEnabled()) {
-                log.debug("module default datasource [{}] -> {}", datasource, joinPoint.getSignature().toShortString());
+                log.debug("module datasource [{}] -> {}", datasource, joinPoint.getSignature().toShortString());
             }
             return joinPoint.proceed();
         } finally {
@@ -55,17 +58,39 @@ public class ModuleDefaultDataSourceAspect {
         }
     }
 
-    private boolean hasDsAnnotation(ProceedingJoinPoint joinPoint) {
+    /**
+     * 方法 / 声明类 / 实际目标类上的 @DS 优先
+     */
+    private String resolveExplicitDs(ProceedingJoinPoint joinPoint) {
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         Method method = signature.getMethod();
+        DS ds = AnnotatedElementUtils.findMergedAnnotation(method, DS.class);
+        if (ds != null && StringUtils.hasText(ds.value())) {
+            return ds.value();
+        }
         Class<?> declaringType = signature.getDeclaringType();
-        if (AnnotatedElementUtils.hasAnnotation(method, DS.class)) {
-            return true;
+        if (declaringType != null) {
+            ds = AnnotatedElementUtils.findMergedAnnotation(declaringType, DS.class);
+            if (ds != null && StringUtils.hasText(ds.value())) {
+                return ds.value();
+            }
         }
-        if (declaringType != null && AnnotatedElementUtils.hasAnnotation(declaringType, DS.class)) {
-            return true;
+        Class<?> targetClass = joinPoint.getTarget() != null ? joinPoint.getTarget().getClass() : null;
+        if (targetClass != null) {
+            ds = AnnotatedElementUtils.findMergedAnnotation(targetClass, DS.class);
+            if (ds != null && StringUtils.hasText(ds.value())) {
+                return ds.value();
+            }
+            // CGLIB 代理取父类
+            Class<?> superclass = targetClass.getSuperclass();
+            if (superclass != null && superclass != Object.class) {
+                ds = AnnotatedElementUtils.findMergedAnnotation(superclass, DS.class);
+                if (ds != null && StringUtils.hasText(ds.value())) {
+                    return ds.value();
+                }
+            }
         }
-        return AnnotatedElementUtils.hasAnnotation(joinPoint.getTarget().getClass(), DS.class);
+        return null;
     }
 
     /**

+ 12 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/mapper/support/TradeBaseMapper.java

@@ -0,0 +1,12 @@
+package com.storlead.trade.mapper.support;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.storlead.framework.common.constant.DSConstants;
+import com.storlead.framework.mybatis.mapper.MyBaseMapper;
+
+/**
+ * trade 库 Mapper 基类,统一路由到 spring.datasource.dynamic.datasource.trade。
+ */
+@DS(DSConstants.DATASOURCE_TRADE)
+public interface TradeBaseMapper<T> extends MyBaseMapper<T> {
+}