有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Spring在数据库中创建表

有一个web应用程序使用SpringBoot和JpaRepository接口来执行数据库查询。我希望应用程序能够在启动时自己在数据库中创建表。如何做到这一点?也许我需要用Hibernate或者其他什么来做这个?没有xml


共 (3) 个答案

  1. # 1 楼答案

    如果你想使用MySQL,请使用前面的答案,如果需要使用postgres,请使用下面给出的代码

    spring.datasource.platform=postgres
    spring.datasource.url=jdbc:postgresql://localhost:5432/schema_name
    spring.datasource.username=postgres
    spring.datasource.password=postgres
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect
    
  2. # 2 楼答案

    您可以将Liquibase与Spring Boot一起使用(参见docs)。启动时可以创建和更新数据库表

  3. # 3 楼答案

    您需要将JPA配置添加到应用程序中。属性文件。MySQL的示例如下:

    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=test
    spring.datasource.password=
    

    你可以看到一些解释。 另外,请阅读关于spring boot和JPA here