CRUD API Using Hibernate

About

Building a CRUD API Using Hibernate with Spring Boot

Maven Dependencies

Add these dependencies in our pom.xml:

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Starter for JPA (includes Hibernate) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- MySQL Connector (or any DB you use) -->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <!-- Optional: Lombok to reduce boilerplate -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Application Properties File (Hibernate + DB Settings)

Entity Class

Employee.java

Repository Interface

Extending JpaRepository provides ready-to-use methods like save(), findById(), findAll(), deleteById(), etc.

Service Layer

Service Interface:

Service Implementation:

Controller Layer (REST API)

Main Application Class

Testing the API

Operation
HTTP Method
URL
Body (JSON)

Create Employee

POST

/api/employees

{ "firstName": "John", "lastName": "Doe", "email": "[email protected]" }

Get Employee by ID

GET

/api/employees/{id}

Get All Employees

GET

/api/employees

Update Employee

PUT

/api/employees/{id}

{ "firstName": "Jane", "lastName": "Doe", "email": "[email protected]" }

Delete Employee

DELETE

/api/employees/{id}

Last updated