Jdbctemplate 다 중 데이터 원본 설정 방법 상세 설명

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;
  }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기