spring mysql双主架构,Spring Boot与MySQL中的多个架构连接

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.

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

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

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

I ended up specifying my connection as:

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

And my entities as:

@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;

}

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.

Hopefully this helps someone.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值