Mohamed Chiheb ben Jemaa Mohamed Chiheb ben Jemaa - 5 months ago 25
MySQL Question

spring boot data jpa mysql could not create database

I am new in spring
I will post my code,
that the application.properties

spring.datasource.url=jdbc:mysql://localhost/spring
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update


and this my entity

package model;

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

@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String phone;
private String adresse;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public Person(long id, String name, String phone, String adresse) {
super();
this.id = id;
this.name = name;
this.phone = phone;
this.adresse = adresse;
}
public Person() {
super();
}

}


and this is the repository

package repositry;



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


import model.Person;

public interface PersonRespositry extends JpaRepository<Person, Long> {


}


and my controller

package contoller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import model.Person;

import repositry.PersonRespositry;

@RestController
public class PersonController {

PersonRespositry rp;
@Autowired
public PersonController(PersonRespositry rp) {
// TODO Auto-generated constructor stub
this.rp=rp;
}


@RequestMapping("/find")
public Person find(long id){
return rp.findOne(id);
}

@RequestMapping("/findall")
public List<Person> findall(){
return rp.findAll();
}
@RequestMapping(value="/hello")
public String Demo(){
return "Hello world !!";
}
@RequestMapping(value="/create", method=RequestMethod.GET)
public String create(){
Person p=new Person();
p.setName("med");
p.setPhone("233888");
p.setAdresse("rue ");
rp.save(p);
return " success";
}


}


This is the architect of the project:



When I run the application the database does not generate and only the
localhost:8080 is running.

Answer

Your problem is the location of the Application.java.

@ComponentScanlooks for Spring beans inside the package of the class annotated with (@SpringBootApplication contains @ComponentScan) and in subpackages of this package.

I already provided an example to a very similar setup.

Please have a look here: http://stackoverflow.com/a/27983870/2576531

Furthermore the hint of Robert Moskal is correct. The database itself has to exist already. Only the tables will be created automatically.