在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host、需要使用多种数据库(MySql、Oracle、SqlServer…)
如果使用springboot开发,可做如下配置:
Config:
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean(name = "testDataSource")
@Primary
@Qualifier("testDataSource")
@ConfigurationProperties(prefix="spring.datasource.hikari.mysql")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(n