如何使用2个或更多jdbcTemplate与spring-boot?

时间:2018-05-07 14:09:08

标签: java spring-boot jdbc spring-jdbc jdbctemplate

我想在我的项目中使用2个或更多jdbcTemplate使用application.properties.I尝试但是得到了运行时异常。

##########我的application.properties:-
     spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     spring.datasource.url=jdbc:mysql://localhost:3306/ccm_new
     spring.datasource.username=test
     spring.datasource.password=test

      spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
   spring.oracledatasource.password=test
   spring.oracledatasource.username=test
   spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver

     @Bean(name = "dsMaster") ############
     @Primary
     @ConfigurationProperties(prefix="spring.oracledatasource")
     public DataSource primaryDataSource() {
     return DataSourceBuilder.create().build();
    }

      @Bean(name = "jdbcMaster") #############
        public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster)
           {
       return new JdbcTemplate(dsMaster);
      }
################我正常使用mysql连接但是使用oracle连接我得到了

org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接;嵌套异常是java.sql.SQLException:无法创建类''用于连接网址' null'     在org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)     在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)     在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)     在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)     在enter code here

2 个答案:

答案 0 :(得分:1)

我得到了我错的地方,我想通过没有@bean配置的application.properties建立mysql连接。如果你想接受2个或更多连接,你只需要用他们的@ConfigurationProperties定义所有数据源(prefix = " spring.mysqldatasource")不同的prifix而不是" spring.datasource" .prifix" spring.datasource"仅在我们需要从一个数据库建立连接时才使用。这是最终的工作代码示例: -

application.properties

 spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
 spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
 spring.mysqldatasource.username=test
 spring.mysqldatasource.password=test
 spring.mysqldatasource.dbcp2.initial-size=5
 spring.mysqldatasource.dbcp2.max-total=15
 spring.mysqldatasource.dbcp2.pool-prepared-statements=true



spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true



 @Configuration
 public class PrototypeUtility {
 @Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
      return new JdbcTemplate(dsMaster);
}

@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
      return new JdbcTemplate(dsMasterMysql);
}
  }

然后我自动连接两个连接: -

     @Autowired
      private JdbcTemplate jdbcMasterMysql;

     @Autowired
     public JdbcTemplate jdbcMaster;

此代码为我成功运行。 如果有人怀疑,请不要犹豫。

答案 1 :(得分:0)

我得到了我错的地方,我想通过没有@bean配置的application.properties建立mysql连接。如果你想接受2个或更多连接,你只需要用他们的@ConfigurationProperties定义所有数据源(prefix = " spring.mysqldatasource")不同的prifix而不是" spring.datasource" .prifix" spring.datasource"仅在我们需要从一个数据库建立连接时才使用。这是最终的工作代码示例: -

application.properties

spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
 spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
 spring.mysqldatasource.username=test
 spring.mysqldatasource.password=test
 spring.mysqldatasource.dbcp2.initial-size=5
 spring.mysqldatasource.dbcp2.max-total=15
 spring.mysqldatasource.dbcp2.pool-prepared-statements=true



spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true



 @Configuration
 public class PrototypeUtility {
 @Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
      return new JdbcTemplate(dsMaster);
}

@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
      return new JdbcTemplate(dsMasterMysql);
}
  }