引入依赖
<!--阿里druid数据库链接依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--事务管理:原子性,一致性,隔离性,持久性-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--postgresql数据库-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
配置application-druid.yml文件,添加数据源参数
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 默认使用mysql驱动
driverClassName: com.mysql.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://192.168.1.117:33606/jintumap?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: true
# 使用postgresql驱动
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://192.168.1.117:5432/ZJGGXMK?useUnicode=true&characterEncoding=utf8
username: postgres
password: root
# 从库数据源-XZDB
slave-xzdb:
# 从数据源开关/默认关闭
enabled: true
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://192.168.1.117:5432/XZDB?useUnicode=true&characterEncoding=utf8
username: postgres
password: root
增加DataSourceType类里面的枚举类型
public enum DataSourceType
{
/**
* 主库
*/
MASTER,
/**
* 从库
*/
SLAVE,
/**
* 从库 XZDB
*/
SLAVE_XZDB
}
在DruidConfig中配置多个数据源的datasource
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource(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)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave-xzdb")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave-xzdb", name = "enabled", havingValue = "true")
public DataSource slaveXZDBDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource,DataSource slaveXZDBDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
targetDataSources.put(DataSourceType.SLAVE_XZDB.name(), slaveXZDBDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
在需要切换数据源的方法上加上@DataSource注解
@DataSource(value = DataSourceType.SLAVE)
@Select("select count(1) from public.\"LX_CJ_DK\"")
public int selectZjggxmkCount();
@DataSource(value = DataSourceType.SLAVE_XZDB)
@Select("select count(1) from public.\"XZQ\"")
public int selectXzdbCount();
如果不加@DataSource注解,默认使用主数据源。