Jdbctemplate多数据源配置方法详解

所属分类: 软件编程 / java 阅读数: 81
收藏 0 赞 0 分享

1.数据源配置

spring:
  #  jdbctemplate 连接多数据源配置
 db1:
  datasource:
   jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
   username: root
   password: 123456
   driver-class-name: com.mysql.jdbc.Driver
   type: com.alibaba.druid.pool.DruidDataSource
 db2:
  datasource:
   jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main2?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
   username: root
   password: 123456
   driver-class-name: com.mysql.jdbc.Driver
   type: com.alibaba.druid.pool.DruidDataSource

2.启动类

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

3.config 配置datasource

package com.example.demo.jdbctemplate.config;
 
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 javax.sql.DataSource;
 
@Configuration
public class DataSourceConfig {
 
  @Primary //(主数据源配置)
  @Bean(name = "db1")
  @Qualifier("db1")
  @ConfigurationProperties(prefix = "spring.db1.datasource")
  public DataSource mysqlDataSource(){
 
    return DataSourceBuilder.create().build();
  }
 
  //
  @Bean(name = "db2")
  @Qualifier("db2")
  @ConfigurationProperties(prefix = "spring.db2.datasource")
  public DataSource sqlServerDataSource(){
 
    return DataSourceBuilder.create().build();
  }
}

构造 db1JdbcTemplate、  db2JdbcTemplate

package com.example.demo.jdbctemplate.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
 
import javax.sql.DataSource;
 
@Repository
public class DBLoader {
  @Bean(name = "db1JdbcTemplate")
  public JdbcTemplate primaryJdbcTemplate(@Qualifier("db1") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
 
  @Bean(name = "db2JdbcTemplate")
  public JdbcTemplate secondaryJdbcTemplate(@Qualifier("db2") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
 
}

4.调用

@Service
public class DBTools {
  @Autowired
  @Qualifier( "db1JdbcTemplate")
  private JdbcTemplate jdbcTemplate1;
  @Autowired
  @Qualifier("db2JdbcTemplate")
  private JdbcTemplate jdbcTemplate2 ;
 
  JdbcTemplate jdbcTemplate;
 
  public JdbcTemplate getDB(String db ) {
    if("db1".equals(db)){
      return jdbcTemplate1;
    }else if ("db2".equals(db)){
      return jdbcTemplate2;
    }else {
      return null ;
    }
 
  }
 
  /***
   * 查询
   * @param sql
   * @return 返回list
   */
  public  List<Map<String, Object>> queryForList(String db,String sql ) {
    List<Map<String, Object>> queryForList = getDB(db).queryForList(sql );
    return queryForList;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多