springboot多数据源

1.添加多个数据原配置,这里是一盒postgres一个ck两个数据库

server:
  port: 9002

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: extranet_server
  servlet:
    multipart:
      enabled: true
      max-file-size: 200MB
      max-request-size: 200MB
  postgredatasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://192.168.1.44:5433/cli_server
    username: postgres
    password:
  ckdatasource:
    url: jdbc:clickhouse://192.168.1.44:8123/cli_server
    username:
    password:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
  druid:
    initial-size: 8
    min-idle: 1
    max-active: 20
    max-wait: 60000
    time-between-eviction-runsMillis: 60000
    min-evictable-idle-timeMillis: 300000
    validation-query: select 1
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    pool-prepared-statements: true
    max-open-prepared-statements: 20
    max-pool-prepared-statement-per-connection-size: 20
    filters: stat
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true
  redis:
    host: 192.168.1.111
    port: 6379
    password: 123456
    database: 5
    lettuce:
      pool:
        max-wait: -1
        max-active: 8
        min-idle: 5
        max-total: 500
      timeout: 5000

在Spring Boot中配置多数据源可以通过以下步骤实现:

  1. 在pom.xml文件中添加依赖:
 

复制代码

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency>

  1. 在application.properties文件中配置数据源信息:
 

复制代码

# 主数据源 spring.datasource.url=jdbc:mysql://localhost:3306/main_db?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 从数据源 spring.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary_db?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.secondary.username=root spring.datasource.secondary.password=123456 spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

  1. 创建两个数据源的配置类:
 

复制代码

@Configuration public class MainDataSourceConfig { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource") public DataSource mainDataSource() { return DataSourceBuilder.create().build(); } } @Configuration public class SecondaryDataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }

  1. 在需要使用数据源的地方注入对应的数据源即可:
 

复制代码

@Service public class UserServiceImpl implements UserService { @Autowired @Qualifier("mainDataSource") private DataSource mainDataSource; @Autowired @Qualifier("secondaryDataSource") private DataSource secondaryDataSource; // ... }

在指定的Mapper中切换数据源可以通过以下步骤实现:

  1. 在需要切换数据源的Mapper接口上添加@Mapper注解,并使用@Qualifier注解指定对应的数据源:
 

复制代码

@Mapper @Qualifier("secondaryDataSource") public interface SecondaryUserMapper { // ... }

  1. 在需要切换数据源的方法上使用@Select注解,并在SQL语句中使用${}占位符引用参数:
 

复制代码

@Select("SELECT * FROM user WHERE id = ${id}") List<User> getUserById(@Param("id") Long id);

  1. 在需要切换数据源的Service实现类中注入对应的Mapper即可:
 

复制代码

@Service public class UserServiceImpl implements UserService { @Autowired private MainUserMapper mainUserMapper; @Autowired private SecondaryUserMapper secondaryUserMapper; @Override public List<User> getUserById(Long id) { if (id % 2 == 0) { return mainUserMapper.getUserById(id); } else { return secondaryUserMapper.getUserById(id); } } // ... }

这样就可以根据需要在不同的Mapper中切换数据源了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值