赞
踩
<!-- PostgreSql驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 主库数据源
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/i_ren_shi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: true
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/easytrack
username: 123456
password: 123456
easytrack:
# 从数据源开关/默认关闭
enabled: true
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/easytrack
username: 123456
password: 123456
package com.ruoyi.common.enums;
/**
* 数据源
*
* @author ruoyi
*/
public enum DataSourceType
{
/**
* 主库
*/
MASTER,
/**
* 从库
*/
SLAVE,
/**
* 新配置数据源名称
*/
EASYTRACK
}
@Bean
@ConfigurationProperties("spring.datasource.druid.easytrack")
@ConditionalOnProperty(prefix = "spring.datasource.druid.easytrack", name = "enabled", havingValue = "true")
public DataSource easyTrackDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
setDataSource(targetDataSources, DataSourceType.EASYTRACK.name(), "easyTrackDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources);
}
@DataSource(value = DataSourceType.EASYTRACK)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。