If you're starting with Spring Boot, one of the first things you should learn is how to build a CRUD application.

CRUD is a fundamental concept in backend development. It allows applications to create, retrieve, update, and delete data.

In this article, we'll build a simple CRUD REST API using Spring Boot, Spring Data JPA, and MySQL.

By the end of this tutorial, you'll understand how these components work together to build a basic backend application.

echnologies We'll Use

Before starting, let's look at the technologies required:

Java
Spring Boot
Spring Web
Spring Data JPA
MySQL
Maven

You can create your Spring Boot project using Spring Initializr.

Add these dependencies:

Spring Web
Spring Data JPA
MySQL Driver
Lombok (Optional)

roject Structure

For a simple and maintainable application, we'll organize our code into separate packages.

src/main/java/com/example/student
│
├── controller
│ └── StudentController.java
│
├── service
│ └── StudentService.java
│
├── repository
│ └── StudentRepository.java
│
├── entity
│ └── Student.java
│
└── StudentApplication.java

Controller: Handles incoming HTTP requests.
Service: Contains business logic.
Repository: Communicates with the database.
Entity: Represents a database table.

1.Create the Student Entity

First, we need an entity to represent our Student table in the database.

@Entity @Table(name = "students") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Getters and Setters } 

Let's understand the annotations:

  • @entity – Marks the class as a JPA entity.
  • @Table – Specifies the database table name.
  • @id – Defines the primary key.
  • @GeneratedValue – Automatically generates the ID.

2.Create the Repository

Next, we need a repository to interact with the database.

@Repository public interface StudentRepository extends JpaRepository<Student, Long> { } 

@Repository marks a class or interface as the data access layer
JpaRepository provides built-in database operations

For example:

save() – Create or update a student.
findAll() – Retrieve all students.
findById() – Retrieve a student by ID.
deleteById() – Delete a student by ID.

3.Create the Service Layer

The service layer handles the application's business logic.

@Service public class StudentService { @Autowired private StudentRepository repository; public Student createStudent(Student student) { return repository.save(student); } public List<Student> getAllStudents() { return repository.findAll(); } public Student getStudentById(Long id) { return repository.findById(id) .orElseThrow(() -> new RuntimeException("Student not found")); } public Student updateStudent(Long id, Student updatedStudent) { Student student = getStudentById(id); student.setName(updatedStudent.getName()); student.setEmail(updatedStudent.getEmail()); return repository.save(student); } public void deleteStudent(Long id) { repository.deleteById(id); } 

@Autowired tells Spring: "find the matching bean and inject it here for me." You don't create the object with new; Spring creates it and hands it to you. This is called dependency injection.

Notice that we communicate with the repository through the service layer rather than directly from the controller.

  • Why is that?

Problem:

The Controller has two responsibilities:

  • -Handling HTTP requests.
  • -Managing application logic and database operations.
  • As the application grows, controllers can become complicated and difficult to maintain.

This separation makes our application easier to maintain and extend.

4.Create the REST Controller

Now it's time to expose our CRUD operations through REST API endpoints.

@RestController @RequestMapping("/api/students") public class StudentController { @Autowired private StudentService service; @PostMapping public Student createStudent(@RequestBody Student student) { return service.createStudent(student); } @GetMapping public List<Student> getAllStudents() { return service.getAllStudents(); } @GetMapping("/{id}") public Student getStudentById(@PathVariable Long id) { return service.getStudentById(id); } @PutMapping("/{id}") public Student updateStudent( @PathVariable Long id, @RequestBody Student student) { return service.updateStudent(id, student); } @DeleteMapping("/{id}") public String deleteStudent(@PathVariable Long id) { service.deleteStudent(id); return "Student deleted successfully"; } } 

Let's understand the HTTP methods:

  • @RestController - This marks a class as a controller that returns data (JSON) directly, instead of returning a web page.
  • @RequestMapping - This maps URLs to classes or methods
  • @PostMapping – Creates a new student.
  • @GetMapping – Retrieves student information.
  • @PutMapping – Updates an existing student.
  • @DeleteMapping – Deletes a student.

5.Configure the Database

Add the following configuration to your application.properties file.
application.properties is the main configuration file of a Spring Boot project

spring.datasource.url=jdbc:mysql://localhost:3306/student_db spring.datasource.username=root spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update //Hibernate auto-updates tables to match your @Entity spring.jpa.show-sql=true // Prints every SQL query Hibernate runs in the console 

Make sure your MySQL server is running and the database exists.

6. Test the REST API

Run your Spring Boot application and test the endpoints using Postman or Swagger UI.

7.Conclusion

Congratulations! You've built your first basic CRUD REST API using Spring Boot.

In this tutorial, we covered:

Creating a JPA entity.

Using Spring Data JPA repositories.

Implementing business logic in the service layer.

Creating REST API endpoints.

Connecting Spring Boot to MySQL.

Testing CRUD operations.

CRUD is one of the most important concepts in backend development. Once you understand these fundamentals, you can move on to more advanced topics such as DTOs, validation, exception handling, pagination, Spring Security, and JWT authentication.

Keep learning and keep building!

Happy Coding! 🚀