spring:
profiles:
include: base
thymeleaf:
cache: false
encoding: UTF-8
mode: LEGACYHTML5
servlet:
content-type: text/html
# BI
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: url1
username: 用户名
password: 密码
hikari:
minimum-idle: 1
maximum-pool-size: 10
#connection-test-query: select 1
connection-timeout: 30000
idle-timeout: 60000
max-lifetime: 1800000
# 数据源2
datasource-reportsys:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: url2
username: ba
password: ba
hikari:
minimum-idle: 5
maximum-pool-size: 50
#connection-test-query: select 1
connection-timeout: 30000
idle-timeout: 60000
max-lifetime: 1800000
#数据源3
datasource-scm:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl: url3
username: sa
password: sa
hikari:
minimum-idle: 5
maximum-pool-size: 50
#connection-test-query: select 1
connection-timeout: 30000
idle-timeout: 60000
max-lifetime: 1800000
第二部: 启动类配置
package com.lesu;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import javax.sql.DataSource;
@SpringBootApplication(scanBasePackages = {"com.ice","com.lesu"})
@EntityScan(basePackages = {"com.ice","com.lesu"})
@EnableJpaRepositories(basePackages = {"com.ice","com.lesu"})
public class BIApp {
/**
* 主数据源(本系统)
* @return
*/
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
DataSource ds = DataSourceBuilder.create().build();
return ds;
}
/**
*数据源2
* @return
*/
@Bean(name = "reportsysDataSource")
@ConfigurationProperties(prefix = "spring.datasource-reportsys")
public DataSource reportsysDataSource() {
DataSource ds = DataSourceBuilder.create().build();
return ds;
}
/**
* 数据源3
* @return
*/
@Bean(name = "scmDataSource")
@ConfigurationProperties(prefix = "spring.datasource-scm")
public DataSource scmDataSource() {
DataSource ds = DataSourceBuilder.create().build();
return ds;
}
/**
* 经营数据统计系统 JdbcTemplate
* @param reportsysDataSource
* @return
*/
@Bean(name = "reportsysTemplate")
public JdbcTemplate rocketBaseJdbcTemplate(@Qualifier("reportsysDataSource") DataSource reportsysDataSource) {
return new JdbcTemplate(reportsysDataSource);
}
/**
* E享统计系统 JdbcTemplate
* @param scmDataSource
* @return
*/
@Bean(name = "scmTemplate")
public JdbcTemplate rocketBaseJdbcTemplate1(@Qualifier("scmDataSource") DataSource scmDataSource) {
return new JdbcTemplate(scmDataSource);
}
@Bean(name = "namedParameterJdbcTemplate")
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
NamedParameterJdbcTemplate jdbcTemplate2 = new NamedParameterJdbcTemplate(jdbcTemplate);
return jdbcTemplate2;
}
public static void main(String[] args) {
SpringApplication.run(BIApp.class, args);
}
/**
* 配置定时任务线程数量
*
* @return
*/
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(20);
return scheduler;
}
}
第三步 使用 注入templete
在实现类中 注入
@Autowired
@Qualifier("scmTemplate")
private JdbcTemplate scmTemplate;
@Autowired
@Qualifier("reportsysTemplate")
private JdbcTemplate reportsysTemplate;
String orderCountSql = MessageFormat.format("select a from 表");
List<Map<String, Object>> orgOrderCountList = scmTemplate.queryForList(orderCountSql);
String sql = MessageFormat.format("SELECT * from biaom")
List<Map<String, Object>> list = reportsysTemplate.queryForList(sql);