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
第二步:编写配置类
public class DataSourceConfig {
public DataSource primaryDataSource() {
eturn DataSourceBuilder.create().build();
}
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}