springboot jdbc连接多个数据源

Posted welleys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot jdbc连接多个数据源相关的知识,希望对你有一定的参考价值。

源码

依赖

注:github中源码依赖是精简后的结果.

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring's support of jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- spring's support of test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

配置

取消数据库自动配置

  • 自动配置默认加载spring.datasource.*配置
  • 若此配置不存在,spring boot报错
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class)
public class Application 
    public static void main(String args[]) 
        SpringApplication.run(Application.class, args);
    

配置多个数据库

src/main/resources/application.yml

src:
  datasource:
    jdbc-url: jdbc:mysql://serverIP:port/db?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST
    username: user
    password: pwd

target:
  datasource:
    jdbc-url: jdbc:mysql://serverIP:port/db?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST
      username: user
      password: pwd

加载多数据库配置

  • @Primary指定默认的主要数据库
  • @Bean的name属性指定名称,可与@Qualifier配置使用,装载指定名称的bean
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 

    @Primary
    @Bean(name = "srcDs")
    @ConfigurationProperties(prefix = "src.datasource")
    public DataSource srcDs() 
        return DataSourceBuilder.create().build();
    

    @Bean(name = "targetDs")
    @ConfigurationProperties(prefix = "target.datasource")
    public DataSource targetDs() 
        return DataSourceBuilder.create().build();
    

    @Bean(name = "srcJdbcTemplate")
    public JdbcTemplate srcJdbcTemplate(
            @Qualifier("srcDs") DataSource dataSource) 
        return new JdbcTemplate(dataSource);
    

    @Bean(name = "targetJdbcTemplate")
    public JdbcTemplate targetJdbcTemplate(
            @Qualifier("targetDs") DataSource dataSource) 
        return new JdbcTemplate(dataSource);
    

使用

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DataSourcesTest 

    @Autowired
    @Qualifier("srcJdbcTemplate")
    protected JdbcTemplate srcJdbcTemplate;

    @Autowired
    @Qualifier("targetJdbcTemplate")
    protected JdbcTemplate targetJdbcTemplate;

    @Test
    public void test() throws Exception 

        List<Map<String, Object>> srcMaps = srcJdbcTemplate.queryForList(" select * from user limit 10 ");

        List<Map<String, Object>> targetMaps = targetJdbcTemplate.queryForList(" select * from user limit 10 ");

        return;
    

以上是关于springboot jdbc连接多个数据源的主要内容,如果未能解决你的问题,请参考以下文章

springboot jdbc 动态连接数据库

springjdbc连接多个数据库谁有比较好的解决方法

使用多个 jdbc 连接运行并行查询的 Spring Boot 应用程序

数据库连接池文件格式

java数据库连接进化过程(JDBC)

java数据库连接进化过程(JDBC)