Browse Source

数据源切换

LLL 1 year ago
parent
commit
c37ac0498c

+ 20 - 26
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/aspect/TargetDataSourceAspect.java

@@ -2,14 +2,10 @@ package org.jeecg.modules.dataSourceSwitch.aspect;
 
 
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.aspectj.lang.reflect.MethodSignature;
-import org.jeecg.modules.customColumns.entity.CustomColumns;
-import org.jeecg.modules.customColumns.service.ICustomColumnsService;
 import org.jeecg.modules.dataSourceSwitch.annotation.TargetDataSource;
 import org.jeecg.modules.dataSourceSwitch.annotation.TargetDataSource;
 import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
 import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
 import org.jeecg.modules.dataSourceSwitch.manage.DataSourceManagement;
 import org.jeecg.modules.dataSourceSwitch.manage.DataSourceManagement;
@@ -18,8 +14,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.Map;
 
 
 @Component
 @Component
 @Aspect
 @Aspect
@@ -36,34 +30,34 @@ public class TargetDataSourceAspect {
     @Before("dsPointCut()")
     @Before("dsPointCut()")
     public void around(JoinPoint point) throws Throwable {
     public void around(JoinPoint point) throws Throwable {
 
 
-//        TargetDataSource annotation = null;
-//        Class<? extends Object> target = point.getTarget().getClass();
-//        if(target.isAnnotationPresent(TargetDataSource.class)){
-//            // 判断类上是否标注着注解
-//            annotation = target.getAnnotation(TargetDataSource.class);
-//            log.info("类上标注了注解");
-//        }else{
-//            Method method = ((MethodSignature) point.getSignature()).getMethod();
-//            if(method.isAnnotationPresent(TargetDataSource.class)){
-//                // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
-//                annotation = method.getAnnotation(TargetDataSource.class);
-//                log.info("方法上标注了注解");
-//            }else{
-//                throw new RuntimeException("@TargetDataSource注解只能用于类或者方法上, 错误出现在:[" +
-//                        target.toString() +" " + method.toString() + "];");
-//            }
-//        }
+        TargetDataSource annotation = null;
+        Class<? extends Object> target = point.getTarget().getClass();
+        if(target.isAnnotationPresent(TargetDataSource.class)){
+            // 判断类上是否标注着注解
+            annotation = target.getAnnotation(TargetDataSource.class);
+            log.info("类上标注了注解");
+        }else{
+            Method method = ((MethodSignature) point.getSignature()).getMethod();
+            if(method.isAnnotationPresent(TargetDataSource.class)){
+                // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
+                annotation = method.getAnnotation(TargetDataSource.class);
+                log.info("方法上标注了注解");
+            }else{
+                throw new RuntimeException("@TargetDataSource注解只能用于类或者方法上, 错误出现在:[" +
+                        target.toString() +" " + method.toString() + "];");
+            }
+        }
 
 
         // 切换数据源 0主数据源,1其他数据源
         // 切换数据源 0主数据源,1其他数据源
         SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
         SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
         Integer type = Integer.valueOf(switchDataSource.getType());
         Integer type = Integer.valueOf(switchDataSource.getType());
-        System.out.println("==========================="+type+"========================");
         if (type != null && type == 0) {
         if (type != null && type == 0) {
             DataSourceManagement.flag.set("MASTER");
             DataSourceManagement.flag.set("MASTER");
+            System.out.println("==========================="+type+"MASTER========================");
         } else if (type != null && type == 1) {
         } else if (type != null && type == 1) {
-            DataSourceManagement.flag.set("SLAVE");
+            DataSourceManagement.flag.set(annotation.value().name());
+            System.out.println("==========================="+type+annotation.value().name()+"========================");
         }
         }
-//        DataSourceManagement.flag.set(annotation.value().name());
     }
     }
 
 
 }
 }

+ 3 - 2
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/controller/SwitchDataSourceController.java

@@ -48,6 +48,7 @@ import org.jeecg.common.aspect.annotation.AutoLog;
 @Slf4j
 @Slf4j
 public class SwitchDataSourceController extends JeecgController<SwitchDataSource, ISwitchDataSourceService> {
 public class SwitchDataSourceController extends JeecgController<SwitchDataSource, ISwitchDataSourceService> {
 	@Autowired
 	@Autowired
+	@SuppressWarnings("all")
 	private ISwitchDataSourceService switchDataSourceService;
 	private ISwitchDataSourceService switchDataSourceService;
 
 
 	 /**
 	 /**
@@ -55,12 +56,12 @@ public class SwitchDataSourceController extends JeecgController<SwitchDataSource
 	  */
 	  */
 	 @ApiOperation(value="数据源切换")
 	 @ApiOperation(value="数据源切换")
 	 @GetMapping(value = "/edit1")
 	 @GetMapping(value = "/edit1")
-	 public Result<String> edit1(Integer type) {
+	 public Integer edit1(Integer type) {
 		 SwitchDataSource switchDataSource = new SwitchDataSource();
 		 SwitchDataSource switchDataSource = new SwitchDataSource();
 		 switchDataSource.setId(1);
 		 switchDataSource.setId(1);
 		 switchDataSource.setType(type);
 		 switchDataSource.setType(type);
 		 switchDataSourceService.updateById(switchDataSource);
 		 switchDataSourceService.updateById(switchDataSource);
-		 return Result.OK("编辑成功!");
+		 return type;
 	 }
 	 }
 
 
 	/**
 	/**