下面就详细讲解“Spring Jpa多数据源工程配置过程解析”的完整攻略。
一、Spring Jpa多数据源工程配置过程解析
1.1 背景
在实际开发中,有时候我们需要使用多个数据源,分别连接不同数据库进行数据库操作。Spring Jpa框架提供了配置多数据源的方法,本文将详细介绍配置过程。
1.2 配置步骤
- 添加Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
- 配置数据源
在application.properties文件中配置数据源信息,例如:
# 主数据源:MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=12345
# 从数据源:Oracle
jdbc2.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc2.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL
jdbc2.datasource.username=test
jdbc2.datasource.password=12345
- 配置JPA数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
basePackages = {"com.example.dao"}
)
public class JpaConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.entity")
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
- 配置第二个JPA数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "secondEntityManagerFactory",
transactionManagerRef = "secondTransactionManager",
basePackages = {"com.example.dao2"}
)
public class SecondJpaConfig {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "jdbc2.datasource")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("secondDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.entity2")
.persistenceUnit("secondPersistenceUnit")
.build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(
@Qualifier("secondEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
1.3 示例说明
下面给出两个示例,分别是从数据库和主数据库的查询。
- 查询主数据库的用户数据
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getList(){
return userRepository.findAll();
}
}
- 查询从数据库的部门数据
@Service
public class DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
public List<Department> getList(){
return departmentRepository.findAll();
}
}
1.4 总结
本文介绍了Spring Jpa多数据源工程配置过程和示例说明,详细讲解了如何配置数据源和JPA数据源,以及如何实现多个数据源的连接与操作。希望本文对读者有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring Jpa多数据源工程配置过程解析 - Python技术站