Spring Boot Configure and Use Two DataSources

后端 未结 9 2059
不思量自难忘°
不思量自难忘° 2020-11-22 04:26

How can I configure and use two data sources?

For example here is what I have for the first data source:

application.properties



        
9条回答
  •  广开言路
    2020-11-22 05:05

    # Here '1stDB' is the database name
    spring.datasource.url=jdbc:mysql://localhost/A
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    
    # Here '2ndDB' is the database name
    spring.second-datasourcee.url=jdbc:mysql://localhost/B
    spring.second-datasource.username=root
    spring.second-datasource.password=root
    spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver
    
    
        @Bean
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource firstDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        @ConfigurationProperties(prefix = "spring.second-datasource")
        public DataSource secondDataSource() {
           return DataSourceBuilder.create().build();
        }
    

提交回复
热议问题