7. Create MySQL Schema & Entity Class in Spring Boot | Auto-Create Table on Run

The following demonstrates how to create a MySQL schema using MySQL Workbench.

The next step shows how to create an entity class inside the entity package.

The entity class is annotated with @Entity and @Table(name=""), while the primary key field is annotated with @Id and @GeneratedValue(strategy = GenerationType.IDENTITY).

After running the application, the corresponding table is created in the database as defined.

Below is the implementation of the Book.java entity class.

package com.codersstash.book_details.entity;

import jakarta.persistence.*;

@Entity
@Table(name="book")
public class Book {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer book_id;
    private String book_name;
    private String book_author;
    private String book_publisher;
    private String book_description;
    private Double book_price;

    public Integer getBook_id() {
        return book_id;
    }

    public void setBook_id(Integer book_id) {
        this.book_id = book_id;
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getBook_author() {
        return book_author;
    }

    public void setBook_author(String book_author) {
        this.book_author = book_author;
    }

    public String getBook_publisher() {
        return book_publisher;
    }

    public void setBook_publisher(String book_publisher) {
        this.book_publisher = book_publisher;
    }

    public String getBook_description() {
        return book_description;
    }

    public void setBook_description(String book_description) {
        this.book_description = book_description;
    }

    public Double getBook_price() {
        return book_price;
    }

    public void setBook_price(Double book_price) {
        this.book_price = book_price;
    }

}

You can find the related GitHub repository branch here : book-details-entity

Leave a Reply

Your email address will not be published. Required fields are marked *