Featured Article

Spring Boot CrudRepository Example Masterclass Build Fast APIs

Kenneth Jul 13, 2026

Spring Boot, a popular Java-based framework for building microservices and web applications, provides a convenient way to interact with databases using Spring Data. One of its interfaces, `CrudRepository`, simplifies common database operations, making it a great starting point for CRUD (Create, Read, Update, Delete) functionality. Let's explore a practical example of using `CrudRepository` in Spring Boot.

Spring Boot + Mysql + CrudRepository Example | Java Inspires
Spring Boot + Mysql + CrudRepository Example | Java Inspires

First, ensure you have the Spring Data JPA dependency in your project. If you're using Spring Boot, it's automatically included in the 'web' or 'web-reactive' starter dependencies. Our example will use a simple 'employee' entity, but you can apply it to any domain object.

Spring Boot RESTful CRUD Example with MySQL Database - Websparrow
Spring Boot RESTful CRUD Example with MySQL Database - Websparrow

Setting up the Project

To begin, let's create a new Spring Boot project and add our 'employee' entity.

spring boot value annotation
spring boot value annotation

Build a `Employee` class:

```java public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String department; // Getters and setters } ```

Next, create a `EmployeeRepository` interface extending `CrudRepository`:

Difference between Spring Boot and Spring Cloud Framework
Difference between Spring Boot and Spring Cloud Framework

```java import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface EmployeeRepository extends CrudRepository { } ```

Basic CRUD Operations

The `CrudRepository` interface provides methods for basic CRUD operations. Let's look at each:

  • Save (Create/Update): Use the save method to create or update an employee. If the employee has an infused ID, it will update; otherwise, it creates a new record.
  • Find by ID (Read): Use the findById method to retrieve an employee by their ID.
  • Find all (Read): Use the findAll method to retrieve a list of all employees.
  • Delete: Use the deleteById or delete method to remove an employee.
25 Spring Rain Boot Planter Ideas for Your Garden Porch
25 Spring Rain Boot Planter Ideas for Your Garden Porch

Custom Queries

You can also create custom queries by annotating a method in your repository:

```java public interface EmployeeRepository extends CrudRepository { List findByNameLike(String name); } ```

This creates a dynamic query to find employees by a matching name pattern.

Top 10 Free Courses to learn Spring Framework and Spring Boot in 2024— Best of Lot
Top 10 Free Courses to learn Spring Framework and Spring Boot in 2024— Best of Lot

Integrating with a Controller

Now let's create a simple controller to use our repository methods:

watercolor painting of yellow rubber boots with flowers in them and the words hello spring
watercolor painting of yellow rubber boots with flowers in them and the words hello spring
15 Upcycled Easter Front Porch Decor For A Fresh Look
15 Upcycled Easter Front Porch Decor For A Fresh Look
Rugged Winter Hunting Boots, Nicks Boots Lace To Toe, English Hunting Boots, Viking Winter Boots, Nicks Handmade Boots, Role Club Boots, Wesco Logger Boots Size Chart, Nicks Work Boots, Nicks Boots
Rugged Winter Hunting Boots, Nicks Boots Lace To Toe, English Hunting Boots, Viking Winter Boots, Nicks Handmade Boots, Role Club Boots, Wesco Logger Boots Size Chart, Nicks Work Boots, Nicks Boots
a pair of green rain boots with flowers in front of a wooden fence and field
a pair of green rain boots with flowers in front of a wooden fence and field
rain boots template for kids to color and print on the back of a blue background
rain boots template for kids to color and print on the back of a blue background
two black rubber boots with green plants growing out of them in front of a wooden wall
two black rubber boots with green plants growing out of them in front of a wooden wall
a pair of boots with flowers in them
a pair of boots with flowers in them
Knee Length Rubber Boots, Tall Rubber Boots Outfit, Knee High Rubber Boots, Wellies And Shorts, Dirty Riding Boots, Rubber Knee High Boots, British Riding Boots History, Muddy Riding Boots, Rubber Riding Boots
Knee Length Rubber Boots, Tall Rubber Boots Outfit, Knee High Rubber Boots, Wellies And Shorts, Dirty Riding Boots, Rubber Knee High Boots, British Riding Boots History, Muddy Riding Boots, Rubber Riding Boots
boots and rivets
boots and rivets

Build a `EmployeeController` class:

```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/employees") public class EmployeeController { @Autowired private EmployeeRepository employeeRepository; // Implement CRUD endpoints using the employeeRepository methods // ... } ```

This controller demonstrates how to use the `EmployeeRepository` to create CRUD endpoints for employees.

Testing the Implementation

Write integration tests using Spring Boot's testing support to ensure your repository and controller are functioning as expected.

Ultimately, understanding and utilizing `CrudRepository` in Spring Boot equips you with a robust foundation for data access in your applications. As you become more comfortable with Spring Data, you can explore other interfaces like `JpaRepository` and `PagingAndSortingRepository` to further enhance your data manipulation skills.