【问题标题】:Why is my database table not generated automatically from STS?为什么我的数据库表没有从 STS 自动生成?
【发布时间】:2021-04-15 11:58:11
【问题描述】:

我正在开发一个 Spring Boot 应用程序。我正在尝试将我的数据库表自动生成到我的数据库中。

我想我遗漏了一些东西,但我还想不通,所以请你帮我理解为什么我的数据库是空的,即使我已经创建了数据库但没有生成表。

我正在使用 STS 4、Spring Boot 2.4.4 和 WAMP (MySQL 5.7.31) 和 Java 11。

我创建了项目,并在创建时添加了 MySQL Driver、Spring Data JPA 和 Spring Web 依赖项。

这是我的 pom.xml

    <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.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>
            </plugin>
        </plugins>
    </build>

</project>

这是我的 application.properties 文件

server.port=8081
### DATABASE ### 
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bike?useUnicode=true&useJD BCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC 
spring.datasource.username=root
spring.datasource.password=
### JPA / HIBERNATE ###
spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect


#logging configuration
logging.file.name=C:\logs\springboot.log
logging.level.root=INFO
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %-5level - %logger{36} - %msg%n 

我已经创建了一个名为 Bike 的空数据库,并创建了实体类和存储库:

@Entity
@Table(name = "bike")
public class Bike implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    
    @Column(name = "name")
    private String name;
    @Column(name = "email")
    private String email;
    @Column(name = "phone")
    private String phone;
    @Column(name = "model")
    private String model;
    @Column(name = "serialNumber")
    private String serialNumber;
    @Column(name = "purchasePrice")
    private BigDecimal purchasePrice;
    @Column(name = "purchaseDate")
    private Date purchaseDate;
    @Column(name = "contact")
    private boolean contact;
}

它的存储库:

@Repository
public interface BikeRepository extends CrudRepository<Bike, Integer> {
    
}

这是控制台中的执行

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.4)

2021-04-15 13:50:17 - INFO  - c.e.d.SpringAngularDemoApplication - Starting SpringAngularDemoApplication using Java 11.0.10 on DESKTOP-F9N36EA with PID 3088 (C:\Users\hp compact\Documents\Dev_Tools\Spring_STS\STS_WorkSpace\Spring_Angular_Demo\target\classes started by hp compact in C:\Users\hp compact\Documents\Dev_Tools\Spring_STS\STS_WorkSpace\Spring_Angular_Demo)
2021-04-15 13:50:17 - INFO  - c.e.d.SpringAngularDemoApplication - No active profile set, falling back to default profiles: default
2021-04-15 13:50:18 - INFO  - o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-15 13:50:18 - INFO  - o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 4 ms. Found 0 JPA repository interfaces.
2021-04-15 13:50:19 - INFO  - o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8081 (http)
2021-04-15 13:50:19 - INFO  - o.a.catalina.core.StandardService - Starting service [Tomcat]
2021-04-15 13:50:19 - INFO  - o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-15 13:50:19 - INFO  - o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2021-04-15 13:50:19 - INFO  - o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1719 ms
2021-04-15 13:50:19 - INFO  - o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-15 13:50:19 - INFO  - org.hibernate.Version - HHH000412: Hibernate ORM core version 5.4.29.Final
2021-04-15 13:50:19 - INFO  - o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-04-15 13:50:19 - INFO  - com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2021-04-15 13:50:20 - INFO  - com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2021-04-15 13:50:20 - INFO  - org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2021-04-15 13:50:20 - INFO  - o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-04-15 13:50:20 - INFO  - o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-04-15 13:50:20 - WARN  - o.s.b.a.o.j.JpaBaseConfiguration$JpaWebConfiguration - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-04-15 13:50:20 - INFO  - o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
2021-04-15 13:50:20 - INFO  - o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8081 (http) with context path ''
2021-04-15 13:50:20 - INFO  - c.e.d.SpringAngularDemoApplication - Started SpringAngularDemoApplication in 3.716 seconds (JVM running for 4.091)

【问题讨论】:

  • 它一定是在创建应用程序根本无法启动的其他东西。因此,要么你没有查看你认为正在查看的数据库,要么应用程序没有使用你认为它使用的数据库。
  • 好吧,我重新安装了 wamp 服务器 bcz 我认为这可能是问题的原因,我只在其中创建了一个新数据库......所以我不会混淆数据库名称在 mysql 中是“自行车”,我在 application.properties 中使用相同的名称。
  • 嗯,它有一些东西。如果数据库不会被创建,应用程序将不会启动,它会启动,因此数据库有一个表/模式。因此,您或应用程序正在寻找其他东西。您可以将根记录器设置为 DEBUG 并查看是否获得更多信息(您也可以执行 TRACE,但这会淹没您的日志记录)。
  • 好吧,我将实体类移动到与包含 @EnableAutoConfiguration 注释的主类相同的包中,并且该表是在数据库中创建的,但这很奇怪,这不是我第一次创建 Spring Boot 应用程序....即使我的旧项目也可以完美地工作,而无需将实体的类与主类放在同一个包中。我曾经使用这个层次结构:> src/main/java -com.example.spring(主类放在这里)-com.example.spring.model(实体类放在这里)
  • 我解决了这个问题,是我错误地创建了子包。

标签: java mysql spring hibernate jpa


【解决方案1】:

我认为这是因为如果您设置表的名称,您会明确表明具有该名称的表存在。尝试删除注释@Table(name="bike") 看看会发生什么。我认为它会创建与您的班级“Bike”同名的表格。另一件事,当你的表列名与你在java中声明的属性相同时,你不必指定@Column(name = " ")注解

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2020-11-24
    相关资源
    最近更新 更多