From d9992854e6096967a78f293a3e0a07d68f45d259 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Wed, 27 Nov 2024 17:07:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0sqlserver=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ktg-admin/pom.xml | 33 ++++--------------- .../src/main/resources/application-druid.yml | 17 +++++----- ktg-admin/src/main/resources/application.yml | 6 ++-- ktg-admin/src/main/resources/logback.xml | 2 +- .../com/ktg/common/annotation/DataSource.java | 2 +- .../com/ktg/common/enums/DataSourceType.java | 4 +-- .../com/ktg/framework/config/DruidConfig.java | 30 +++++++++++------ .../service/impl/SysConfigServiceImpl.java | 2 +- 8 files changed, 42 insertions(+), 54 deletions(-) diff --git a/ktg-admin/pom.xml b/ktg-admin/pom.xml index dbebbe6..2ec34a2 100644 --- a/ktg-admin/pom.xml +++ b/ktg-admin/pom.xml @@ -103,32 +103,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - com.dm DmJdbcDriver @@ -139,7 +113,12 @@ hibernate5 5.4 - + + + com.microsoft.sqlserver + mssql-jdbc + 12.2.0.jre8 + diff --git a/ktg-admin/src/main/resources/application-druid.yml b/ktg-admin/src/main/resources/application-druid.yml index ac71691..7eec95a 100644 --- a/ktg-admin/src/main/resources/application-druid.yml +++ b/ktg-admin/src/main/resources/application-druid.yml @@ -2,22 +2,23 @@ spring: datasource: type: com.alibaba.druid.pool.DruidDataSource - driverClassName: dm.jdbc.driver.DmDriver druid: # 主库数据源 - master: + dm: + driverClassName: dm.jdbc.driver.DmDriver # url: jdbc:dm://30.30.2.24:5237/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 -# url: jdbc:dm://localhost:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + # url: jdbc:dm://localhost:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:dm://192.168.8.60:5236/WMS_DJ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: SYSDBA password: SYSDBA # 从库数据源 - slave: + sqlserver: # 从数据源开关/默认关闭 - enabled: false - url: - username: - password: + enabled: true + driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver + url: jdbc:sqlserver://192.168.8.60:5236:1433;DatabaseName=DPG_RH_YQ_20240711 + username: sa + password: rh123456 # 初始连接数 initialSize: 5 # 最小连接池数量 diff --git a/ktg-admin/src/main/resources/application.yml b/ktg-admin/src/main/resources/application.yml index cc555d4..736ed7c 100644 --- a/ktg-admin/src/main/resources/application.yml +++ b/ktg-admin/src/main/resources/application.yml @@ -63,15 +63,13 @@ spring: # redis 配置 redis: # 地址 -# host: 192.168.8.60 - host: 127.0.0.1 + host: 192.168.8.60 # 端口,默认为6379 port: 6379 # 数据库索引 database: 4 # 密码 -# password: 123456 - password: + password: 123456 # 连接超时时间 timeout: 10s lettuce: diff --git a/ktg-admin/src/main/resources/logback.xml b/ktg-admin/src/main/resources/logback.xml index fbedcc3..5a8fa21 100644 --- a/ktg-admin/src/main/resources/logback.xml +++ b/ktg-admin/src/main/resources/logback.xml @@ -1,7 +1,7 @@ - + diff --git a/ktg-common/src/main/java/com/ktg/common/annotation/DataSource.java b/ktg-common/src/main/java/com/ktg/common/annotation/DataSource.java index a2a5b09..bd56b9f 100644 --- a/ktg-common/src/main/java/com/ktg/common/annotation/DataSource.java +++ b/ktg-common/src/main/java/com/ktg/common/annotation/DataSource.java @@ -24,5 +24,5 @@ public @interface DataSource /** * 切换数据源名称 */ - public DataSourceType value() default DataSourceType.MASTER; + public DataSourceType value() default DataSourceType.DM; } diff --git a/ktg-common/src/main/java/com/ktg/common/enums/DataSourceType.java b/ktg-common/src/main/java/com/ktg/common/enums/DataSourceType.java index ef3089e..762de84 100644 --- a/ktg-common/src/main/java/com/ktg/common/enums/DataSourceType.java +++ b/ktg-common/src/main/java/com/ktg/common/enums/DataSourceType.java @@ -10,10 +10,10 @@ public enum DataSourceType /** * 主库 */ - MASTER, + DM, /** * 从库 */ - SLAVE + SQLSERVER, } diff --git a/ktg-framework/src/main/java/com/ktg/framework/config/DruidConfig.java b/ktg-framework/src/main/java/com/ktg/framework/config/DruidConfig.java index 7e79ffb..fc5705c 100644 --- a/ktg-framework/src/main/java/com/ktg/framework/config/DruidConfig.java +++ b/ktg-framework/src/main/java/com/ktg/framework/config/DruidConfig.java @@ -9,6 +9,8 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; @@ -33,17 +35,17 @@ import com.ktg.framework.datasource.DynamicDataSource; public class DruidConfig { @Bean - @ConfigurationProperties("spring.datasource.druid.master") - public DataSource masterDataSource(DruidProperties druidProperties) + @ConfigurationProperties("spring.datasource.druid.dm") + public DataSource dmDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @Bean - @ConfigurationProperties("spring.datasource.druid.slave") - @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") - public DataSource slaveDataSource(DruidProperties druidProperties) + @ConfigurationProperties("spring.datasource.druid.sqlserver") + @ConditionalOnProperty(prefix = "spring.datasource.druid.sqlserver", name = "enabled", havingValue = "true") + public DataSource sqlServerDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); @@ -51,12 +53,20 @@ public class DruidConfig @Bean(name = "dynamicDataSource") @Primary - public DynamicDataSource dataSource(DataSource masterDataSource) - { + public DynamicDataSource dataSource(@Qualifier("dmDataSource") DataSource dmDataSource) { Map targetDataSources = new HashMap<>(); - targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); - setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); - return new DynamicDataSource(masterDataSource, targetDataSources); + targetDataSources.put(DataSourceType.DM.name(), dmDataSource); + + // 尝试获取 slaveDataSource,如果存在则添加到数据源中 + try { + DataSource sqlServerDataSource = SpringUtils.getBean("sqlServerDataSource"); + targetDataSources.put(DataSourceType.SQLSERVER.name(), sqlServerDataSource); + } catch (Exception e) { + // 这里可以记录日志表示未启用 SQLServer 数据源 + System.out.println("SQLServer DataSource is not enabled or configured."); + } + + return new DynamicDataSource(dmDataSource, targetDataSources); } /** diff --git a/ktg-system/src/main/java/com/ktg/system/service/impl/SysConfigServiceImpl.java b/ktg-system/src/main/java/com/ktg/system/service/impl/SysConfigServiceImpl.java index 562286d..a6bdab1 100644 --- a/ktg-system/src/main/java/com/ktg/system/service/impl/SysConfigServiceImpl.java +++ b/ktg-system/src/main/java/com/ktg/system/service/impl/SysConfigServiceImpl.java @@ -47,7 +47,7 @@ public class SysConfigServiceImpl implements ISysConfigService * @return 参数配置信息 */ @Override - @DataSource(DataSourceType.MASTER) + @DataSource(DataSourceType.DM) public SysConfig selectConfigById(Long configId) { SysConfig config = new SysConfig();