Spring Boot 配置和使用两个数据源

Posted

技术标签:

【中文标题】Spring Boot 配置和使用两个数据源【英文标题】:Spring Boot Configure and Use Two DataSources 【发布时间】:2021-06-22 07:38:49 【问题描述】:

如何配置和使用两个数据源?

例如,这里是我的第一个数据源:

application.properties

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...

应用类

@SpringBootApplication
public class SampleApplication

    public static void main(String[] args) 
        SpringApplication.run(SampleApplication.class, args);
    

如何修改application.properties 以添加另一个数据源?如何自动装配它以供不同的存储库使用?

【问题讨论】:

【参考方案1】:

给你。

在你的 application.properties 文件中添加:

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

在使用@Configuration 注释的任何类中添加以下方法:

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


@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() 
    return DataSourceBuilder.create().build();

【讨论】:

看看baeldung.com/spring-data-jpa-multiple-databases,它描述了您正在寻找的内容。 有时您可能需要将 datasource、transactionManager 和 SqlSessionFactory 分配为主要所有。 @K. Siva Prasad Reddy 好的,但我有 2 个不同的 JPARepositories - Spring Boot 如何知道要使用哪个 DataSource?每个 JPARepository 都应该使用不同的数据库 @Matley 这篇博文javadevjournal.com/spring-boot/… 可能正是您想要的。 @K.SivaPrasadReddy 配置多个数据源所有数据库都应该在同一台服务器上?【参考方案2】:

参考the official documentation


创建多个数据源的工作方式与创建第一个数据源相同。如果您使用 JDBC 或 JPA 的默认自动配置,您可能希望将其中一个标记为 @Primary(然后任何@Autowired 注入都会选择该配置)。

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() 
    return DataSourceBuilder.create().build();


@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() 
    return DataSourceBuilder.create().build();

【讨论】:

感谢您提供有关此官方文档的链接。【参考方案3】:

这里是完整的解决方案

#First Datasource (DB1)
db1.datasource.url: url
db1.datasource.username:user
db1.datasource.password:password

#Second Datasource (DB2)
db2.datasource.url:url
db2.datasource.username:user
db2.datasource.password:password

由于我们要访问两个不同的数据库(db1,db2),我们需要分别配置每个数据源配置,如:

public class DB1_DataSource 
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean db1EntityManager() 
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(db1Datasource());
    em.setPersistenceUnitName("db1EntityManager");
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<string, object=""> properties = new HashMap<>();
    properties.put("hibernate.dialect",
            env.getProperty("hibernate.dialect"));
    properties.put("hibernate.show-sql",
            env.getProperty("jdbc.show-sql"));
    em.setJpaPropertyMap(properties);
    return em;


@Primary
@Bean
public DataSource db1Datasource() 

    DriverManagerDataSource dataSource
            = new DriverManagerDataSource();
    dataSource.setDriverClassName(
            env.getProperty("jdbc.driver-class-name"));
    dataSource.setUrl(env.getProperty("db1.datasource.url"));
    dataSource.setUsername(env.getProperty("db1.datasource.username"));
    dataSource.setPassword(env.getProperty("db1.datasource.password"));

    return dataSource;


@Primary
@Bean
public PlatformTransactionManager db1TransactionManager() 

    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            db1EntityManager().getObject());
    return transactionManager;


第二个数据源:

public class DB2_DataSource 

@Autowired
private Environment env;

@Bean
public LocalContainerEntityManagerFactoryBean db2EntityManager() 
    LocalContainerEntityManagerFactoryBean em
            = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(db2Datasource());
    em.setPersistenceUnitName("db2EntityManager");
    HibernateJpaVendorAdapter vendorAdapter
            = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<string, object=""> properties = new HashMap<>();
    properties.put("hibernate.dialect",
            env.getProperty("hibernate.dialect"));
    properties.put("hibernate.show-sql",
            env.getProperty("jdbc.show-sql"));
    em.setJpaPropertyMap(properties);
    return em;


@Bean
public DataSource db2Datasource() 
    DriverManagerDataSource dataSource
            = new DriverManagerDataSource();
    dataSource.setDriverClassName(
            env.getProperty("jdbc.driver-class-name"));
    dataSource.setUrl(env.getProperty("db2.datasource.url"));
    dataSource.setUsername(env.getProperty("db2.datasource.username"));
    dataSource.setPassword(env.getProperty("db2.datasource.password"));

    return dataSource;


