1.添加多个数据原配置,这里是一盒postgres一个ck两个数据库
server:port: 9002spring:main:allow-bean-definition-overriding: trueapplication:name: extranet_serverservlet:multipart:enabled: truemax-file-size: 200MBmax-request-size: 200MBpostgredatasource:driver-class-name: org.postgresql.Driverurl: jdbc:postgresql://192.168.1.44:5433/cli_serverusername: postgrespassword:ckdatasource:url: jdbc:clickhouse://192.168.1.44:8123/cli_serverusername:password:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: ru.yandex.clickhouse.ClickHouseDriverdruid:initial-size: 8min-idle: 1max-active: 20max-wait: 60000time-between-eviction-runsMillis: 60000min-evictable-idle-timeMillis: 300000validation-query: select 1test-while-idle: truetest-on-borrow: falsetest-on-return: falsepool-prepared-statements: truemax-open-prepared-statements: 20max-pool-prepared-statement-per-connection-size: 20filters: statconnection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000use-global-data-source-stat: trueredis:host: 192.168.1.111port: 6379password: 123456database: 5lettuce:pool:max-wait: -1max-active: 8min-idle: 5max-total: 500timeout: 5000
在Spring Boot中配置多数据源可以通过以下步骤实现:
- 在pom.xml文件中添加依赖:
复制代码
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency>
- 在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
- 创建两个数据源的配置类:
复制代码
@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(); } }
- 在需要使用数据源的地方注入对应的数据源即可:
复制代码
@Service public class UserServiceImpl implements UserService { @Autowired @Qualifier("mainDataSource") private DataSource mainDataSource; @Autowired @Qualifier("secondaryDataSource") private DataSource secondaryDataSource; // ... }
在指定的Mapper中切换数据源可以通过以下步骤实现:
- 在需要切换数据源的Mapper接口上添加@Mapper注解,并使用@Qualifier注解指定对应的数据源:
复制代码
@Mapper @Qualifier("secondaryDataSource") public interface SecondaryUserMapper { // ... }
- 在需要切换数据源的方法上使用@Select注解,并在SQL语句中使用${}占位符引用参数:
复制代码
@Select("SELECT * FROM user WHERE id = ${id}") List<User> getUserById(@Param("id") Long id);
- 在需要切换数据源的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中切换数据源了。