我需要知道从多个数据库访问存储库和实体类的简单步骤。 示例:我在 DB1 中有 User 表,在 DB2 中有 Email 表,必须通过单个服务请求进行访问。
数据库1
spring.datasource.url = jdbc:mysql://localhost:3306/dbName1?useSSL=false
spring.datasource.username = user
spring.datasource.password = password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
数据库2
spring.datasource.url = jdbc:mysql://localhost:3306/dbName2?useSSL=false
spring.datasource.username = user
spring.datasource.password = password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
其他属性
spring.jpa.properties.hibernate.jdbc.batch_size=20
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
最佳答案
解决方案 1:
您可以创建一个有权访问这两个数据库的用户,然后在查询外部表时使用完全限定的表名称。
MySQL 支持 dbname.tablename 语法来访问当前数据库范围之外的表。
这要求当前连接的用户具有从另一个物理数据库中的请求表中读取数据的适当权限。
解决方案 2:
您可以按照此处所述配置两个数据源:
关于java - Spring-Boot 中的多个数据库与 mariaDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270332/