@Bean
public PlatformTransactionManager db2TransactionManager() 
    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            db2EntityManager().getObject());
    return transactionManager;


您可以在我的博客上找到完整的示例: Spring Boot with Multiple DataSource Configuration

【讨论】:

此代码将无法运行 2 个 @primary 注释如何在同一个数据库中协同工作。 确实,但是这段代码提到了手动属性设置的重要部分,而不是简单明了的return DataSourceBuilder.create().build();,这显然不起作用,因此我的赞成票在这里【参考方案4】:

使用 Spring Boot 1.5.8.RELEASE 更新 2018-01-07

大多数答案不提供如何使用它们(作为数据源本身和作为事务),只提供如何配置它们。

你可以在https://www.surasint.com/spring-boot-with-multiple-databases-example/看到可运行的例子和一些解释

我在这里复制了一些代码。

首先你必须像这样设置application.properties

#Database
database1.datasource.url=jdbc:mysql://localhost/testdb
database1.datasource.username=root
database1.datasource.password=root
database1.datasource.driver-class-name=com.mysql.jdbc.Driver

database2.datasource.url=jdbc:mysql://localhost/testdb2
database2.datasource.username=root
database2.datasource.password=root
database2.datasource.driver-class-name=com.mysql.jdbc.Driver

然后像这样将它们定义为提供者(@Bean):

@Bean(name = "datasource1")
@ConfigurationProperties("database1.datasource")
@Primary
public DataSource dataSource()
    return DataSourceBuilder.create().build();


@Bean(name = "datasource2")
@ConfigurationProperties("database2.datasource")
public DataSource dataSource2()
    return DataSourceBuilder.create().build();

注意我有@Bean(name="datasource1")@Bean(name="datasource2"),那么当我们需要数据源时可以使用@Qualifier("datasource1")@Qualifier("datasource2"),例如

@Qualifier("datasource1")
@Autowired
private DataSource dataSource;

如果您确实关心事务,则必须为它们定义 DataSourceTransactionManager,如下所示:

@Bean(name="tm1")
@Autowired
@Primary
DataSourceTransactionManager tm1(@Qualifier ("datasource1") DataSource datasource) 
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;


@Bean(name="tm2")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) 
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;

那么你就可以像这样使用它了

@Transactional //this will use the first datasource because it is @primary

@Transactional("tm2")

这应该足够了。请参阅上面链接中的示例和详细信息。

【讨论】:

嗨@Surasin Tancharoen,我们正试图让两个数据源保持相同的数据,这样如果一个失败,应用程序将在另一个数据源上运行。上述方法会好吗? @ArunSudhakaran 否。此解决方案不能用作备份。如果您正在寻找高可用性,那么大多数数据库已经具有使用单个虚拟 IP 运行多个数据库的配置。试试吧。 链接surasint.com/spring-boot-with-multiple-databases-example 不再起作用了。【参考方案5】:
# Here '1stDB' is the database name
spring.datasource.url=jdbc:mysql://localhost/A
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


# Here '2ndDB' is the database name
spring.second-datasourcee.url=jdbc:mysql://localhost/B
spring.second-datasource.username=root
spring.second-datasource.password=root
spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver


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

    @Bean
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() 
       return DataSourceBuilder.create().build();
    

【讨论】:

不适合我。并说:在类路径资源[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]中定义的名称为“dataSourceScriptDatabaseInitializer”的bean创建错误:init方法的调用失败;嵌套异常是 java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName。 @AmirKeshavarz 我的回答可能有点……为时已晚,但是您必须将 datasource.url 修改为 datasource.jdbc-url 并且它会起作用【参考方案6】:

我用的是 mybatis - springboot 2.0 技术栈, 解决方案:

//application.properties - start
    sp.ds1.jdbc-url=jdbc:mysql://localhost:3306/mydb?useSSL=false
    sp.ds1.username=user
    sp.ds1.password=pwd
    sp.ds1.testWhileIdle=true
    sp.ds1.validationQuery=SELECT 1
    sp.ds1.driverClassName=com.mysql.jdbc.Driver


    sp.ds2.jdbc-url=jdbc:mysql://localhost:4586/mydb?useSSL=false
    sp.ds2.username=user
    sp.ds2.password=pwd
    sp.ds2.testWhileIdle=true
    sp.ds2.validationQuery=SELECT 1
    sp.ds2.driverClassName=com.mysql.jdbc.Driver

