Spring Boot不会生成表架构

时间:2018-11-22 11:18:46

标签: spring hibernate jpa

我是春天的新手。当我启动应用程序时,它可以很好地启动,但是不会生成表模式。我不知道我是否犯了错误或是否有配置要做

SpringBootApplication

package org.opendevup;

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

@SpringBootApplication
public class TpSpringMvcApplication {

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

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/db_sco_mvc
spring.datasource.username=******
spring.datasource.password=******
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port = 3647

实体

package org.opendevup.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
public class Etudiant implements Serializable{
    @Id
    @GeneratedValue
    private Long id;
    private String nom;
    private Date dateNaissance;
    private String email;
    private String photo;

    public Etudiant() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Etudiant(String nom, Date dateNaissance, String email, String photo) {
        super();
        this.nom = nom;
        this.dateNaissance = dateNaissance;
        this.email = email;
        this.photo = photo;
    }

    public Long getId() {
        return id;
    }

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

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public Date getDateNaissance() {
        return dateNaissance;
    }

    public void setDateNaissance(Date dateNaissance) {
        this.dateNaissance = dateNaissance;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

}

注意:我更改了Tomcat的端口,因为端口8080被另一个进程使用,并且我已经创建了数据库: db_sco_mvc

我正在使用maven进行依赖。

最诚挚的问候。

2 个答案:

答案 0 :(得分:0)

为了创建表和架构,请执行以下操作:

添加@Entity,实现Serializable接口,并为您的实体对象创建默认的和全参数的构造函数。

application.properties中,使用以下配置:

# Mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://myurl/myschema?createDatabaseIfNotExist=true
spring.datasource.username=user
spring.datasource.password=pass

# Hibernate
spring.datasource.platform=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.data.jpa.repositories.enabled=true

答案 1 :(得分:0)

还要在您的应用程序中添加以下配置:

spring.jpa.generate-ddl=true

对于ddl-auto,您可以继续使用update或尝试使用create-drop

spring.jpa.hibernate.ddl-auto = create-drop