Error connect Mysql with SpringBoot

0

I am trying to access data from the Mysql Database with SpringBoot: But I get an error, I've never accessed, so I'm totally new, I'm following tutorials (5-6), none has worked.

The idea is to put an address in the browser and return the list of users.

We started: pom.xml

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</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-starter-data-rest</artifactId>
            </dependency>

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

             <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                 <version>6.0.5</version>
               <scope>runtime</scope>
               </dependency>
    </dependencies>

interface

package prueba;

import org.springframework.data.jpa.repository.JpaRepository;

public interface RepositorioUsuariosDB extends JpaRepository<Usuario, Long> {

}

Entity / Class

package prueba;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Usuario {
 @Id
 private Long id;
 private String nombre;
 private String password;

 public Usuario() {}

 public Usuario(String nombre, String password ) {
     this.nombre = nombre;
     this.password = password;
 }

 public Long getId() {
     return id;
 }

 public void setId(Long id) {
     this.id = id;
 }

 public String getNombre() {
     return nombre;
 }

 public void setNombre(String nombre) {
     this.nombre = nombre;
 }


 public String getPassword() {
     return password;
 }

 public void setPassword(String password) {
     this.password = password;
 }

 @Override
 public String toString() {
     return "Usuario{" +
             "id=" + id +
             ", nombre='" + nombre + '\''+
             '}';
 }
}

Settings

spring.datasource.url= jdbc:mysql://localhost:8080/localservice?useSSL=false
spring.datasource.username= miname
spring.datasource.password= mipass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 
logging.level.org.hibernate.SQL=debug

Error code

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-06-29 11:01:06.260 ERROR 12404 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Parameter 0 of constructor in org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.
http://ledze.mx/index.php/10-spring-boot/restful/12-spring-boot-datarest-conexion-con-mysql-y-manejo-con-jpa

I was following that tutorial, but I modified 2 things:

The fields in the DB I have put them as in the Entity "id, name, password" so in the interface I have not put the findEmail.

At the moment it is to collect data, see how it works , information transfer, and above all, learn well.

link Unable to sign in

I update with the new dependencies and the new error code

    
asked by EduBw 29.06.2018 в 10:37
source

1 answer

2

In your pom.xml you have:

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>RELEASE</version>
      <scope>runtime</scope>
    </dependency>

This loads the latest Release version of the mysql-connector-java library in your classpath when running the program via maven.

In the configuration you have

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

But looking at mysql-connector-java , explains that in that jar the driver is called com.mysql.cj.jdbc.Driver . According to this changelog , the class was renamed in passing to version 8.0 of the jar.

Probably the example was tested before the release of version 8.0 and now it does not work anymore.

The solution should be to change the configuration:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
answered by 29.06.2018 в 11:50