//application.properties - end

//configuration class

    @Configuration
    @ComponentScan(basePackages = "com.mypkg")
    public class MultipleDBConfig 


        public static final String SQL_SESSION_FACTORY_NAME_1 = "sqlSessionFactory1";
        public static final String SQL_SESSION_FACTORY_NAME_2 = "sqlSessionFactory2";

        public static final String MAPPERS_PACKAGE_NAME_1 = "com.mypg.mymapper1";
        public static final String MAPPERS_PACKAGE_NAME_2 = "com.mypg.mymapper2";


        @Bean(name = "mysqlDb1")
        @Primary
        @ConfigurationProperties(prefix = "sp.ds1")
        public DataSource dataSource1() 
            System.out.println("db1 datasource");
            return DataSourceBuilder.create().build();
        

        @Bean(name = "mysqlDb2")
        @ConfigurationProperties(prefix = "sp.ds2")
        public DataSource dataSource2() 
            System.out.println("db2 datasource");
            return  DataSourceBuilder.create().build();
        

        @Bean(name = SQL_SESSION_FACTORY_NAME_1)
        @Primary
        public SqlSessionFactory sqlSessionFactory1(@Qualifier("mysqlDb1") DataSource dataSource1) throws Exception 
            System.out.println("sqlSessionFactory1");
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setTypeHandlersPackage(MAPPERS_PACKAGE_NAME_1);
            sqlSessionFactoryBean.setDataSource(dataSource1);
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
            return sqlSessionFactory;
        

        @Bean(name = SQL_SESSION_FACTORY_NAME_2)
        public SqlSessionFactory sqlSessionFactory2(@Qualifier("mysqlDb2") DataSource dataSource2) throws Exception 
            System.out.println("sqlSessionFactory2");
            SqlSessionFactoryBean diSqlSessionFactoryBean = new SqlSessionFactoryBean();
            diSqlSessionFactoryBean.setTypeHandlersPackage(MAPPERS_PACKAGE_NAME_2);
            diSqlSessionFactoryBean.setDataSource(dataSource2);
            SqlSessionFactory sqlSessionFactory = diSqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
            return sqlSessionFactory;
        

        @Bean
        @Primary
        public MapperScannerConfigurer mapperScannerConfigurer1() 
            System.out.println("mapperScannerConfigurer1");
            MapperScannerConfigurer configurer = new MapperScannerConfigurer();
            configurer.setBasePackage(MAPPERS_PACKAGE_NAME_1);
            configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_1);
            return configurer;
        

        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer2() 
            System.out.println("mapperScannerConfigurer2");
            MapperScannerConfigurer configurer = new MapperScannerConfigurer();
            configurer.setBasePackage(MAPPERS_PACKAGE_NAME_2);
            configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_2);
            return configurer;
        



    

注意: 1)@Primary -> @primary

2)---.属性中的“jdbc-url” -> After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName

【讨论】:

【参考方案7】: 如果两个数据源位于同一数据库位置/服务器上,

@Primary 注释在用于如下方法时效果很好。

@Bean(name = "datasource1")
@ConfigurationProperties("database1.datasource")
@Primary
public DataSource dataSource()
  return DataSourceBuilder.create().build();


@Bean(name = "datasource2")
@ConfigurationProperties("database2.datasource")
public DataSource dataSource2()
  return DataSourceBuilder.create().build();

如果数据源位于不同的服务器上,最好使用@Component 和@Primary 注释。下面的代码 sn -p 在不同位置的两个不同数据源上运行良好

database1.datasource.url = jdbc:mysql://127.0.0.1:3306/db1
database1.datasource.username = root
database1.datasource.password = mysql
database1.datasource.driver-class-name=com.mysql.jdbc.Driver

database2.datasource1.url = jdbc:mysql://192.168.113.51:3306/db2
database2.datasource1.username = root
database2.datasource1.password = mysql
database2.datasource1.driver-class-name=com.mysql.jdbc.Driver

@Configuration
@Primary
@Component
@ComponentScan("com.db1.bean")
class DBConfiguration1
    @Bean("db1Ds")
    @ConfigurationProperties(prefix="database1.datasource")
    public DataSource primaryDataSource() 
        return DataSourceBuilder.create().build();
    



@Configuration
@Component
@ComponentScan("com.db2.bean")
class DBConfiguration2
    @Bean("db2Ds")
    @ConfigurationProperties(prefix="database2.datasource1")
    public DataSource primaryDataSource() 
        return DataSourceBuilder.create().build();
    


