springboot druid 多数据源 示例
时间: 2023-05-23 19:02:50 浏览: 113
这是一个使用Spring Boot和Druid连接池的多数据源示例:
1. 首先,需要添加以下依赖关系:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
```
2. 在application.properties文件中配置数据源:
```
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#主数据源
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#从数据源1
spring.datasource.test1.url=jdbc:mysql://localhost:3306/test1?useSSL=false
spring.datasource.test1.username=root
spring.datasource.test1.password=root
#从数据源2
spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2?useSSL=false
spring.datasource.test2.username=root
spring.datasource.test2.password=root
```
3. 在代码中配置多数据源:
```
@Configuration
public class DataSourceConfig {
@Bean(name = "masterDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource test1DataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource test2DataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
public DynamicDataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("master", dataSource());
dataSourceMap.put("test1", test1DataSource());
dataSourceMap.put("test2", test2DataSource());
dynamicDataSource.setDefaultTargetDataSource(dataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap);
return dynamicDataSource;
}
}
```
4. 创建动态数据源:
```
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
```
5. 在需要使用不同数据源的方法中设置数据源:
```
@Component
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public int addUser(User user) {
DataSourceContextHolder.setDataSource(DataSourceNames.TEST1);
String sql = "insert into user(id, name, age) values(?, ?, ?)";
return jdbcTemplate.update(sql, user.getId(), user.getName(), user.getAge());
}
public List<User> getAllUsers() {
DataSourceContextHolder.setDataSource(DataSourceNames.TEST2);
String sql = "select * from user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class));
}
}
```
6. 在访问需要使用不同数据源的方法时切换数据源:
```
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/addUser")
public int addUser(@RequestBody User user) {
return userService.addUser(user);
}
@GetMapping("/getAllUsers")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
```
以上就是Spring Boot和Druid连接池的多数据源示例。
阅读全文