无法使用 Spring Boot 和 JPA 在数据库中创建 table

Unable to create table in database using Spring Boot and JPA

当我使用 Spring 引导应用程序和 JPA 在名为 schooldb 的数据库中创建 table 时,代码运行正常,但我无法自动创建 table 并且必须在 IDE.

中使用 CMD 或任何其他终端显式创建它

这是我的存储库class

package com.jpabasics.jpaBasics.enitity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {

    @Id
     private long studentId;
     private String firstName;
     private String lastName;
     private String emailId;
     private String guardianName;
     private String guardianEmail;
     private String guardianMobile;


}

这是我的 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.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jpabasics</groupId>
    <artifactId>jpaBasics</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jpaBasics</name>
    <description>Jpa basics</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这也是我的 application.properties 文件

# Database Config : mysql
spring.datasource.url = jdbc:mysql://localhost:3306/schooldb
spring.datasource.username = root
spring.datasource.password = Lko@6388895330
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

# Hibernate Configuration
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl.auto = update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect

有人可以帮我解决这个问题吗?

您缺少在数据库中构建 table 以托管实体所需的 @table 注释。此外,注释您的列也是一个很好的习惯,以确保它们被正确命名(尽管在您的情况下,它们会被正确解析)。例如:

package com.jpabasics.jpaBasics.enitity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Entity
@Table(name="STUDENTS")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {

     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "STUDENT_ID")
     private long studentId;

     @Column(name = "FIRST_NAME")
     private String firstName;

     @Column(name = "LAST_NAME")
     private String lastName;

     @Column(name = "EMAIL_ID")
     private String emailId;

     @Column(name = "GUARDIAN_NAME")
     private String guardianName;

     @Column(name = "GUARDIAN_EMAIL")
     private String guardianEmail;

     @Column(name = "GUARDIAN_MOBILE")
     private String guardianMobile;

}