feat: 增加sqlserver数据源

This commit is contained in:
Kelvin 2024-11-27 17:07:23 +08:00
parent bc00c4380c
commit d9992854e6
8 changed files with 42 additions and 54 deletions

View File

@ -103,32 +103,6 @@
</dependency>
<!-- 达梦数据库驱动依赖 -->
<!-- <dependency>-->
<!-- <groupId>com.dameng</groupId>-->
<!-- <artifactId>DmJdbcDriver18</artifactId>-->
<!-- <version>8.1.2.79</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dameng</groupId>-->
<!-- <artifactId>DmDialect-for-hibernate5.4</artifactId>-->
<!-- <version>8.1.3.140</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dm</groupId>-->
<!-- <artifactId>DmJdbcDriver</artifactId>-->
<!-- <version>1.8</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/DmJdbcDriver18.jar</systemPath>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dm.dialect</groupId>-->
<!-- <artifactId>hibernate5</artifactId>-->
<!-- <version>5.4</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/DmDialect-for-hibernate5.4.jar</systemPath>-->
<!-- </dependency>-->
<dependency>
<groupId>com.dm</groupId>
<artifactId>DmJdbcDriver</artifactId>
@ -139,7 +113,12 @@
<artifactId>hibernate5</artifactId>
<version>5.4</version>
</dependency>
<!--sqlServer-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre8</version>
</dependency>
</dependencies>
<build>

View File

@ -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://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
# 最小连接池数量

View File

@ -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:

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路径 -->
<property name="log.path" value="/home/ktg/logs" />
<property name="log.path" value="./logs" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

View File

@ -24,5 +24,5 @@ public @interface DataSource
/**
* 切换数据源名称
*/
public DataSourceType value() default DataSourceType.MASTER;
public DataSourceType value() default DataSourceType.DM;
}

View File

@ -10,10 +10,10 @@ public enum DataSourceType
/**
* 主库
*/
MASTER,
DM,
/**
* 从库
*/
SLAVE
SQLSERVER,
}

View File

@ -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<Object, Object> 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);
}
/**

View File

@ -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();