栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot yml文件中配置多数据源连接方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot yml文件中配置多数据源连接方式

SpringBoot yml文件中配置多数据源连接方式
  • springboot配置多数据源
    • pom文件配置
    • application.yml配置
    • 配置Configuration类
    • 配置一个控制器,并启动程序
    • 测试

springboot配置多数据源

因项目需要用到多数据源,特意从网上找到的一套可行方式,并在本地进行启动,测试

pom文件配置

	
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.springframework.boot
			spring-boot-starter-web
		
        
            org.springframework.boot
            spring-boot-autoconfigure
            2.5.7
        
        
            com.oracle.database.jdbc
            ojdbc8
            21.1.0.0
        

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 {

    
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    @Bean(name = "db1DataSource")
    public DataSource masterDataSource() throws SQLException {
        return DataSourceBuilder.create().build();
    }

    
    @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 getDemo () throws SQLException {
    	// 使用的是db1数据库,也就是默认数据库
        List> maps = this.jdbcTemplate.queryForList("select * from table_a where name = 'xiaoming' ");
        System.out.println("默认数据库 =>" + maps.toString());
        
		// 使用的是默认数据库只不过是以传参形式生效
        JdbcTemplate jdbc = new JdbcTemplate(db1DataSource);
        List> maps1 = jdbc.queryForList("select * from table_a where name = 'xiaoming' ");
        System.out.println("默认数据库,使用传参形式,参数为db1 =>" + maps1.toString());
		
		// 使用的是db2数据库,以传参形式实现
        JdbcTemplate jdbc2 = new JdbcTemplate(db2DataSource);
        List> maps2 = jdbc2.queryForList("select * from table_a where name = 'xiaoming' ");
        System.out.println("使用2号数据库,传参,参数为db2 =>" + maps2.toString());
        
        return null;
    }
}

测试

直接在流程器访问:http://localhost/getDemo ,控制台打印的数据前两条数据是相同的,第三条是不同的,前提是你的db1和db2同表,数据不同。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号