springboot JdbcTemplate执行mysql 多数据源方法

业务中需要用到一个服务连接多个mysql数据库源的需求,整理一个小demo记录一下。
用到springboot1.5.4,通过JdbcTemplate执行sql,文末demo地址。

step1:添加datasource配置类

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 {
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

step2:properties问价添加mysql配置


spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
# input your first mysql db connection info
spring.datasource.primary.url=jdbc:mysql://192.168.0.1:3306/test?autoReconnect=true&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=root


spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
# input your second mysql db connection info
spring.datasource.secondary.url=jdbc:mysql://192.168.0.2:3306/test?autoReconnect=true&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root

step3:注入,并测试

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate primaryJdbcTemplate;
    
    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate secondaryJdbcTemplate;

    @Override
    public void run(String... args) throws Exception {
        String sql1 = "show databases;";
        List source1 = 
                primaryJdbcTemplate.queryForList(sql1,String.class);
        String sql2 = "show databases;";
        List source2 = 
                secondaryJdbcTemplate.queryForList(sql2,String.class);
        System.out.println("source1 printing--------------------------->");
        for(String s:source1) {
            System.out.println(s);
        }
        System.out.println("source2 printing--------------------------->");
        for(String s:source2) {
            System.out.println(s);
        }
    }

完成

完整的demo地址请点击我

你可能感兴趣的:(springboot JdbcTemplate执行mysql 多数据源方法)