java - Spring-Boot 中的多个数据库与 mariaDB

标签 java hibernate spring-boot jpa mariadb

我需要知道从多个数据库访问存储库和实体类的简单步骤。 示例:我在 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:

您可以按照此处所述配置两个数据源:

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-two-datasources

关于java - Spring-Boot 中的多个数据库与 mariaDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270332/

相关文章:

java - 如何使用 java 中的 Spring data JPA 将附件(文件)添加到 couchbase 中的文档?

java - 为什么 Thymeleaf 代码在 HTML 的头部部分不起作用,但在正文中却能正常工作

java - Spring Boot应用程序找不到webjar文件

java - 给定泛型类型参数,如何调用相应的方法?

java - Maven Ant 任务。传递 (pom) 依赖项和 https 问题

java - 我想继续要求输入,直到表单扫描仪提供有效的输入

java - 由 : org. hibernate.MappingException 引起:在以下位置找到元素的重复属性映射

java - 将自定义标识符分配给 @id 属性

java - UnsatisfiedDependencyException 尝试在 eclipse 中配置基本的 spring-boot 项目

java - 改进 Java 代码,使其在 Android 上执行得更快