SpringBoot yml文件中配置多数据源连接方式
springboot配置多数据源
因项目需要用到多数据源,特意从网上找到的一套可行方式,并在本地进行启动,测试
持久层实现动态数据源切换持久层数据源动态切换配置链接
pom文件配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.1.0.0</version>
</dependency>
</dependencies>
application.yml配置
server:
port: 80
spring:
datasource:
db1:
username: username
password: password2
jdbc-url: jdbc:oracle:thin:@ip:1521:new
driver-class-name: oracle.jdbc.OracleDriver
db2:
username: username2
password: password2
jdbc-url: jdbc:oracle:thin:@ip:1521:new
driver-class-name: oracle.jdbc.OracleDriver
配置Configuration类
import oracle.jdbc.datasource.impl.OracleDataSource;
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 javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
public class DataSourceConfig {
/**
* DataSource 配置
* @Primary指定当前数据库是主库
*/
@Primary
@ConfigurationProperties(prefix = "spring.datasource.db1")
@Bean(name = "db1DataSource")
public DataSource masterDataSource() throws SQLException {
return DataSourceBuilder.create().build();
}
/**
* DataSource 配置
*/
@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean(name = "db2DataSource")
public DataSource heiaxinDataSource() throws SQLException {
return DataSourceBuilder.create().build();
}
}
配置一个控制器,并启动程序
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
DataSource db1DataSource;
@Autowired
@Qualifier("db2DataSource") // 指定当前db2DataSource使用db2的源
DataSource db2DataSource;
@GetMapping("/getDemo")
public List<Map> getDemo () throws SQLException {
// 使用的是db1数据库,也就是默认数据库
List<Map<String, Object>> maps = this.jdbcTemplate.queryForList("select * from table_a where name = 'xiaoming' ");
System.out.println("默认数据库 =>" + maps.toString());
// 使用的是默认数据库只不过是以传参形式生效
JdbcTemplate jdbc = new JdbcTemplate(db1DataSource);
List<Map<String, Object>> maps1 = jdbc.queryForList("select * from table_a where name = 'xiaoming' ");
System.out.println("默认数据库,使用传参形式,参数为db1 =>" + maps1.toString());
// 使用的是db2数据库,以传参形式实现
JdbcTemplate jdbc2 = new JdbcTemplate(db2DataSource);
List<Map<String, Object>> maps2 = jdbc2.queryForList("select * from table_a where name = 'xiaoming' ");
System.out.println("使用2号数据库,传参,参数为db2 =>" + maps2.toString());
return null;
}
}
测试
直接在流程器访问:http://localhost/getDemo ,控制台打印的数据前两条数据是相同的,第三条是不同的,前提是你的db1和db2同表,数据不同。