Springboot配置多数据源

该文章展示了如何在SpringBoot应用中配置多个数据源,包括主数据源和两个额外的数据源。每个数据源都使用HikariCP作为连接池,并配置了相应的数据库连接信息。此外,文章还展示了如何创建并注入JdbcTemplate实例,以便在业务逻辑中使用这些数据源进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值