请提供springboot连接双数据源的方法以及使用例子
时间: 2023-09-10 14:15:48 浏览: 51
当使用Spring Boot时,连接多个数据源的最佳方法是使用Spring Data JPA和Hibernate来处理数据库连接。以下是一个使用Spring Boot连接双数据源的示例代码:
1. 首先,你需要添加相关依赖,具体可以在`pom.xml`文件中添加以下代码:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
```
2. 然后,你需要在`application.properties`文件中定义两个数据源的配置,例如:
```properties
# 主数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/main_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# 从数据源
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary_db?useSSL=false&serverTimezone=UTC
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
```
3. 接下来,你需要在Spring Boot中配置多个数据源。你可以通过使用`@Configuration`注解创建一个配置类,并使用`@Bean`注解来定义两个数据源:
```java
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
@Primary
@Bean(name = "mainDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource mainDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
// 配置JdbcTemplate
@Bean(name = "mainJdbcTemplate")
public JdbcTemplate mainJdbcTemplate(
@Qualifier("mainDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
// 配置EntityManagerFactory
@Primary
@Bean(name = "mainEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean mainEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("mainDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.demo.main.entity")
.persistenceUnit("main")
.build();
}
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("secondaryDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.demo.secondary.entity")
.persistenceUnit("secondary")
.build();
}
// 配置事务管理器
@Primary
@Bean(name = "mainTransactionManager")
public PlatformTransactionManager mainTransactionManager(
@Qualifier("mainEntityManagerFactory") EntityManagerFactory mainEntityManagerFactory) {
return new JpaTransactionManager(mainEntityManagerFactory);
}
@Bean(name = "secondaryTransactionManager")
public PlatformTransactionManager secondaryTransactionManager(
@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory secondaryEntityManagerFactory) {
return new JpaTransactionManager(secondaryEntityManagerFactory);
}
}
```
4. 最后,你可以在你的应用程序中使用`@Autowired`注解注入两个不同的JdbcTemplate和EntityManagerFactory,以便你可以在您的应用程序中访问两个不同的数据库:
```java
@Service
public class UserService {
@Autowired
@Qualifier("mainJdbcTemplate")
private JdbcTemplate mainJdbcTemplate;
@Autowired
@Qualifier("secondaryJdbcTemplate")
private JdbcTemplate secondaryJdbcTemplate;
@Autowired
@Qualifier("mainEntityManagerFactory")
private EntityManagerFactory mainEntityManagerFactory;
@Autowired
@Qualifier("secondaryEntityManagerFactory")
private EntityManagerFactory secondaryEntityManagerFactory;
@Transactional(value = "mainTransactionManager")
public void addUser(User user) {
mainJdbcTemplate.update("insert into user (name, age) values (?, ?)", user.getName(), user.getAge());
}
@Transactional(value = "secondaryTransactionManager")
public void addSecondaryUser(User user) {
EntityManager entityManager = secondaryEntityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(user);
entityManager.getTransaction().commit();
}
}
```
以上就是使用Spring Boot连接双数据源的方法和示例代码。注意,你需要在你的代码中明确指定使用哪个数据源进行数据库操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)