Home > Database > Mysql Tutorial > Create a table using MySQL - Hibernate

Create a table using MySQL - Hibernate

WBOY
Release: 2023-09-05 17:37:01
forward
1213 people have browsed it

To create the table, you need to insert the following lines into application.properties -

spring.jpa.hibernate.ddl-auto=update
Copy after login

Here, Hibernate will automatically create the table demo88. The application.properties code is as follows-

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
Copy after login

demo88 entity class creation table column is as follows-

Example

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;
}
Copy after login

The main class code is as follows-

Example

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);
   }
}
Copy after login

Now right click on the main class and click on the "Run as Java Application" menu. The application will run and create the table.

This will produce the following output-

Output使用 MySQL 创建表 - Hibernate

For verification, let us display the table-

mysql> show create table demo88;
Copy after login

This will produce the following output-

Output

+--------+------------------------------------------------------------+| 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)
Copy after login

The above is the detailed content of Create a table using MySQL - Hibernate. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template