|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|