springBoot não cria tabelas no MySql

MySQL

Java

Spring Boot

24/03/2023

Olá pessoal!
Tenho um projeto em SpringBoot conectando em um banco MySql 8.0.31 (WampServer 3.3.0).
O projeto estava apresentando o seguinte erro:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-03-24T13:16:52.572-03:00 ERROR 5192 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : 

***************************
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


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).


A partir daí eu inseri a seguinte anotação na minha classe “application”:
@SpringBootApplication (exclude = {DataSourceAutoConfiguration.class }) e parou de apresentar o erro, mas não está criando as tabelas no banco.
Será que pode ter relação dessa anotação com a não criação das tabelas no banco?

Meu application.properties:
#Configurações do JPA:

spring.datasource.url=jdbc:mysql://localhost:3306/minas_cafe?createDatabaseIfNotExist=true

spring.datasource.username=root

spring.datasource.password=359423

spring.jpa.generate-ddl=true

spring.jpa.hibernate.ddl-auto=validate

#Porta de trabalho do Tomcat

server.port=${port:8080}

#Exibe os comandos SQL

spring.jpa.show-sql=true

#Especifica qual o banco de dados você usará

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

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

#Enable spring data repos

spring.data.jpa.repositories.enabled=true

spring.jpa.database=mysql


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>3.0.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cafe</groupId>
	<artifactId>Minas</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Minas</name>
	<description>Projeto de Automatização de Processos de Com. e Prest. Serviços Minas Café</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.6.15.Final</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</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>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</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>


Minha classe application:
package com.cafe.api;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

//Desabilitando a segurança do SpringBoot - Informa para o spring-boot excluir/desativar a autoconfiguração de DataSource
@SpringBootApplication (exclude = {DataSourceAutoConfiguration.class }/*, scanBasePackages= {"com.minascafe.api.repositories"}*/)
//@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) //tenta configurar automaticamente seu aplicativo Spring com base nas dependências jar que você adicionou
//E exclui o carregamento do bean da fonte de dados

//@EntityScan(basePackages = {"com.minascafe.api.entities"}) //Onde estão sendo criadas as classes Java mapeadas com @Entity
//@ComponentScan(basePackages = {"com.minascafe.api.repositories"})
// //@EnableJpaRepositories(basePackages = {"com.minascafe.api.repositories"})

public class MinasApplication {

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

	   //gera um componente do Spring

	   @Bean
		 public CommandLineRunner commandLineRunner() { //Executa automaticamente junto com o Spring
			
	        return args ->{

	            System.out.println("Executando o primeiro teste do projeto perfeitamente!");

	     };		    
	  }

}

Alguém poderia me ajudar?
Edson

Edson

Curtidas 0

Respostas

Gustavo Nascimento

Gustavo Nascimento

24/03/2023

Tenta utilizar a dependência no arquivo POM
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.27</version> <!-- Use a versão correspondente ao seu MySQL -->
</dependency>
GOSTEI 0
POSTAR