Featured Article

Spring CRUD Tutorial: Build Your First Database App in Minutes

Kenneth Jul 13, 2026

Spring, a lightweight, open-source framework for building Java-based applications, has gained significant popularity due to its simplicity and efficiency. When it comes to handling routine tasks like creating, retrieving, updating, and deleting (CRUD) data, Spring provides robust support. In this comprehensive tutorial, we will walk you through setting up a Spring CRUD operation using Spring Boot, Spring Data JPA, and an in-memory database like H2.

Spring 4 MVC REST Controller Service Example (JSON CRUD Tutorial)
Spring 4 MVC REST Controller Service Example (JSON CRUD Tutorial)

The aim of this tutorial is to provide a hands-on, step-by-step guide for beginners. By the end of this article, you will have a fully functional Spring CRUD application ready to use as a starting point for your projects. So, let's dive right in!

CRUD with React Js and Spring Boot
CRUD with React Js and Spring Boot

Setting Up the Spring CRUD Project

To start, we need to set up a new Spring Boot project. We will use Spring Initializr (start.spring.io), which initializes a project with minimal configuration and dependencies.

Spring MVC Hibernate MySQL Integration CRUD Example Tutorial | DigitalOcean
Spring MVC Hibernate MySQL Integration CRUD Example Tutorial | DigitalOcean

Select 'Web', 'Spring Data JPA', and 'H2' as dependencies. Click on 'GENERATE', download the zip file, and extract it to your desired location.

Defining the Domain Model

Spring Boot Tutorial for Beginners 9 - Spring Boot CRUD Operations using Apache Derby
Spring Boot Tutorial for Beginners 9 - Spring Boot CRUD Operations using Apache Derby

Let's create a simple 'User' entity to represent the data we will manipulate in our CRUD operations.

User Entity

In your project's package (e.g., 'com.example.demo'), create the 'User.java' file and define the 'User' entity as follows:

Spring Boot MongoDB CRUD Example
Spring Boot MongoDB CRUD Example

```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // Getters and setters } ```

User Repository

Next, we'll create a 'UserRepository.java' interface that extends 'JpaRepository<User, Long>'.

```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository { } ```

Creating the CRUD Controller

GitHub - NazarKosteckij/CourseWorkDB: Spring MVC + Hibernate + Maven tutorial with all CRUD operations
GitHub - NazarKosteckij/CourseWorkDB: Spring MVC + Hibernate + Maven tutorial with all CRUD operations

The controller is responsible for handling HTTP requests for creating, retrieving, updating, and deleting 'User' data.

UserController

an image of some logos on a computer screen
an image of some logos on a computer screen
DIY Simple spring ideas for the crafty girls.
DIY Simple spring ideas for the crafty girls.
Advanced Spring Tutorial | Spring MVC, Spring Data, JDBC & CRUD Operations
Advanced Spring Tutorial | Spring MVC, Spring Data, JDBC & CRUD Operations
Java Spring Boot
Java Spring Boot
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
journalling tutorial
journalling tutorial
How To Develop REST CRUD API Using Spring Boot ? - JavaTechOnline
How To Develop REST CRUD API Using Spring Boot ? - JavaTechOnline
the spring boot diagram shows how to get an employee using spring boot
the spring boot diagram shows how to get an employee using spring boot
Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets
Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets

Create a 'UserController.java' file and implement the CRUD methods as follows:

```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public List getAllUsers() { return userRepository.findAll(); } @PostMapping public User createUser(@RequestBody User user) { return userRepository.save(user); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); return userRepository.save(user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userRepository.deleteById(id); } } ```

Now, you have a fully functional Spring CRUD application ready for testing. You can use tools like Postman or curl to send HTTP requests to your API endpoints.

Testing the Spring CRUD API

Test each endpoint to create, retrieve, update, and delete 'User' data. Ensure that the operations are working as expected.

By following this tutorial, you now have a solid understanding of how to implement CRUD operations using Spring Boot, Spring Data JPA, and H2. This knowledge will serve as an excellent foundation for building more complex applications.

Happy coding! Remember, the best way to learn is by doing. Start with this project and explore more features and improvements as your skills grow.