Spring boot 如何使用弹簧靴+;数据JPA在控制器中生成模式(如表、字段、列)

Spring boot 如何使用弹簧靴+;数据JPA在控制器中生成模式(如表、字段、列),spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我不想在应用程序运行时生成模式 我想知道用户何时调用控制器,然后生成模式。谢谢@Jens Schauder 我找到了你推荐的解决方案 <?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

我不想在应用程序运行时生成模式

我想知道用户何时调用控制器,然后生成模式。

谢谢@Jens Schauder 我找到了你推荐的解决方案

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  <persistence-unit name="sample" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.mydomain.User</class>
    <properties>
      <property name="javax.persistence.schema-generation.scripts.action" value="create"/>
      <property name="javax.persistence.schema-generation.scripts.create-target" value="create.ddl"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
      <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
      <property name="javax.persistence.sql-load-script-source" value="import.sql"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value=""/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/app_restaurant_1?useSSL=false"/>
    </properties>
  </persistence-unit>
</persistence>

org.hibernate.ejb.HibernatePersistence
com.mydomain.User
创建persistence.xml并打开main/resources/META-INF/persistence.xml

@GetMapping("/category")
private List<Category> getCategory(){
    Persistence.generateSchema("sample", null); // -- generate schema in persistence.xml
    return categoryService.getCategory();
}
@GetMapping(“/category”)
私有列表getCategory(){
generateSchema(“sample”,null);//--在Persistence.xml中生成模式
return categoryService.getCategory();
}

在控制器上,当在@Entity

中调用controller时,链接问题的可能重复超出了您的问题范围,但答案告诉您如何在需要时以编程方式运行模式生成。