1、引入对应数据库相关依赖
<!--mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--postgresql数据库-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.6</version>
</dependency>

<!-- sql server数据库驱动-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>runtime</scope>
</dependency>
2、yml方式配置资源文件
spring:
datasource:
one: # 业务系统库PostgreSQL数据库
url: jdbc:postgresql://XXX/数据库名称?useSSL=false&currentSchema=public
driver-class-name: org.postgresql.Driver
username: xxxx
password: xxx
platform: postgres
two: #年报库sqlServer数据库
jdbc-url: jdbc:sqlserver://XXXXX:1433;DatabaseName=SS2019
username: sa
password: 123
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
3、自定义数据库资源配置类
package com.tongtu.lzz.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 {
@Value("${spring.datasource.two.username}")
private String user;
@Value("${spring.datasource.two.password}")
private String pass;
@Value("${spring.datasource.two.jdbc-url}")
private String url;
@Value("${spring.datasource.two.driver-class-name}")
private String driverClassName;

/**
* 配置业务库PostgreSql数据库
*/
@Bean(name = "baseSys")
@Primary//将当前bean设置为首选
@ConfigurationProperties(prefix = "spring.datasource.one")
DataSource baseSys() {
return DruidDataSourceBuilder.create().build();
}

@Bean(name = "styhDatabase")
JdbcTemplate jdbcTemplateBaseSys(@Qualifier("baseSys") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

/**
* 配置年报库SQL server数据库
*
* @return
*/
@Bean(name = "sqlServerDataSource")
@Qualifier("sqlServerDataSource")
@ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource getMyDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "annualReportDatabase")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("sqlServerDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

4、在serviceImpl层注入对应的jdbc
/**
* 注入SQL Server年报库数据源
*/
@Resource
@Qualifier("annualReportDatabase")
protected JdbcTemplate jdbcTemplateYear;

5、应用:拼接sql,执行sql