【问题标题】:SpringBoot doesn't create table on MySQL WorkbenchSpring Boot 不在 MySQL Workbench 上创建表
【发布时间】:2019-10-19 19:35:42
【问题描述】:

我在一个包uniroma3.siw.model 中创建了一些类,主要在另一个包uniroma3.siw.progetto 中。项目编译并且不会产生任何错误,但在 MySQL Workbench 中没有创建表。

这是我创建的实体之一:

@Entity
public class Admin {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    private String nome;

}

我没改主,这是application.proprieties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/progetto? 
serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

【问题讨论】:

  • 小注:术语 WorkBench 是一个工具,而不是一个数据库。表存在于数据库中,MySQL 是本题中的 DBMS
  • 尝试更改为 spring.jpa.hibernate.ddl-auto=create-dropspring.jpa.hibernate.ddl-auto=update 。让我们告诉我之后的结果。
  • 按照 Spring Boot 团队的最佳实践建议,将 main 移动到 uniroma3.siw

标签: java mysql spring-boot jpa mysql-workbench


【解决方案1】:

这有一些可能的原因:

  • Admin 类与您的应用程序不在同一包或子包中。解决此问题的方法是使用 @EntityScan( basePackages = {"uniroma3.siw.model"} ) 注释您的应用程序
  • application.properties 文件的位置不正确。它应该位于src/main/resources

【讨论】:

  • 非常感谢,我通过添加 @EntityScan(...) 解决了这个问题
【解决方案2】:

我找到了解决方案!这很容易。

@EntityScan( basePackages = {"it.uniroma3.siw.model"} )

在@SpringBootApplication 之后添加这一行就足够了

【讨论】:

  • 这是因为您的 main 位于不同的包中。 Spring Boot 团队建议将您的主要(@SpringBOotApplication 注释类)放在一个涵盖所有内容的 apckage 中。这样你就不必做这样的事情@EntityScan
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 2017-08-11
  • 2017-02-05
  • 2018-12-19
  • 1970-01-01
  • 2021-01-09
  • 2016-10-29
相关资源
最近更新 更多