下面为您介绍在Spring Boot中配置多数据源的方法。
1. 添加依赖
在 pom.xml
文件中添加以下依赖:
<!-- 数据源驱动依赖 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!-- Spring Boot JPA依赖 -> 如果使用Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. 配置主数据源
在 application.yml
或 application.properties
文件中,配置主数据源的连接信息:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3. 配置第二个数据源
3.1 创建配置类
创建一个配置类,用来配置第二个数据源的连接信息:
@Configuration
public class DataSourceConfig2 {
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
3.2 在配置文件中配置第二个数据源连接信息
在 application.yml
或 application.properties
文件中添加以下配置信息:
spring:
datasource:
secondary:
url: jdbc:mysql://localhost:3306/mydb2
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
4. 配置JPA
如果需要使用Spring Data JPA,可以在 pom.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>
4.1 创建一个JPA的配置类
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactory2",
basePackages= { "com.example.multi.repository.secondary" }) // 指定JPA Repository的位置
public class JpaConfig2 {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Bean(name = "entityManagerFactory2")
public EntityManagerFactory secondaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean factory = builder
.dataSource(secondaryDataSource)
// 指定Entity的位置
.packages("com.example.multi.entity.secondary")
.persistenceUnit("secondaryPersistenceUnit")
.properties(jpaProperties.getProperties())
.build();
return factory.getObject();
}
@Bean(name = "transactionManager2")
public PlatformTransactionManager secondaryTransactionManager(EntityManagerFactoryBuilder builder) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(secondaryEntityManagerFactory(builder));
return jpaTransactionManager;
}
}
4.2 在Service中使用
最后,在Service中使用EntityManager和@Transactional注解来实现对两个库的访问。
@Service
public class UserService {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@PersistenceContext(unitName = "primaryEntityManagerFactory")
private EntityManager entityManager;
@PersistenceContext(unitName = "secondaryEntityManagerFactory")
private EntityManager entityManager2;
@Transactional
public void save(User user) {
entityManager.persist(user);
}
@Transactional(value = "transactionManager2")
public void save2(User user) {
entityManager2.persist(user);
}
}
示例
示例1: 单个Service内使用两个数据源
@Service
public class UserService {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@PersistenceContext(unitName = "primaryEntityManagerFactory")
private EntityManager entityManager;
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@PersistenceContext(unitName = "secondaryEntityManagerFactory")
private EntityManager entityManager2;
@Transactional
public void save(User user) {
entityManager.persist(user);
}
@Transactional(value = "transactionManager2")
public void save2(User user) {
entityManager2.persist(user);
}
}
示例2: 不同Service使用不同的数据源
@Service
public class UserServicePrimary {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@PersistenceContext(unitName = "primaryEntityManagerFactory")
private EntityManager entityManager;
@Transactional
public void save(User user) {
entityManager.persist(user);
}
}
@Service
public class UserServiceSecondary {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@PersistenceContext(unitName = "secondaryEntityManagerFactory")
private EntityManager entityManager2;
@Transactional(value = "transactionManager2")
public void save2(User user) {
entityManager2.persist(user);
}
}
以上就是在Spring Boot中配置多数据源的方法,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot下配置多数据源的方法 - Python技术站