springboot 配置多数据源 示例代码
时间: 2023-07-23 18:01:06 浏览: 109
以下是一个示例代码,展示了如何在Spring Boot中配置多数据源:
1. 创建一个配置类,用于配置第一个数据源:
```java
@Configuration
@MapperScan(basePackages = "com.example.firstdatasource.mapper", sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class FirstDataSourceConfig {
@Bean(name = "firstDataSource")
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "firstTransactionManager")
public DataSourceTransactionManager firstTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "firstSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
2. 创建另一个配置类,用于配置第二个数据源:
```java
@Configuration
@MapperScan(basePackages = "com.example.seconddatasource.mapper", sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class SecondDataSourceConfig {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
3. 在application.properties或application.yml中添加数据源相关的配置:
```yaml
spring.datasource.first.url=jdbc:mysql://localhost:3306/first_db
spring.datasource.first.username=first_user
spring.datasource.first.password=first_password
spring.datasource.second.url=jdbc:mysql://localhost:3306/second_db
spring.datasource.second.username=second_user
spring.datasource.second.password=second_password
```
4. 在需要使用第一个数据源的地方,使用@Qualifier注解指定要使用的数据源:
```java
@Service
public class FirstDataSourceService {
private final FirstMapper firstMapper;
public FirstDataSourceService(@Qualifier("firstSqlSessionTemplate") SqlSessionTemplate firstSqlSessionTemplate) {
this.firstMapper = firstSqlSessionTemplate.getMapper(FirstMapper.class);
}
// 使用第一个数据源进行数据库操作
// ...
}
```
5. 在需要使用第二个数据源的地方,使用@Qualifier注解指定要使用的数据源:
```java
@Service
public class SecondDataSourceService {
private final SecondMapper secondMapper;
public SecondDataSourceService(@Qualifier("secondSqlSessionTemplate") SqlSessionTemplate secondSqlSessionTemplate) {
this.secondMapper = secondSqlSessionTemplate.getMapper(SecondMapper.class);
}
// 使用第二个数据源进行数据库操作
// ...
}
```
这样就完成了Spring Boot中配置多数据源的示例代码。你可以根据自己的需求进行相应的修改和扩展。
阅读全文