Spring Boot Thymeleaf CRUD Database Real-Time Project - PART 1


In this PART 1, we will complete:
  1. Creating and Setup Spring Boot Project in Eclipse STS
  2. Database Setup
  3. Create JPA Entity - Employee.java

Use the below links to navigate different parts of this tutorial:

1. Spring Boot Thymeleaf CRUD Database Real-Time Project - PART 1
  • Create and Setup Spring Boot Project in Eclipse STS
  • Database Setup
7. Spring Boot Thymeleaf CRUD Database Real-Time Project - PART 7
  • Implement Sorting Feature

1. Create and Setup Spring Boot Project in Eclipse STS

1. Create a Spring Boot Project

There are many ways to create a Spring Boot application. You can refer to the articles below to create a Spring Boot application.

2. Maven Dependencies

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

3. Project Structure

2. Database Setup

Create a database with the name "demo" in the MySQL database server.
We’ll need to configure the MySQL database URL, username, and password so that Spring can establish a connection with the database on startup. 
Open application.properties and add the following MySQL database configuration:
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password=root

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
Make sure that you change the spring.datasource.username and spring.datasource.password properties as per your MySQL installation.
The spring.jpa.hibernate.ddl-auto = update property makes sure that the database tables and the domain models in your application are in sync. Whenever you change the domain model, hibernate will automatically update the mapped table in the database when you restart the application.
I have also specified the log levels for Hibernate so that we can debug the SQL queries it executes.

3. Create JPA Entity - Employee.java

package net.javaguides.springboot.model;

import jakarta.persistence.*;

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

Run Spring boot app and demo

This tutorial explained very well with demo in below video tutorial:

Use the below links to navigate different parts of this tutorial:

1. Spring Boot Thymeleaf CRUD Database Real-Time Project - PART 1
  • Create and Setup Spring Boot Project in Eclipse STS
  • Database Setup
7. Spring Boot Thymeleaf CRUD Database Real-Time Project - PART 7 // working on it
  • Implement Sorting Feature

Comments

  1. hello guy, thank you for explanation about it, it's help me alot because I'm learning Java and using Spring boot but, I have a question, in this project it's include hibernate integration? i'm sorry for that i'm new in spring =)

    ReplyDelete
    Replies
    1. Yes. we are using Hibernate to perform database operations

      Delete
  2. Can you please list down the list of dependencies you used in spring.io while creating?

    ReplyDelete

Post a Comment

Leave Comment