【讨论】:

【参考方案8】:

我的要求略有不同,但使用了两个数据源。

我为同一个包中的同一个 JPA 实体使用了两个数据源。一种用于在服务器启动时执行 DDL 以创建/更新表,另一种用于在运行时执行 DML。

在执行 DDL 语句后,应关闭 DDL 连接,以防止在代码中的任何位置进一步使用超级用户。

属性

spring.datasource.url=jdbc:postgresql://Host:port
ddl.user=ddluser
ddl.password=ddlpassword
dml.user=dmluser
dml.password=dmlpassword
spring.datasource.driver-class-name=org.postgresql.Driver

数据源配置类

//DDL 数据源的第一个配置类

  public class DatabaseDDLConfig 
        @Bean
        public LocalContainerEntityManagerFactoryBean ddlEntityManagerFactoryBean() 
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            PersistenceProvider persistenceProvider = new 
            org.hibernate.jpa.HibernatePersistenceProvider();
            entityManagerFactoryBean.setDataSource(ddlDataSource());
            entityManagerFactoryBean.setPackagesToScan(new String[]  
            "com.test.two.data.sources");
            HibernateJpaVendorAdapter vendorAdapter = new 
            HibernateJpaVendorAdapter();
            entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
            HashMap<String, Object> properties = new HashMap<>();
            properties.put("hibernate.dialect", 
            "org.hibernate.dialect.PostgreSQLDialect");
            properties.put("hibernate.physical_naming_strategy", 
            "org.springframework.boot.orm.jpa.hibernate.
            SpringPhysicalNamingStrategy");
            properties.put("hibernate.implicit_naming_strategy", 
            "org.springframework.boot.orm.jpa.hibernate.
            SpringImplicitNamingStrategy");
            properties.put("hibernate.hbm2ddl.auto", "update");
            entityManagerFactoryBean.setJpaPropertyMap(properties);
            entityManagerFactoryBean.setPersistenceUnitName("ddl.config");
            entityManagerFactoryBean.setPersistenceProvider(persistenceProvider);
            return entityManagerFactoryBean;
        


    @Bean
    public DataSource ddlDataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("ddl.user");
        dataSource.setPassword(env.getProperty("ddl.password"));
        return dataSource;
    

    @Bean
    public PlatformTransactionManager ddlTransactionManager() 
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(ddlEntityManagerFactoryBean().getObject());
        return transactionManager;
    

//DML 数据源的第二个配置类

public class DatabaseDMLConfig 

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean dmlEntityManagerFactoryBean() 
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        PersistenceProvider persistenceProvider = new org.hibernate.jpa.HibernatePersistenceProvider();
        entityManagerFactoryBean.setDataSource(dmlDataSource());
        entityManagerFactoryBean.setPackagesToScan(new String[]  "com.test.two.data.sources" );
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
        entityManagerFactoryBean.setJpaProperties(defineJpaProperties());
        entityManagerFactoryBean.setPersistenceUnitName("dml.config");
        entityManagerFactoryBean.setPersistenceProvider(persistenceProvider);
        return entityManagerFactoryBean;
    

    @Bean
    @Primary
    public DataSource dmlDataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(envt.getProperty("spring.datasource.url"));
        dataSource.setUsername("dml.user");
        dataSource.setPassword("dml.password");
        return dataSource;
    

    @Bean
    @Primary
    public PlatformTransactionManager dmlTransactionManager() 
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(dmlEntityManagerFactoryBean().getObject());
        return transactionManager;
    


  

//代码中DDL数据源的使用。

public class DDLServiceAtStartup 

//Import persistence unit ddl.config for ddl purpose.

@PersistenceUnit(unitName = "ddl.config")
private EntityManagerFactory entityManagerFactory;

public void executeDDLQueries() throws ContentServiceSystemError 
    try 
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.createNativeQuery("query to create/update table").executeUpdate();
        entityManager.flush();
        entityManager.getTransaction().commit();
        entityManager.close();

        //Close the ddl data source to avoid from further use in code.
        entityManagerFactory.close();
     catch(Exception ex) 

//代码中DML数据源的使用。

public class DDLServiceAtStartup 
  @PersistenceUnit(unitName = "dml.config")
  private EntityManagerFactory entityManagerFactory;

  public void createRecord(User user) 
     userDao.save(user);
  

【讨论】:

【参考方案9】:

我还必须从 Spring Boot 应用程序设置到 2 个数据源的连接,这并不容易 - Spring Boot documentation 中提到的解决方案不起作用。在互联网上进行了长时间的挖掘后,我成功了,主要想法来自this article 和其他一些地方。

以下解决方案是用 Kotlin 编写的,适用于 Spring Boot 2.1.3Hibernate Core 5.3.7。主要问题是仅仅设置不同的 DataSource 配置是不够的,还需要为两个数据库配置 EntityManagerFactoryTransactionManager

这是第一个(主)数据库的配置:

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "firstDbEntityManagerFactory",
    transactionManagerRef = "firstDbTransactionManager",
    basePackages = ["org.path.to.firstDb.domain"]
)
@EnableTransactionManagement
class FirstDbConfig 

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.firstDb")
    fun firstDbDataSource(): DataSource 
        return DataSourceBuilder.create().build()
    

    @Primary
    @Bean(name = ["firstDbEntityManagerFactory"])
    fun firstDbEntityManagerFactory(
        builder: EntityManagerFactoryBuilder,
        @Qualifier("firstDbDataSource") dataSource: DataSource
    ): LocalContainerEntityManagerFactoryBean 
        return builder
            .dataSource(dataSource)
            .packages(SomeEntity::class.java)
            .persistenceUnit("firstDb")
            // Following is the optional configuration for naming strategy
            .properties(
                singletonMap(
                    "hibernate.naming.physical-strategy",
                    "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"
                )
            )
            .build()
    

    @Primary
    @Bean(name = ["firstDbTransactionManager"])
    fun firstDbTransactionManager(
        @Qualifier("firstDbEntityManagerFactory") firstDbEntityManagerFactory: EntityManagerFactory
    ): PlatformTransactionManager 
        return JpaTransactionManager(firstDbEntityManagerFactory)
    

这是第二个数据库的配置:

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "secondDbEntityManagerFactory",
    transactionManagerRef = "secondDbTransactionManager",
    basePackages = ["org.path.to.secondDb.domain"]
)
@EnableTransactionManagement
class SecondDbConfig 

    @Bean
    @ConfigurationProperties("spring.datasource.secondDb")
    fun secondDbDataSource(): DataSource 
        return DataSourceBuilder.create().build()
    

    @Bean(name = ["secondDbEntityManagerFactory"])
    fun secondDbEntityManagerFactory(
        builder: EntityManagerFactoryBuilder,
        @Qualifier("secondDbDataSource") dataSource: DataSource
    ): LocalContainerEntityManagerFactoryBean 
        return builder
            .dataSource(dataSource)
            .packages(EntityFromSecondDb::class.java)
            .persistenceUnit("secondDb")
            .build()
    

    @Bean(name = ["secondDbTransactionManager"])
    fun secondDbTransactionManager(
        @Qualifier("secondDbEntityManagerFactory") secondDbEntityManagerFactory: EntityManagerFactory
    ): PlatformTransactionManager 
        return JpaTransactionManager(secondDbEntityManagerFactory)
    

