简体   繁体   English

将Spring JDBC创建的模式设置为默认模式

[英]Make schema created by Spring JDBC the default schema

I'm using Spring Boot and JDBC for my database connection. 我正在使用Spring Boot和JDBC进行数据库连接。 I placed schema.sql at the classpath to initialize a schema and tables. 我将schema.sql放在类路径中以初始化模式和表。

Because the schema doesn't exist yet while connecting to the datasource, I have to configure the datasource in application.properties like so: 因为连接到数据源时还不存在该模式,所以我必须在application.properties中配置数据源,如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/

schema.sql: schema.sql:

CREATE DATABASE IF NOT EXISTS <schema_name>
USE <schema.name>;

CREATE TABLE...

So I select the schema after creating it. 所以我在创建架构后选择架构。 This obviously doesn't persist for too long. 这显然不会持续太久。

How do I configure this properly? 如何正确配置? Is there a way to select a default schema after the create script or maybe change the datasource url? 有没有一种方法可以在创建脚本之后选择默认架构,或者更改数据源URL?

With JDBC you need to use Connection.setCatalog to switch between databases. 使用JDBC,您需要使用Connection.setCatalog在数据库之间进行切换。 You should not use USE <databasename> as the JDBC driver itself needs to be aware of which database it is operating on. 您不应使用USE <databasename>因为JDBC驱动程序本身需要知道它在哪个数据库上运行。

Based on your code from the schema.sql 基于来自schema.sql的代码

USE <schema.name>;

This will not work for your java environment. 这不适用于您的Java环境。 The schema.sql will be executed and finished, which will not cater your requirement to set the default schema. schema.sql将被执行并完成,不会满足您设置默认架构的要求。 The General approach will be to use JDBC URL as; 通用方法是将JDBC URL用作:

jdbc:mysql://localhost:3306/DB_NAME

This will set the default db as DB_NAME. 这会将默认数据库设置为DB_NAME。 Assumptions: I am assuming that you want to connect to single node DB without any loadbalancer or failover mechanism to be used. 假设:我假设您要连接到单节点DB,而无需使用任何负载平衡器或故障转移机制。 URL may change based on these features to be configured. URL可能会根据要配置的这些功能而更改。

If you are not specifying the DB_NAME in the URL, it means their is no default schema. 如果未在URL中指定DB_NAME,则表示它们不是默认架构。 You have 2 options to access the DB in that case. 在这种情况下,您有2个选项可以访问数据库。

1) Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement. 1)始终使用Connection.setCatalog()方法在JDBC应用程序中指定所需的数据库,而不是USE数据库语句。

2) fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename ...) in your SQL. 2)在SQL中使用数据库名称(即SELECT dbname.tablename.colname FROM dbname.tablename ...)完全指定表名称。 Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers. 通常,仅在构建可与多个数据库一起使用的工具(例如GUI数据库管理器)时,在不指定要使用的数据库的情况下打开连接才有用。

For more details refer to the below mysql portal for reference. 有关更多详细信息,请参考下面的mysql门户以供参考。

https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html https://dev.mysql.com/doc/connector-j/zh-CN/connector-j-reference-configuration-properties.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM