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同表,数据不同。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值