Administrator 2 anos atrás
pai
commit
5265c3ffd9

+ 25 - 0
src/main/java/com/xiesx/fastboot/core/jpa/annotation/EnableJpaPlusDataSourceRepositories.java

@@ -0,0 +1,25 @@
+package com.xiesx.fastboot.core.jpa.annotation;
+
+import com.xiesx.fastboot.core.jpa.cfg.DataSourceConfig;
+import com.xiesx.fastboot.core.jpa.cfg.JpaPlusCfg;
+import com.xiesx.fastboot.core.jpa.factory.JpaPlusRepositoryFactoryBean;
+import org.springframework.context.annotation.Import;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @title EnableJpaPlusRepositories.java
+ * @description
+ * @author Sixian.xie
+ * @date 2020-7-21 22:32:26
+ */
+@EnableJpaRepositories(repositoryFactoryBeanClass = JpaPlusRepositoryFactoryBean.class)
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Import(DataSourceConfig.class)
+public @interface EnableJpaPlusDataSourceRepositories {
+}

+ 1 - 1
src/main/java/com/xiesx/fastboot/core/jpa/annotation/TargetDataSource.java

@@ -7,7 +7,7 @@ import java.lang.annotation.*;
  * @Author: lsw
  * @Date : 2022/3/14 18:27
  */
-@Target({ElementType.METHOD, ElementType.TYPE})
+@Target({ElementType.METHOD})
 @Retention(value = RetentionPolicy.RUNTIME)
 @Documented
 public @interface TargetDataSource {

+ 63 - 0
src/main/java/com/xiesx/fastboot/core/jpa/cfg/DataSourceConfig.java

@@ -0,0 +1,63 @@
+package com.xiesx.fastboot.core.jpa.cfg;
+
+import com.querydsl.jpa.impl.JPAQueryFactory;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+import javax.persistence.EntityManager;
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @title TokenCfg.java
+ * @description 令牌认证
+ * @author Sixian.xie
+ * @date 2020-7-21 22:36:02
+ */
+@Configuration
+public class DataSourceConfig {
+
+    @Bean
+    public JPAQueryFactory jpaQuery(EntityManager entityManager) {
+        return new JPAQueryFactory(entityManager);
+    }
+
+    @Bean(name = "masterDataSource")
+    @ConfigurationProperties(prefix = "spring.datasource.write")
+    public DataSource masterDataSource() {
+        return DataSourceBuilder.create().build();
+    }
+
+    @Bean(name = "slaveDataSource")
+    @ConfigurationProperties(prefix = "spring.datasource.read")
+    public DataSource slaveDataSource() {
+        return DataSourceBuilder.create().build();
+    }
+
+    @Primary
+    @Bean
+    public DynamicRoutingDataSource dynamicDataSource(
+            @Qualifier(value = "masterDataSource") DataSource masterDataSource,
+            @Qualifier(value = "slaveDataSource") DataSource slaveDataSource) {
+        Map<Object, Object> targetDataSources = new HashMap<>(2);
+        targetDataSources.put(DataSourceEnum.master, masterDataSource);
+        targetDataSources.put(DataSourceEnum.slave, slaveDataSource);
+        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
+        //设置数据源
+        dynamicRoutingDataSource.setTargetDataSources(targetDataSources);
+        //设置默认选择的数据源
+        dynamicRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
+        dynamicRoutingDataSource.afterPropertiesSet();
+        return dynamicRoutingDataSource;
+    }
+
+
+
+
+
+}

+ 3 - 1
src/main/java/com/xiesx/fastboot/core/jpa/cfg/DynamicRoutingDataSource.java

@@ -1,5 +1,6 @@
 package com.xiesx.fastboot.core.jpa.cfg;
 
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
 /**
@@ -7,11 +8,12 @@ import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  * @Author: lsw
  * @Date : 2022/3/14 18:25
  */
+@Slf4j
 public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
     @Override
     protected Object determineCurrentLookupKey() {
         Object lookupKey = DynamicRoutingDataSource.getRoutingDataSource();
-        System.err.println(Thread.currentThread().getName() + " determineCurrentLookupKey : " + lookupKey);
+        log.info(lookupKey.toString());
         return lookupKey;
     }
 

+ 0 - 29
src/main/java/com/xiesx/fastboot/core/jpa/cfg/JpaPlusCfg.java

@@ -37,35 +37,6 @@ public class JpaPlusCfg{
         return new JPAQueryFactory(entityManager);
     }
 
-    @Bean(name = "masterDataSource")
-    @ConfigurationProperties(prefix = "spring.datasource.write")
-    public DataSource masterDataSource() {
-        return DataSourceBuilder.create().build();
-    }
-
-    @Bean(name = "slaveDataSource")
-    @ConfigurationProperties(prefix = "spring.datasource.read")
-    public DataSource slaveDataSource() {
-        return DataSourceBuilder.create().build();
-    }
-
-    @Primary
-    @Bean
-    public DynamicRoutingDataSource dynamicDataSource(
-            @Qualifier(value = "masterDataSource") DataSource masterDataSource,
-            @Qualifier(value = "slaveDataSource") DataSource slaveDataSource) {
-        Map<Object, Object> targetDataSources = new HashMap<>(2);
-        targetDataSources.put(DataSourceEnum.master, masterDataSource);
-        targetDataSources.put(DataSourceEnum.slave, slaveDataSource);
-        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
-        //设置数据源
-        dynamicRoutingDataSource.setTargetDataSources(targetDataSources);
-        //设置默认选择的数据源
-        dynamicRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
-        dynamicRoutingDataSource.afterPropertiesSet();
-        return dynamicRoutingDataSource;
-    }
-