SpringBoot 启动问题(MySQL 数据源 URL)

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

我正在尝试启动一个简单的程序,它是一个学生系统。 这些是我的包裹:

Package Tree 我会把我的课程和我的 pom 放在这一行下。

Controle.java类:

package br.com.systemvidenci.systemvidenci.controle;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import br.com.systemvidenci.systemvidenci.modelo.Estudante;

@RestController
public class Controle {
  
  @GetMapping("")
  public String mensagem(){
    return "Hello World!";
  }

  @GetMapping("/boasVindas")
  public String boasVindas(){
    return "Seja bem vindo(a)";
  }
  
  @GetMapping("/boasVindas/{nome}")
  public String boasVindas(@PathVariable String nome){
    return "Seja bem vindo(a), " + nome;
  }

  @PostMapping("/estudante")
  public Estudante estudante(@RequestBody Estudante e){
    return e;
  }


}

Estudante.java 类:

package br.com.systemvidenci.systemvidenci.modelo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="estudantes")
public class Estudante {
  @Id
  @GeneratedValue(strategy= GenerationType.IDENTITY)
  private int codigo;
  public int getCodigo() {
    return codigo;
  }
  public void setCodigo(int codigo) {
    this.codigo = codigo;
  }
  private String nome;
  private int idade;


  public String getNome() {
    return nome;
  }
  public void setNome(String nome) {
    this.nome = nome;
  }

  
  public int getIdade() {
    return idade;
  }
  public void setIdade(int idade) {
    this.idade = idade;
  }
}

Systemvidenci应用类:

package br.com.systemvidenci.systemvidenci;

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

@SpringBootApplication
public class SystemvidenciApplication {

    public static void main(String[] args) {
        SpringApplication.run(SystemvidenciApplication.class, args);
    }

}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>br.com.systemvidenci</groupId>
    <artifactId>systemvidenci</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>systemvidenci</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            </dependency>

    </dependencies>

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

</project>

Application.properties文件:

spring.jpa.hibernate.ddl-auto=update

# Acesso ao banco de dados
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/estudantes

# Usuário do banco de dados
spring.datasource.username=root

# Senha do banco de dados
spring.datasource.password=99560405

但是当我启动这个程序时,出现以下错误:


应用程序启动失败


描述:

无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。

原因:未能确定合适的司机类别

动作:

考虑以下因素: 如果您想要嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径中。 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有任何配置文件处于活动状态)。

有人知道怎么解决吗?我想学习这些基础知识,然后在我的系统上实现更好的配置,但我现在遇到了这个麻烦......

我试图将应用程序属性“spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/estudantes”更改为 spring.datasource.url=jdbc:mysql://localhost:3306/estudantes

什么也没有发生。我知道如何验证我的数据库是否处于活动状态,如果不是,我该如何解决...

java spring-boot database-connection
1个回答
0
投票

您可以尝试添加

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

到你的

application.properties
文件

© www.soinside.com 2019 - 2024. All rights reserved.