From 84a737c5959d167f839b7d510ef52fdf248ab48a Mon Sep 17 00:00:00 2001 From: duLingLing Date: Thu, 8 Aug 2019 17:47:47 +0800 Subject: [PATCH] =?UTF-8?q?Upd:=E4=BF=AE=E5=A4=8D=E5=A4=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=E5=88=87=E6=8D=A2=E6=97=A0=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../datasource/DataSourceConfiguration.java | 23 ++++++- .../pos/datasource/MybatisConfiguration.java | 63 ------------------- 3 files changed, 21 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java diff --git a/pom.xml b/pom.xml index 56b69b425..baf790dc1 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 manage - 1.0.40 + 1.1.0 UTF-8 diff --git a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java index f50c714a1..d5abe9fb9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java +++ b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java @@ -6,9 +6,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; /** * 多数据源配置类 @@ -21,15 +24,29 @@ public class DataSourceConfiguration { private Class dataSourceType; @Bean(name = "masterDataSource") - @Primary @ConfigurationProperties(prefix = "spring.datasource.master") - public DataSource masterDataSource(){ + public DataSource masterDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") - public DataSource slaveDataSource1(){ + public DataSource slaveDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); } + + + @Bean("routingDataSource") + @Primary + public AbstractRoutingDataSource roundRobinDataSouceProxy() { + ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource(); + Map targetDataResources = new HashMap<>(); + DataSource masterDataSource = masterDataSource(); + targetDataResources.put(DbContextHolder.DbType.MASTER, masterDataSource); + DataSource slaveDataSource = slaveDataSource(); + targetDataResources.put(DbContextHolder.DbType.SLAVE, slaveDataSource); + proxy.setDefaultTargetDataSource(masterDataSource);//默认源 + proxy.setTargetDataSources(targetDataResources); + return proxy; + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java deleted file mode 100644 index b329a44b4..000000000 --- a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java +++ /dev/null @@ -1,63 +0,0 @@ -package au.com.royalpay.payment.manage.pos.datasource; - - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.ibatis.mapping.DatabaseIdProvider; -import org.apache.ibatis.plugin.Interceptor; -import org.apache.ibatis.session.SqlSessionFactory; -import org.aspectj.apache.bcel.util.ClassLoaderRepository; -import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer; -import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; -import org.mybatis.spring.boot.autoconfigure.MybatisProperties; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ResourceLoader; -import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; - -import javax.annotation.Resource; -import javax.sql.DataSource; -import java.util.List; -import java.util.Map; - -/** - * Created by Dulingling on 2019/8/7 - */ -@Configuration -@AutoConfigureAfter({DataSourceConfiguration.class}) -public class MybatisConfiguration extends MybatisAutoConfiguration { - - - private static Log logger = LogFactory.getLog(MybatisConfiguration.class); - - @Resource(name = "masterDataSource") - private DataSource masterDataSource; - @Resource(name = "slaveDataSource") - private DataSource slaveDataSource; - - public MybatisConfiguration(MybatisProperties properties, ObjectProvider interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider databaseIdProvider, ObjectProvider> configurationCustomizersProvider) { - super(properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider); - } - - @Bean - public SqlSessionFactory sqlSessionFactory() throws Exception { - return super.sqlSessionFactory(roundRobinDataSouceProxy()); - } - - public AbstractRoutingDataSource roundRobinDataSouceProxy(){ - ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource(); - Map targetDataResources = new ClassLoaderRepository.SoftHashMap(); - targetDataResources.put(DbContextHolder.DbType.MASTER,masterDataSource); - targetDataResources.put(DbContextHolder.DbType.SLAVE,slaveDataSource); - proxy.setDefaultTargetDataSource(masterDataSource);//默认源 - proxy.setTargetDataSources(targetDataResources); - return proxy; - } - - public void test(){ - - } -} -