Browse Source

多数据源智能切换ok,但只能从service调用(正在解决dao调用问题)

NorthLan 7 years ago
parent
commit
b7951e13a3
1 changed files with 72 additions and 0 deletions
  1. 72 0
      zen-admin/src/main/kotlin/com/gxzc/zen/A.java

+ 72 - 0
zen-admin/src/main/kotlin/com/gxzc/zen/A.java

@@ -0,0 +1,72 @@
+package com.gxzc.zen;
+
+import com.gxzc.zen.orm.DynamicDataSource;
+import com.gxzc.zen.orm.DynamicMultipleDataSource;
+import kotlin.TypeCastException;
+import kotlin.jvm.internal.Intrinsics;
+import org.apache.ibatis.binding.MapperProxy;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.aspectj.lang.reflect.SourceLocation;
+import org.jetbrains.annotations.NotNull;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Objects;
+
+@Aspect
+@Order(-1)
+@Component
+public class A {
+
+    @Before("execution(* com.gxzc.zen.api.sys..*Service.*(..))")
+    public void sysService(@NotNull JoinPoint joinPoint) {
+        this.setDataSource(joinPoint, "sysDataSource");
+    }
+
+    @Before("execution(* com.baomidou.mybatisplus..*Service.*(..)) || execution(* com.baomidou.mybatisplus..*Mapper.*(..))")
+    public void dataSourceSys2(@NotNull JoinPoint joinPoint) {
+        SourceLocation a = joinPoint.getSourceLocation();
+        Class b = a.getWithinType();
+        String k = joinPoint.getKind();
+        Object o = joinPoint.getTarget();
+        Class cs = o.getClass();
+        JoinPoint.StaticPart s = joinPoint.getStaticPart();
+        String packageName = joinPoint.getTarget().getClass().getPackage().getName();
+        if (packageName.contains("com.gxzc.zen.api.sys")) {
+            sysService(joinPoint);
+        } else if (packageName.contains("com.gxzc.zen.api.rec")) {
+            recService(joinPoint);
+        }
+    }
+
+    @Before("execution(* com.gxzc.zen.api.rec..*Service.*(..))")
+    public void recService(@NotNull JoinPoint joinPoint) {
+        this.setDataSource(joinPoint, "recDataSource");
+    }
+
+    private void setDataSource(JoinPoint joinPoint, String defaultKey) {
+        DynamicDataSource dynamicDataSource = this.currentMethod(joinPoint).getAnnotation(DynamicDataSource.class);
+        if (Objects.isNull(dynamicDataSource)) {
+            DynamicMultipleDataSource.Companion.setDataSource(defaultKey);
+        } else {
+            DynamicMultipleDataSource.Companion.setDataSource(dynamicDataSource.value());
+        }
+    }
+
+    private Method currentMethod(JoinPoint joinPoint) {
+        Signature var10000 = joinPoint.getSignature();
+        if (var10000 == null) {
+            throw new TypeCastException("null cannot be cast to non-null type org.aspectj.lang.reflect.MethodSignature");
+        } else {
+            Method var2 = ((MethodSignature) var10000).getMethod();
+            Intrinsics.checkExpressionValueIsNotNull(var2, "(joinPoint.signature as MethodSignature).method");
+            return var2;
+        }
+    }
+}