Spring Boot 3, MySQL, Spring Data JPA, Hibernate CRUD REST API Tutorial [2024]

In this tutorial, we will learn how to build CRUD RESTful API using Spring Boot 3, Spring Data JPA (Hibernate), and MySQL database.

CRUD stands for "create, read, update, and delete," which are the four basic functions of persistent storage. Spring Boot is a Java-based framework used to build web applications and RESTful APIs. Together, Spring Boot and CRUD can be used to quickly develop a RESTful API that can create, read, update, and delete data in a database.

Learn complete Spring boot at Learn and Master Spring Boot

Spring Boot Project Architecture

We are going to use three-layer architecture in our Spring boot project:

Tools and Technologies Used:

  • Spring Boot 3.0
  • Java 17
  • Spring Data JPA
  • Hibernate
  • MySQL Database
  • Maven
  • Postman

1. Create and Setup Spring Boot Project in IntelliJ

Spring Boot provides a web tool called Spring Initializer to create and bootstrap Spring boot applications quickly. Just go to https://start.spring.io/ and generate a new spring boot project.

Refer to this screenshot to enter the details to create the Spring Boot project:
spring boot restful webservices

Here is the pom.xml file for your reference:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot Restful Webservices</description>




Project Structure

Refer to this screenshot to create a project structure:
Refer to this screenshot to enter the details to create the Spring Boot project:

Configure MySQL Database

Let's first create a database, go to MySQL workbench, and use the below SQL query to create a new database:
create database user_management
Next, open the application.properties file and add the following properties to it.



Make sure that you change the spring.datasource.username & spring.datasource.password properties as per your MySQL installation.

In the above properties file, the last two properties are for Hibernate. Spring Boot uses Hibernate as the default JPA implementation.

The property spring.jpa.hibernate.ddl-auto is used for database initialization. I’ve used the value “update” for this property to create the database tables automatically.

Create User JPA Entity

An Entity is a plain old Java object (POJO) that represents the data you want to store. You will need to annotate the class with @Entity and define the fields of the class along with the getters and setters for each field.

Let's create a User class and add the following content to it:
package net.javaguides.springboot.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Table(name = "users")
public class User {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false)
    private String firstName;
    @Column(nullable = false)
    private String lastName;
    @Column(nullable = false, unique = true)
    private String email;
Note that we are using Lombok annotations to reduce the boilerplate code (getters/setters).

Create Spring Data JPA Repository - UserRepository

A repository is an interface that defines the methods for performing CRUD operations on the Entity. Spring Data JPA will automatically create the implementation for the Repository interface.

Let's create a UserRepository interface that extends JpaRepository:
package net.javaguides.springboot.repository;

import net.javaguides.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

Note that we do not need to add @Repository annotation because Spring Data JPA internally takes care of it.

Create Service Layer

This layer will contain the business logic for the API and will be used to perform CRUD operations using the Repository.

Let's implement the service layer, we will first create an interface and then its implementation class.

Service Interface

Let's create an UserService interface and declare the following CRUD methods:

package net.javaguides.springboot.service;

import net.javaguides.springboot.entity.User;

import java.util.List;

public interface UserService {
    User createUser(User user);

    User getUserById(Long userId);

    List<User> getAllUsers();

    User updateUser(User user);

    void deleteUser(Long userId);


Let's create UserServiceImpl class that implements UserService interface methods:

package net.javaguides.springboot.service.impl;

import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.User;
import net.javaguides.springboot.repository.UserRepository;
import net.javaguides.springboot.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class UserServiceImpl implements UserService {

    private UserRepository userRepository;

    public User createUser(User user) {
        return userRepository.save(user);

    public User getUserById(Long userId) {
        Optional<User> optionalUser = userRepository.findById(userId);
        return optionalUser.get();

    public List<User> getAllUsers() {
        return userRepository.findAll();

    public User updateUser(User user) {
        User existingUser = userRepository.findById(user.getId()).get();
        User updatedUser = userRepository.save(existingUser);
        return updatedUser;

    public void deleteUser(Long userId) {

Create Controller Layer - UserController

We’ll now create the REST APIs for creating, retrieving, updating, and deleting a User resource.

Let's create UserController class and let's build CRUD REST APIs for the User resource:
package net.javaguides.springboot.controller;

import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.User;
import net.javaguides.springboot.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class UserController {

    private UserService userService;

    // build create User REST API
    public ResponseEntity<User> createUser(@RequestBody User user){
        User savedUser = userService.createUser(user);
        return new ResponseEntity<>(savedUser, HttpStatus.CREATED);

    // build get user by id REST API
    // http://localhost:8080/api/users/1
    public ResponseEntity<User> getUserById(@PathVariable("id") Long userId){
        User user = userService.getUserById(userId);
        return new ResponseEntity<>(user, HttpStatus.OK);

    // Build Get All Users REST API
    // http://localhost:8080/api/users
    public ResponseEntity<List<User>> getAllUsers(){
        List<User> users = userService.getAllUsers();
        return new ResponseEntity<>(users, HttpStatus.OK);

    // Build Update User REST API
    // http://localhost:8080/api/users/1
    public ResponseEntity<User> updateUser(@PathVariable("id") Long userId,
                                           @RequestBody User user){
        User updatedUser = userService.updateUser(user);
        return new ResponseEntity<>(updatedUser, HttpStatus.OK);

    // Build Delete User REST API
    public ResponseEntity<String> deleteUser(@PathVariable("id") Long userId){
        return new ResponseEntity<>("User successfully deleted!", HttpStatus.OK);

Running Spring Boot Application

Run the spring boot application from IDE:

From your IDE, run the SpringbootRestfulWebservicesApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.

Run the spring boot application using the command line:

Just go to the root directory of the application and type the following command to run it -
$ mvn spring-boot:run

The application will start at Spring Boot’s default tomcat port 8080.

Test CRUD RESTful WebServices using Postman Client

Create User REST API:

Request Body:
    "firstName": "ramesh",
    "email": "[email protected]"
Refer to this screenshot to test Create User REST API:

Get User REST API:

HTTP Method: GET

Refer to this screenshot to test GET User REST API:

Update User REST API:

HTTP Method: PUT
Request Body:
    "firstName": "ram",
    "email": "[email protected]"
Refer to this screenshot to test the Update User REST API:

Get All Users REST API:

HTTP Method: GET

Refer to this screenshot to test GET All User REST API:



Refer to this screenshot to test Delete User REST API:

Source Code on GitHub

The source code of this tutorial is available on my GitHub repository at Spring Boot CRUD RESTful WebServices


In this tutorial, we have created a Spring boot project and built CRUD RESTful Webservices using Spring Boot 3, Spring Data JPA (Hibernate), and MySQL database.

Further Reading