• To practice creating a database schema automatically using our code snippet, you should first have a Spring Boot project available.
+- src
+- main
+- java
+- +- com
+- +- flagtick
+- +- entity
+- +- User.java
+- +- MySpringBoot.java
+- resource
+- application.properties
+- log4j2.xml
• Let's delve more deeply into each class that indicates the way to initialize entities and the database schema, which enables you to generate a database on any database management system.
» application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrud?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Use for communicating with a MySQL database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# This is one of behavior of Hibernate and automatically update the database schema based on any
# changes to the entity classes
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
» MySpringBoot.java
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MySpringBoot {
public static void main(String[] args) throws Exception {
SpringApplication.run(MySpringBoot.class, args);
}
}
» log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
</Root>
<Logger name="org.hibernate.SQL" level="debug" additivity="false">
<AppenderRef ref="ConsoleAppender" />
</Logger>
</Loggers>
</Configuration>
» User.java
package com.flagtick.entity;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Column(nullable = false)
private String email;
private String avatar;
private LocalDateTime emailVerifiedAt;
private String password;
private String userEncrypt;
private String userFirebaseId;
private String rememberToken;
private LocalDateTime expiresIn;
@Column(nullable = false)
private final Boolean isMigration = true;
@Column(nullable = false)
private final Integer status = 1;
@Column(nullable = false)
private final Boolean isEditable = true;
private final String typeOfUser = "external";
private String lang;
@CreationTimestamp
private LocalDateTime createdAt;
@UpdateTimestamp
private LocalDateTime updatedAt;
}
Note: In certain scenarios, the `@CreationTimestamp` and `@UpdateTimestamp` annotations may not function properly. If this occurs, you will need to include the necessary dependencies in `pom.xml` file.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.32.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.32.Final</version>
</dependency>
• Visit website https://projectlombok.org/features and take advantage of annotations to reduce boilerplate code in Java classes. In the context of Java entity classes, Lombok can be used to generate getter and setter methods, constructors, and other methods that are commonly used in entity classes.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
Here's an example of how you could modify the User class to use Lombok annotations:
@Entity
@Table(name = "users")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {}
• After that, you can start Spring Boot application.