我有这样的application.properties文件:
spring.datasource.first.jdbcUrl=jdbc:mysql://localhost:3306/world?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.first.url=jdbc:mysql://localhost:3306/world?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.first.username=root
spring.datasource.first.password=root
spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/sakila?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.two.url=jdbc:mysql://localhost:3306/sakila?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.two.username=root
spring.datasource.two.password=root
我有以下配置类:
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 MultipleDataSourceConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.first")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDS")
@ConfigurationProperties(prefix="spring.datasource.two")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
但是当我尝试建立与数据库的连接时,它仅适用于第一个数据库。在第二个连接中,它显示了以下错误:
java.sql.SQLSyntaxErrorException: Table 'world.actor' doesn't exist
正在第一个数据库中搜索,而不是对该实体使用第二个连接。我该怎么解决?