diff --git a/pom.xml b/pom.xml
index 9acfb63..a68dba9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,6 +16,7 @@
1.8
+ 5.1.47
@@ -45,6 +46,18 @@
test
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.9
+
+
+
+ mysql
+ mysql-connector-java
+ ${mysql-connector.version}
+
+
org.apache.commons
diff --git a/src/main/java/net/educoder/ecsonar/config/DynamicDataSource.java b/src/main/java/net/educoder/ecsonar/config/DynamicDataSource.java
new file mode 100644
index 0000000..f944231
--- /dev/null
+++ b/src/main/java/net/educoder/ecsonar/config/DynamicDataSource.java
@@ -0,0 +1,15 @@
+package net.educoder.ecsonar.config;
+
+import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
+
+/**
+ * @Author: youys
+ * @Date: 2021/12/31
+ * @Description:
+ */
+public class DynamicDataSource extends AbstractRoutingDataSource {
+ @Override
+ protected Object determineCurrentLookupKey() {
+ return DynamicDataSourceContextHolder.getContextKey();
+ }
+}
diff --git a/src/main/java/net/educoder/ecsonar/config/DynamicDataSourceConfig.java b/src/main/java/net/educoder/ecsonar/config/DynamicDataSourceConfig.java
new file mode 100644
index 0000000..d8bb9cf
--- /dev/null
+++ b/src/main/java/net/educoder/ecsonar/config/DynamicDataSourceConfig.java
@@ -0,0 +1,97 @@
+package net.educoder.ecsonar.config;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+import javax.sql.DataSource;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Author: youys
+ * @Date: 2021/12/31
+ * @Description:
+ */
+
+@Configuration
+@MapperScan(basePackages = "net.educoder.ecsonar.dao")
+public class DynamicDataSourceConfig {
+
+ public static final String MASTER = "master";
+ public static final String READONLY = "readonly";
+
+ private static final List INIT_SQLS= Arrays.asList("SET NAMES utf8mb4");
+
+ @Value("${spring.datasource.master.driverClassName}")
+ private String masterDriverClass;
+
+ @Value("${spring.datasource.master.url}")
+ private String masterUrl;
+
+ @Value("${spring.datasource.master.username}")
+ private String masterUsername;
+
+ @Value("${spring.datasource.master.password}")
+ private String masterPassword;
+
+ @Value("${spring.datasource.readonly.driverClassName}")
+ private String readonlyDriverClass;
+
+ @Value("${spring.datasource.readonly.url}")
+ private String readonlyUrl;
+
+ @Value("${spring.datasource.readonly.username}")
+ private String readonlyUsername;
+
+ @Value("${spring.datasource.readonly.password}")
+ private String readonlyPassword;
+
+ @Value("${spring.datasource.initSize}")
+ private Integer initSize;
+
+ @Bean("master")
+ public DataSource masterDataSource() {
+ DruidDataSource masterDataSource = new DruidDataSource();
+ masterDataSource.setDriverClassName(masterDriverClass);
+ masterDataSource.setUrl(masterUrl);
+ masterDataSource.setUsername(masterUsername);
+ masterDataSource.setPassword(masterPassword);
+ masterDataSource.setInitialSize(initSize);
+ masterDataSource.setConnectionInitSqls(INIT_SQLS);
+ return masterDataSource;
+ }
+
+ @Bean("readonly")
+ public DataSource slaveDataSource() {
+ DruidDataSource readonlyDataSource = new DruidDataSource();
+ readonlyDataSource.setDriverClassName(readonlyDriverClass);
+ readonlyDataSource.setUrl(readonlyUrl);
+ readonlyDataSource.setUsername(readonlyUsername);
+ readonlyDataSource.setPassword(readonlyPassword);
+ readonlyDataSource.setInitialSize(initSize);
+ readonlyDataSource.setConnectionInitSqls(INIT_SQLS);
+ return readonlyDataSource;
+ }
+
+
+ @Bean
+ @Primary
+ public DataSource dynamicDataSource() {
+ Map