首页 > 解决方案 > 当从 UI 接收到带有 get 请求的数据库属性时,如何在运行时在 springboot 中配置数据源

问题描述

我有一个要求,其中用户通过 UI 输入数据库连接详细信息,如数据库类型(oracle、mysql、hive)、数据库 url、驱动程序名称、用户名、密码,这些连接详细信息作为GET 请求URL 参数传递给 Spring Boot 应用程序. 然后,我从 Spring Boot 应用程序连接到数据库,提取数据并发送回 UI。

用户可以输入任何数据库连接信息,因此这些详细信息在用户级别是动态的。如何在运行时使用这些用户输入详细信息动态连接到数据库。

   GET Request: http://localhost:8080/api/getData
  
 the following database properties sent from UI to spring boot application      
 {
        dyType: 'hive',
    dbUrl:'jdbc:hive2://localhost:10000/default;principal=hive/localhostDU',
        driverClass: 'org.apache.hive.jdbc.HiveDriver',
        username: 'xyz',
        password: 'xxxxxxxx'
}

我想使用上述详细信息在运行时设置我的数据源。

标签: javaspring-bootdatasourcespring-jdbcjdbctemplate

解决方案


推荐阅读