要创建表,您需要将以下行插入到 application.properties -
spring.jpa.hibernate.ddl-auto=update
这里,Hibernate 将自动创建表 demo88。 application.properties代码如下 -
spring.datasource.platform=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
server.port=8191
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sampledatabase
spring.datasource.username=root
spring.datasource.password=123456
demo88实体类创建表格列如下 -
示例
package com.automaticallytablecreation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class demo88 {
@Id
private int id;
@Column(name="name")
private String name;
}
主类代码如下 -
示例
package com.automaticallytablecreation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AutomaticTableApplication {
public static void main(String[] args) {
SpringApplication.run(AutomaticTableApplication.class, args);
}
}
现在右键单击主类,然后单击“作为 Java 应用程序运行”菜单。应用程序将运行并创建表。
这将产生以下输出 -
Output
为了验证,让我们显示表格 -
mysql> show create table demo88;
这将产生以下输出 -
输出
+--------+------------------------------------------------------------+| Table | Create Table |
+--------+------------------------------------------------------------+| demo88 | CREATE TABLE `demo88` (
`id` int NOT NULL, `name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-------------------------------------------------------------+1 row in set (0.03 sec)