Java 配置Springboot以使用2个不同的数据库
我正在学习如何使用springboot作为java后端框架,目前我已将Java 配置Springboot以使用2个不同的数据库,java,mysql,database,spring,spring-boot,Java,Mysql,Database,Spring,Spring Boot,我正在学习如何使用springboot作为java后端框架,目前我已将应用程序.properties配置为使用1个数据库 我正在考虑添加一个额外的数据库来存储不同的信息,而不是将所有信息都保存在一个数据库中,因此我想知道如何(如果可能)做到这一点 My application.properties文件包含如下数据: spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://
应用程序.properties
配置为使用1个数据库
我正在考虑添加一个额外的数据库来存储不同的信息,而不是将所有信息都保存在一个数据库中,因此我想知道如何(如果可能)做到这一点
My application.properties文件包含如下数据:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306...
datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres
datasource.postgres.driverClassName=org.postgresql.Driver
有什么想法吗?您可以创建两个数据源,其中一个bean标记为@Primary
@Bean
@ConfigurationProperties(prefix = "datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "datasource.postgres")
@Bean
@Primary
public DataSource postgresDataSource() {
return DataSourceBuilder.create().
build();
}
您的application.properties应如下所示:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306...
datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres
datasource.postgres.driverClassName=org.postgresql.Driver
您可以创建两个数据源,其中一个bean标记为@Primary
@Bean
@ConfigurationProperties(prefix = "datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "datasource.postgres")
@Bean
@Primary
public DataSource postgresDataSource() {
return DataSourceBuilder.create().
build();
}
您的application.properties应如下所示:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306...
datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres
datasource.postgres.driverClassName=org.postgresql.Driver
您可以创建两个数据源,其中一个bean标记为@Primary
@Bean
@ConfigurationProperties(prefix = "datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "datasource.postgres")
@Bean
@Primary
public DataSource postgresDataSource() {
return DataSourceBuilder.create().
build();
}
您的application.properties应如下所示:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306...
datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres
datasource.postgres.driverClassName=org.postgresql.Driver
您可以创建两个数据源,其中一个bean标记为@Primary
@Bean
@ConfigurationProperties(prefix = "datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@ConfigurationProperties(prefix = "datasource.postgres")
@Bean
@Primary
public DataSource postgresDataSource() {
return DataSourceBuilder.create().
build();
}
您的application.properties应如下所示:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306...
datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres
datasource.postgres.driverClassName=org.postgresql.Driver
只需要两个具有单独bean或JNDI名称的数据源,每个数据源都有自己的连接URL、驱动程序和凭据。只需要两个具有单独bean或JNDI名称的数据源,每个数据源都有自己的连接URL、驱动程序和凭据。只需两个具有单独bean或JNDI名称的数据源,每个数据源都有自己的连接URL,驱动程序和凭据。只需要两个具有单独bean或JNDI名称的数据源,每个数据源都有自己的连接URL、驱动程序、,和凭据。一个小改进:当使用MySQL或Postgres作为Spring Boot时,不需要指定驱动程序类名,它将根据urlA小改进自动计算出来:当使用MySQL或Postgres作为Spring Boot时,不需要指定驱动程序类名,它将根据urlA小改进自动计算出来改进:当使用MySQL或Postgres作为Spring Boot时,您不需要指定驱动程序类名,它将根据url自动计算出来。一个小改进:当使用MySQL或Postgres作为Spring Boot时,您不需要指定驱动程序类名,它将根据url自动计算出来