Basic CRUD using Java Spring Boot

Published: (December 13, 2025 at 07:07 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

What is Spring Boot

Spring Boot is a Java framework that helps you build web and backend applications easily. It takes care of most of the setup and configuration for you, so you can focus on writing your code instead of managing complex settings.

Getting Started

  1. Install Java (JDK) on your system.
  2. Create a new Spring Boot project at the Spring Initializr. Choose a name, Java version, and add the required dependencies. Click Generate and unzip the package.
  3. Open the generated folder in an IDE (e.g., IntelliJ IDEA – download the Community edition from the official site).

Project Structure Overview

A typical Spring Boot application that handles CRUD operations follows a layered architecture:

LayerDescription
Entity / ModelJava class representing the database table (e.g., User, Product).
RepositoryInterface that interacts directly with the database (Spring Data JPA).
ServiceContains business logic and uses the repository for data operations.
ControllerReceives HTTP requests, delegates to the service, and returns responses.

CRUD Operations Mapping

OperationFunctionHTTP MethodSpring AnnotationRepository MethodPurpose
CreateInsert DataPOST@PostMappingsave(entity)Add a new record to the database.
ReadRetrieve DataGET@GetMappingfindAll(), findById(id)Fetch one or all records.
UpdateModify DataPUT@PutMappingsave(entity)Change an existing record’s data.
DeleteRemove DataDELETE@DeleteMappingdeleteById(id)Permanently remove a record.

1. Create (C)

Goal: Save a new entity (e.g., a new student) to the MySQL database.

Controller

@PostMapping("/addStudent")
public String postDetails(@RequestBody StudentEntity studentEntity) {
    System.out.println("Student entity received: " + studentEntity);
    studentService.saveDetails(studentEntity);
    return "ADDED TO TABLE SUCCESSFULLY";
}

Service

public StudentEntity saveDetails(StudentEntity studentEntity) {
    return studentRepository.save(studentEntity);
}

Result: A new row is inserted into the corresponding MySQL table.

2. Read (R)

Goal: Fetch data from the database.

Read All

@GetMapping("/getStudent")
public List getDetails() {
    return studentService.getAllDetails();
}

Read by ID

@GetMapping("/getStudent/{id}")
public StudentEntity getDetailsById(@PathVariable int id) {
    return studentService.getAllDetailsById(id);
}

Service Methods

// Get all details
public List getAllDetails() {
    return studentRepository.findAll();
}

// Get detail by ID
public StudentEntity getAllDetailsById(int id) {
    return studentRepository.findById(id).orElse(null);
}

3. Update (U)

Goal: Modify an existing entity in the database.

Controller

@PostMapping("/updateStudent")
public StudentEntity updateDetails(@RequestBody StudentEntity studentEntity) {
    return studentService.updateAllDetail(studentEntity);
}

Service

public StudentEntity updateAllDetail(StudentEntity studentEntity) {
    StudentEntity existing = studentRepository.findById(studentEntity.getId()).orElse(null);
    if (existing != null) {
        existing.setMark(studentEntity.getMark());
        existing.setName(studentEntity.getName());
        studentRepository.save(existing);
        return existing;
    }
    return null;
}

4. Delete (D)

Goal: Remove a record from the database.

Controller

@DeleteMapping("/deleteStudent/{id}")
public String deleteStudent(@PathVariable int id) {
    if (studentService.deleteStudentById(id))
        return "Deleted Successfully";
    else
        return "Can't Delete";
}

Service

public boolean deleteStudentById(int id) {
    studentRepository.deleteById(id);
    return true;
}

Result: The corresponding record is permanently removed from the MySQL table.

Configuring the Database

Create or edit the src/main/resources/application.properties file with your MySQL connection details, for example:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

These settings tell Spring Boot how to connect to MySQL and configure JPA behavior.

Back to Blog

Related posts

Read more »

Java projects of backend for beginners

!Forem Logohttps://media2.dev.to/dynamic/image/width=65,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%...