druid配置不同数据库mysql,postgresql

引入依赖

<!--阿里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注解,默认使用主数据源。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值