关于springboot访问多个mysql库

Posted coolsoul

tags:

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

源码

依赖

注: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访问多个mysql库的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot项目中实现MySQL读写分离

mysql 怎么给单个帐号赋予多个库的访问权限?

解决报错提示:Loading class `com.mysql.jdbc.Driver‘.The new driver class is ‘com.mysql.cj.jdbc.Driver‘.(代码片

(日常疑问)同一个mysql为什么可以查询其他库的数据?

mysql数据库高手进来,关于分库和分表的问题

spring boot jpa 访问 mysql