SpringBoot多数据源配置

时间:2021-02-23 07:51:55

 

准备环境:

    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   第二步:编写配置类   @Configurationpublic 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();  } }