Java JPA不创建新表(MySQL)

e0bqpujr  于 12个月前  发布在  Java
关注(0)|答案(4)|浏览(166)

我目前正在学习Sping Boot ,并尝试使用MySQL数据库设置JPA。一切看起来都很好,项目正确启动,没有可见的错误,但是. ProjectApplication.java

package com.first.project;

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

@SpringBootApplication
public class ProjectApplication {

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

}

字符串
我的学生班级:

package com.first.project.student;

import org.springframework.data.annotation.Id;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import java.time.LocalDate;

@Entity
@Table
public class Student {
  @Id
  @GeneratedValue(
          strategy = GenerationType.IDENTITY
  )
  private Long id;
  private String name;
  private String email;
  private LocalDate dateOfBirth;
  private Integer age;

  public Student() {
  }
  public Student(Long studentId, String studentName, String studentEmail, LocalDate studentDoB, Integer studentAge) {
    this.id = studentId;
    this.name = studentName;
    this.email = studentEmail;
    this.dateOfBirth = studentDoB;
    this.age = studentAge;
  }

  public Student(String studentName, String studentEmail, LocalDate studentDoB, Integer studentAge) {
    this.name = studentName;
    this.email = studentEmail;
    this.dateOfBirth = studentDoB;
    this.age = studentAge;
  }

  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 getEmail() {
    return email;
  }

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

  public LocalDate getDateOfBirth() {
    return dateOfBirth;
  }

  public void setDateOfBirth(LocalDate dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", email='" + email + '\'' +
            ", dateOfBirth=" + dateOfBirth +
            ", age=" + age +
            '}';
  }
}


application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/(my database)
spring.datasource.username=(my username)
spring.datasource.password=(my password)
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.generate-ddl=true


我的pom.xml文件中的依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>


在主包中我有ProjectApplication,还有一个子包,里面有Student类。
有什么想法吗?

f45qwnt8

f45qwnt81#

在application.properties文件中

spring.datasource.url=jdbc:mysql://localhost:3306/(my database)
spring.datasource.username=(my username)
spring.datasource.password=(my password)
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.generate-ddl=true

字符串
在这里,spring.jpa.hibernate.ddl-auto= update-drop表示应用程序在启动时创建表,并在退出时删除表。要在应用程序结束后查看表,您可以将其更改为spring.jpa.hibernate.ddl-auto=update。修改后的配置如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/(my database)
spring.datasource.username=(my username)
spring.datasource.password=(my password)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.generate-ddl=true


通过此更改,应用程序将在退出后保留这些表,以便您以后可以查看它们。

i2byvkas

i2byvkas2#

你使用的是drop-drop,所以当你启动应用程序时会创建表,当你停止时会删除表,如果你的应用程序正在运行,仍然没有显示表,那么试着这样做:检查数据库名称是否与spring数据库匹配,并试着在mysqlworkbench中刷新

wqnecbli

wqnecbli3#

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

字符串
在pom文件中尝试这个

biswetbf

biswetbf4#

看起来你的sql没有连接,确保你添加了mysql连接器jar文件,或者可以简单地在你的pom.xml文件中添加这个命令
第一个月
有时候是因为你没有相应的端口,在mysql客户端命令行中通过以下代码检查你的端口:mysql -h your_host -P your_port -u your_username -p -e“SELECT @@port;”
希望这对你有帮助!

相关问题