当前位置: 首页 > 知识库问答 >
问题:

如何在Spring-Boot中使用2个或更多的jdbcTemplate?

戚勇
2023-03-14

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

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

canNotGetJDBCConnectionException:未能获得JDBC连接;嵌套异常是java.sql.sqlException:无法为在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.jdbctemplate.query(

共有1个答案

叶晋
2023-03-14

我错了,我想通过application.properties在没有@bean配置的情况下建立mysql连接。如果你想建立2个或更多的连接,你只需要用@configurationproperties(prefix=“spring.mysqldatasource”)定义所有的数据源,而不是“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;

此代码为我成功运行。如果有人有疑问,尽管问。

 类似资料:
  • 我有一个运行Spring MVC的应用程序。 我需要它访问我的应用程序中的2个不同的数据库(一个是PostgreSQL和另一个是MySQL数据库)。 问候。

  • 问题内容: 我有一个运行Spring MVC的应用程序。 我需要它来访问我的应用程序中的2个不同的数据库(一个是PostgreSQL,另一个是MySQL数据库)。 如何仅使用批注或application.properties文件进行配置? 问题答案: 这是示例代码,希望对你有所帮助! application.properties DatabaseItemsConfig.java DatabaseU

  • 问题内容: 我的javaswing应用程序中大约有3帧。如何处理这些框架的正确方法是什么?我的意思是某种模式或其他。现在,我总是有一个代表框架的类,一个代表面板的框架,这是该框架中的主要类。现在我已将帧定义为静态变量,当我想隐藏它们时,我称 这是正确的解决方案吗? 问题答案: 除了一个或多个实例的(出色)建议之外,还有一些其他策略可能会单独或组合使用,以将各种内容窗格折叠到一个框架中。 / (Tu

  • 我们在Spring启动应用程序中使用hystrix。我们希望使用MDC将请求ID、请求URI和登录用户等特定属性附加到每个日志语句中。这种机制在实现hystrix的任何地方都不起作用。 对于非hystrix注释的方法,它的工作非常好。我知道MDC是特定于线程的,hystrix在不同的线程上执行。由于我无法对hystrix发表评论,请建议开展相关工作。

  • 问题内容: 这是我使用2 fork()系统一个接一个地调用的代码-它实际上如何工作? 我得到的输出为: 0。我是进程27701 1. 我是进程25915 1.我是 进程27701 2.我是进程27781 2.我是进程26170 2.我是进程27701 这是我使用3个fork系统调用的下一个程序,如何获得这样的输出? 如果我要手动解决此代码,那么逻辑是什么? 在这里,我得到的输出为: 0。我是进程2