SpringBoot多数据源配置

 

准备环境:

    jdk1.8

    eclipse

    tomcat8.0

 

第一步:在配置文件添加如下信息:

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=test
spring.datasource.primary.password=test
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
 
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=test
spring.datasource.secondary.password=test
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
 
第二步:编写配置类
  
@Configuration
public class DataSourceConfig {
  @Bean(name = "primaryDataSource")
  @Qualifier("primaryDataSource")
  @ConfigurationProperties(prefix="spring.datasource.primary")
  public DataSource primaryDataSource() {
    eturn DataSourceBuilder.create().build();
  }
  @Bean(name = "secondaryDataSource")
  @Qualifier("secondaryDataSource")
  @Primary
  @ConfigurationProperties(prefix="spring.datasource.secondary")
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
  } 
}
 
 
 
                    
 
posted @ 2017-07-10 21:16  关键我是你力哥  阅读(857)  评论(0编辑  收藏  举报