繁体   English   中英

难以将Mysql连接到Spring Boot

[英]Difficulty connecting Mysql to Spring Boot

我是Spring Boot的新手,一直在尝试将其连接到MySql工作台。 我使用web,jdbc,jpa和hibernate启动了一个新的Spring Boot项目,但是一直处于停滞状态。

有许多关于此主题的教程,但是所有这些教程都首先表明有必要进入application.properties文件并添加类似于以下内容的配置:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/mysqltutorial?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

但是,我发现的所有教程都没有讨论如何查找spring.datasource.url甚至用户名或密码。

如何找到更新这些应用程序属性的方法,以便Mysql工作台将连接到我的Spring Boot应用程序?

在您的情况下, spring.datasource.url等于jdbc:mysql://localhost:3306/mysqltutorial?useSSL=false

分解:

jdbc:mysql://localhost:3306是对在计算机上本地运行的MySQL的引用。 默认情况下,MySQL在端口3306上运行,但是如果您愿意可以更改它。 此外,如果要连接到在其他地方运行的数据库,则可以从localhost更改地址。

mysqltutorial是在MySQL中创建的数据库的名称。

?useSSL=false是连接到数据库的附加属性。

下一个,
spring.datasource.usernamespring.datasource.password是您在MySQL中用于数据库的密码和用户名。 对于小型项目,您可以使用root,尽管对于更严重的项目不建议使用root,并且应该使用不同的权限创建另一个用户。

Springboot是生成Java Config对象(如DataSource等的实例)的自动工具。

对于大多数springboot应用程序,如果在springboot主类上添加了@EnableAutoConfiguration或@SpringBootApplication批注,它将自动加载许多SpringBoot的config Java类。

在此处输入图片说明

对于您的问题,springboot将自动加载DataSourceAutoConfiguration。 在此处输入图片说明

next @EnableConfigurationProperties({DataSourceProperties.class})将从元数据类DataSourceProperties.class加载属性。

因此元数据类DataSourceProperties.class解决了您的问题。 在此处输入图片说明

暂无
暂无

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

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