Spring Boot与MySQL中的多个架构连接 [英] Spring boot connect with multiple schemas in mysql

查看:77
本文介绍了Spring Boot与MySQL中的多个架构连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法配置如何使用Spring Boot连接到多个架构. 到目前为止,使用 spring 4 XML配置,我只能将数据库URL放置为:jdbc:mysql://180.179.57.114:3306/?zeroDateTimeBehavior=convertToNull,并在实体类中指定要使用的架构,从而能够连接到多个架构.

I am not able to configure on how to connect to multiple schemas with spring boot. Until now with spring 4 and XML configuration I was able to only put the DB URL like: jdbc:mysql://180.179.57.114:3306/?zeroDateTimeBehavior=convertToNull and in the entity class specify the schema to use and thus able to connect to multiple schemas.

但是,使用Spring Boot我无法实现相同的目标. 如果在 application.properties 中,我仅指定不带模式的JDBC URL,则会产生错误:

However with Spring Boot I am not able to achieve the same. If in application.properties I only specify the JDBC URL without schema it gives error:

未选择数据库

No database selected

即使我已经在实体类中指定了架构名称. 请提出如何在Spring Boot中实现相同的目标?谢谢.

even though I have specified the schema name in entity class. Please suggest how can I achieve the same in Spring Boot? Thanks.

推荐答案

之所以说未选择数据库"是因为您在端口号后加上了正斜杠.这应该起作用...

The reason why it says "No database selected" is because you put the forward slash after the port number. This should work...

jdbc:mysql://180.179.57.114:3306?zeroDateTimeBehavior=convertToNull

我花了很多时间让Hibernate使用一个MySQL实例和多个模式.

I spent a lot of time on getting Hibernate to work with one MySQL instance and multiple schemas.

我最终将连接指定为:

jdbc:mysql://localhost:3306/schema1?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

我的实体为:

@Entity    
@Table(name="table1", schema="schema1", catalog="schema1")
public class Table1 {
   @Id
   private int id;
}

@Entity
@Table(name="table2", schema="schema2", catalog="schema2")
public class Table2 {
   @Id
   private int id;
}

显然,JDBC将MySQL模式视为目录.我在没有指定架构的情况下尝试了上述方法,但它确实有效,但是对于集成测试,我使用的是HSQL,因此我将架构保留在@Table定义中.

Apparently JDBC considers a MySQL schema as a catalog. I tried the above without specifying schema and it worked, however for integration tests I am using HSQL so I left the schema in the @Table definition.

希望这对某人有帮助.

这篇关于Spring Boot与MySQL中的多个架构连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