Spring Boot:如何使用多个模式并动态选择在运行时使用哪个模式

And*_*Dev 15 mysql database jpa spring-boot

我有同样的问题,但我想知道答案. Spring Boot:如何使用多个模式并动态选择在运行时为每个请求使用哪个模式

请帮我找到答案

如何建立一个数据库连接并为每个请求指定不同的模式?

先感谢您.

bur*_*ete 12

是否可以定义多个数据源,并根据您的请求更改为具有正确架构的数据源?

spring.datasource.url = jdbc:oracle:thin:@//maui:1521/xe
spring.datasource.username = schema1
spring.datasource.password = ...

spring.datasource2.url = jdbc:oracle:thin:@//maui:1521/xe
spring.datasource2.username = schema2
spring.datasource2.password = ..

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource schema1() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.datasource2")
public DataSource schema2() {
    return DataSourceBuilder.create().build();
}
Run Code Online (Sandbox Code Playgroud)

否则,您需要终止并重新创建连接以继续使用单一数据源,但这对您的应用程序来说非常慢,因为它需要一次又一次地重新连接.最好使用一些NoSQL数据库来实现这种动态数据存储.

  • 不,我正在使用signle db,但是通过api 主体给出了不同的模式和模式名称,我需要从多个模式中进行查询并做出响应。 (2认同)