数据源的属性如下:

spring.datasource.firstDb.jdbc-url=
spring.datasource.firstDb.username=
spring.datasource.firstDb.password=

spring.datasource.secondDb.jdbc-url=
spring.datasource.secondDb.username=
spring.datasource.secondDb.password=

属性的问题是我必须定义 jdbc-url 而不是 url 因为否则我会遇到异常。

附言 此外,您的数据库中可能有不同的命名方案,对我来说就是这种情况。由于 Hibernate 5 不支持所有以前的命名方案,我不得不使用来自 this answer 的解决方案 - 也许它也会对某人有所帮助。

【讨论】:

我在实体和表命名方面遇到了问题。它帮助了我和你的回答:mapOf("hibernate.physical_naming_strategy" to "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy", "hibernate.implicit_naming_strategy" to "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy" )

以上是关于Spring Boot 配置和使用两个数据源的主要内容,如果未能解决你的问题,请参考以下文章

如何配置两个实例mongodb使用spring boot和spring data

如何在Spring Boot和Spring Data中使用两个Cassandra数据源?

Spring Boot多数据源配置与使用

Spring Boot多数据源配置与使用

如何在 MySQL 和 MariaDB 的 Spring Boot 应用程序中配置多个(两个以上)数据源,

使用Spring Boot JPA配置多个数据源