91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

springboot怎么配置多數據源

小億
75
2023-10-23 10:05:27
欄目: 編程語言

在Spring Boot中配置多個數據源可以通過以下步驟來實現:

  1. 在pom.xml文件中添加Spring Boot對多數據源的支持依賴:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>
  1. 在application.properties或application.yml配置文件中配置每個數據源的連接信息:
# 數據源1
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=root

# 數據源2
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
  1. 創建多個DataSource bean,分別對應每個數據源:
@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}
  1. 配置JpaVendorAdapter和EntityManagerFactory bean,指定每個數據源的JPA配置:
@Configuration
@EnableJpaRepositories(
        basePackages = "com.example.repository", 
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager"
)
public class JpaConfig {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Autowired
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource)
                .packages("com.example.domain")
                .persistenceUnit("primary")
                .build();
    }

    @Bean(name = "secondaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .packages("com.example.domain")
                .persistenceUnit("secondary")
                .build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Bean(name = "secondaryTransactionManager")
    public PlatformTransactionManager secondaryTransactionManager(
            @Qualifier("secondaryEntityManagerFactory") EntityManagerFactory secondaryEntityManagerFactory) {
        return new JpaTransactionManager(secondaryEntityManagerFactory);
    }
}
  1. 在需要使用數據源的地方使用@Qualifier注解指定具體的數據源:
@Service
public class MyService {

    @Autowired
    @Qualifier("entityManagerFactory")
    private EntityManagerFactory entityManagerFactory;

    // 使用entityManagerFactory進行數據庫操作
}

通過以上步驟,就可以在Spring Boot中成功配置多個數據源。

0
汪清县| 岳阳县| 象山县| 广东省| 宝兴县| 进贤县| 镶黄旗| 拜城县| 沈丘县| 比如县| 承德市| 云梦县| 博白县| 临猗县| 绥芬河市| 珠海市| 扎囊县| 江山市| 莆田市| 彭山县| 绥江县| 桂林市| 白玉县| 安阳县| 东乡县| 白山市| 保定市| 眉山市| 屏南县| 土默特左旗| 宁南县| 临安市| 共和县| 焦作市| 沙田区| 霞浦县| 错那县| 东乌| 凌云县| 厦门市| 全州县|