带有 Maven 的 Springboot:无法配置数据源:'url'

问题描述 投票:0回答:0

我需要在我的 springboot 应用程序中添加一个 MySQL 数据库(使用 Maven)

然而,当我在 Eclipse 中运行项目时,我得到:

***************************
APPLICATION FAILED TO START
***************************

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

我已经检查了所有这些:

  • application.properties 中指定的数据库存在
  • application.properties中的凭据/ url是正确的
  • Mysql服务器正在运行
  • 依赖
    mysql-connector-j
    包含在pom.xml中
  • mysql-connector-j-8.031.jar
    包含在 Maven 依赖项中

我正在使用:

  • java 版本“17.0.6” 2023-01-17 LTS
  • Apache Maven 3.6.3
  • Eclipse 版本:2022-12 (4.26.0)

src/main/recources/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

logging.level.org.hibernate.type.descriptor.sql=TRACE
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.springframework=DEBUG


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <!-- The basics -->
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>MyProjectApplication</artifactId>
    <version>1.0.0</version>

    <!-- Dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
                
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
                
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>

    </dependencies>

    <!-- Build configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

MyProjectApplication.java

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication()
public class MyProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyProjectApplication.class, "--debug");
    }

}

我也是按照默认的spring结构

src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── myproject/
│   │               ├── MyProjectApplication.java
│   │               └── controllers/
│   │                   └── MyController.java
|   |               └── dao/
                    ...
                    ...  
│   ├── resources/
│   │   ├── application.properties
target/
pom.xml
spring-boot maven url datasource spring-boot-maven-plugin
© www.soinside.com 2019 - 2024. All rights reserved.