在实际的开发工作中,我们经常会碰到需要在一个应用中使用多个数据源的情况。Spring Boot提供了简单而强大的功能,使我们能够轻松配置和使用多个数据源。
添加依赖
首先,我们需要在pom.xml
文件中添加Spring Boot的依赖项:
<dependencies>
<!-- 其他依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
这些依赖将使我们能够使用JDBC和JPA来访问多个数据源。
配置数据源
接下来,我们需要配置多个数据源。在application.properties
文件(或application.yml
文件)中添加以下配置:
# 数据源1
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=username1
spring.datasource.password=password1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 数据源2
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
spring.datasource.secondary.username=username2
spring.datasource.secondary.password=password2
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
以上配置为我们定义了两个数据源,名称分别为primary
和secondary
。我们可以根据需要添加更多的数据源。
创建数据源Bean
接下来,我们需要在Spring容器中创建数据源的Bean,以便我们可以在代码中引用它们。首先,我们需要创建两个数据源的配置类:
@Configuration
public class PrimaryDataSourceConfig {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
@Configuration
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
注意,我们在这里使用了@ConfigurationProperties
注解,以便能够通过配置文件中的属性值自动创建数据源的实例。
然后,我们需要在DataSourceConfig
类中创建一个JdbcTemplate
的Bean,以便于执行SQL语句:
@Configuration
public class DataSourceConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Primary
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
在上面的代码中,我们使用@Autowired
和@Qualifier
注解来自动注入数据源,并创建了两个JdbcTemplate
的Bean实例,一个用于主数据源,一个用于次数据源。
使用多数据源
现在,我们可以在代码中使用多数据源了。假设我们有两个实体类User
和Product
,分别对应于两个数据源。
首先,我们需要在UserRepository
接口中定义主数据源的操作:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// 主数据源操作
}
接着,我们定义一个ProductRepository
接口,来操作次数据源的数据:
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
// 次数据源操作
}
最后,我们在某个服务类中,使用@Primary
和@Qualifier
注解来指定使用的数据源:
@Service
public class UserService {
@Autowired
@Qualifier("primaryJdbcTemplate")
private JdbcTemplate primaryJdbcTemplate;
@Autowired
@Qualifier("secondaryJdbcTemplate")
private JdbcTemplate secondaryJdbcTemplate;
public List<User> getUsersFromPrimaryDataSource() {
String sql = "SELECT * FROM user";
return primaryJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
public List<Product> getProductsFromSecondaryDataSource() {
String sql = "SELECT * FROM product";
return secondaryJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));
}
// 其他操作方法
}
在上面的示例中,我们使用了不同的JdbcTemplate
实例来分别访问不同的数据源。
总结
通过以上步骤,我们成功配置并使用了多个数据源。Spring Boot的多数据源功能使得我们能够更灵活地处理不同数据源的相关操作,使我们的应用程序更加高效和可管理。这对于一些需要同时访问多个数据库的应用程序来说非常有用,比如分布式系统或多租户系统。尽管配置和使用多数据源可能需要一些额外的工作,但它能够为我们带来更多的好处和灵活性。
本文来自极简博客,作者:暗夜行者,转载请注明原文链接:Spring Boot多数据源配置与使用