Explorar el Código

数据原切换

LLL hace 1 año
padre
commit
5a2c0eb257

+ 40 - 10
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/aspect/TargetDataSourceAspect.java

@@ -30,34 +30,64 @@ public class TargetDataSourceAspect {
     @Before("dsPointCut()")
     public void around(JoinPoint point) throws Throwable {
 
+        DataSourceManagement.flag.set("MASTER");
+        Integer type = switchDataSourceService.getType();
+
         TargetDataSource annotation = null;
         Class<? extends Object> target = point.getTarget().getClass();
         if(target.isAnnotationPresent(TargetDataSource.class)){
             // 判断类上是否标注着注解
             annotation = target.getAnnotation(TargetDataSource.class);
             log.info("类上标注了注解");
+
+            if ("SLAVE".equals(annotation.value().name())){
+                if(type != null && type == 1){
+                    DataSourceManagement.flag.set(annotation.value().name());
+                    System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
+                }
+            }
+            System.out.println(annotation.value().name()+type);
         }else{
             Method method = ((MethodSignature) point.getSignature()).getMethod();
             if(method.isAnnotationPresent(TargetDataSource.class)){
                 // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
                 annotation = method.getAnnotation(TargetDataSource.class);
                 log.info("方法上标注了注解");
+
+                if ("SLAVE".equals(annotation.value().name())){
+                    if(type != null && type == 1){
+                        DataSourceManagement.flag.set(annotation.value().name());
+                        System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
+                    }
+                }
+                System.out.println(annotation.value().name()+type);
             }else{
                 throw new RuntimeException("@TargetDataSource注解只能用于类或者方法上, 错误出现在:[" +
                         target.toString() +" " + method.toString() + "];");
             }
         }
 
-        // 切换数据源 0主数据源,1其他数据源
-        SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
-        Integer type = Integer.valueOf(switchDataSource.getType());
-        if (type != null && type == 0) {
-            DataSourceManagement.flag.set("MASTER");
-            System.out.println("==========================="+type+"MASTER========================");
-        } else if (type != null && type == 1) {
-            DataSourceManagement.flag.set(annotation.value().name());
-            System.out.println("==========================="+type+annotation.value().name()+"========================");
-        }
+
+
+//        if ("SLAVE".equals(annotation.value().name())){
+//            if(type != null && type == 1){
+//                DataSourceManagement.flag.set(annotation.value().name());
+//                System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
+//            }
+//        }
+//        System.out.println(annotation.value().name()+type);
+
+//        // 切换数据源 0主数据源,1其他数据源
+//        Integer type = switchDataSourceService.getType();
+////        SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
+////        Integer type = Integer.valueOf(switchDataSource.getType());
+//        if (type != null && type == 0) {
+//            DataSourceManagement.flag.set("MASTER");
+//            System.out.println("type == 0==========================="+type+"MASTER========================");
+//        } else if (type != null && type == 1) {
+//            DataSourceManagement.flag.set(annotation.value().name());
+//            System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
+//        }
     }
 
 }

+ 5 - 4
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/mapper/SwitchDataSourceMapper.java

@@ -1,10 +1,8 @@
 package org.jeecg.modules.dataSourceSwitch.mapper;
 
-import java.util.List;
-
-import org.apache.ibatis.annotations.Param;
-import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Select;
+import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
 
 /**
  * @Description: switch_data_source
@@ -14,4 +12,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  */
 public interface SwitchDataSourceMapper extends BaseMapper<SwitchDataSource> {
 
+    @Select("select type from switch_data_source where id =1")
+    public Integer getType();
+
 }

+ 2 - 0
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/service/ISwitchDataSourceService.java

@@ -11,4 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface ISwitchDataSourceService extends IService<SwitchDataSource> {
 
+    public Integer getType();
+
 }

+ 11 - 2
module_kzks/src/main/java/org/jeecg/modules/dataSourceSwitch/service/impl/SwitchDataSourceServiceImpl.java

@@ -1,19 +1,28 @@
 package org.jeecg.modules.dataSourceSwitch.service.impl;
 
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
 import org.jeecg.modules.dataSourceSwitch.mapper.SwitchDataSourceMapper;
 import org.jeecg.modules.dataSourceSwitch.service.ISwitchDataSourceService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-
 /**
  * @Description: switch_data_source
  * @Author: jeecg-boot
  * @Date:   2023-08-27
  * @Version: V1.0
  */
+
 @Service
 public class SwitchDataSourceServiceImpl extends ServiceImpl<SwitchDataSourceMapper, SwitchDataSource> implements ISwitchDataSourceService {
 
+    @Autowired
+    @SuppressWarnings("all")
+    private SwitchDataSourceMapper switchDataSourceMapper;
+
+    public Integer getType(){
+        return switchDataSourceMapper.getType();
+    }
+
 }