请提供springboot连接双数据源的方法以及使用例子
时间: 2023-09-10 19:15:46 浏览: 92
spring boot 双数据源
好的,下面是Spring Boot连接双数据源的方法以及使用例子:
1. 引入相关依赖
在pom.xml文件中引入相关依赖,例如:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
```
2. 配置application.properties文件
在application.properties文件中配置主数据源和副数据源的相关信息,例如:
```properties
# 主数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/maindb
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/seconddb
spring.datasource.secondary.username=root
spring.datasource.secondary.password=123456
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
```
3. 创建数据源配置类
创建两个数据源的配置类,例如:
```java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = { "com.example.maindb.repository" })
public class PrimaryDataSourceConfig {
@Bean(name = "dataSourcePrimary")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSourcePrimary") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.maindb.entity")
.persistenceUnit("primary")
.build();
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactoryPrimary") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySecondary",
transactionManagerRef = "transactionManagerSecondary",
basePackages = { "com.example.seconddb.repository" })
public class SecondaryDataSourceConfig {
@Bean(name = "dataSourceSecondary")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSourceSecondary") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.seconddb.entity")
.persistenceUnit("secondary")
.build();
}
@Bean(name = "transactionManagerSecondary")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactorySecondary") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
```
4. 使用数据源
在代码中使用数据源,例如:
```java
@Service
public class UserService {
@Autowired
private UserRepository mainUserRepository;
@Autowired
@Qualifier("secondaryEntityManagerFactory")
private EntityManagerFactory secondaryEntityManagerFactory;
public List<User> getAllMainUsers() {
return mainUserRepository.findAll();
}
public List<User> getAllSecondaryUsers() {
EntityManager entityManager = secondaryEntityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT u FROM User u");
List<User> users = query.getResultList();
entityManager.close();
return users;
}
}
```
以上就是Spring Boot连接双数据源的方法以及使用例子,希望能够对你有所帮助。
阅读全文