|
@@ -1,13 +1,28 @@
|
|
|
package com.xiesx.fastboot.core.jpa.cfg;
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
|
+import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
|
|
|
+import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
|
|
|
+import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
|
|
+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 org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
import com.querydsl.jpa.impl.JPAQueryFactory;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* @title TokenCfg.java
|
|
|
* @description 令牌认证
|
|
@@ -22,4 +37,37 @@ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|