I'm trying my first Spring Boot application. And when I try to connect my sql database, it shows the application failed to connect to the database.
我正在尝试我的第一个Spring Boot应用程序。当我尝试连接我的SQL数据库时,它显示应用程序无法连接到数据库。
Here's the error message:
以下是错误消息:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
my yml file:
我的YML文件:
spring:
datasource:
# type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false
username: root
password:
#testWhileIdle: true
#validationQuery: SELECT 1
#jpa:
#database: mysql
#database-platform: org.hibernate.dialect.MySQLDialect
my pom.xml file:
我的pom.xml文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- myBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- junit test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>
</plugins>
</build>
Anyone knows why?
有人知道为什么吗?
Thanks!!
谢谢!!
I tried to edit my paplication.yml file and change datasource property, but it does't work.
我试图编辑我的paplication.yml文件并更改数据源属性,但不起作用。
更多回答
Hi there, what IDE are you using? And did you make DB connection in IDE? Usually when you connect to Datasource with IDE is quite easy, in case of IntelliJ IDEA it configures the DB Driver automatically.
你好,你用的是什么IDE?你在IDE中建立数据库连接了吗?通常,当您使用IDE连接到数据源时非常容易,在IntelliJ的情况下,它会自动配置DB驱动程序。
I'm using Intellij IDEA and Maven.
我用的是IntelliJ IDEA和Maven。
have you tried to change driver-class-name: com.mysql.cj.jdbc.Driver
to driverClassName: com.mysql.cj.jdbc.Driver
yet?
您是否已尝试将驱动程序类名称:com.mysql.cj.jdbc.Driver更改为driverClassName:com.mysql.cj.jdbc.Driver?
优秀答案推荐
I think change name paplication.yml to application.yml file or introduce the new configuration file to Spring.
我认为将名称paplication.yml更改为Application.yml文件,或者将新的配置文件引入到Spring。
更多回答
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。
我是一名优秀的程序员,十分优秀!