Basic CRUD using Java Spring Boot
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
- Install Java (JDK) on your system.
- 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.
- 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:
| Layer | Description |
|---|---|
| Entity / Model | Java class representing the database table (e.g., User, Product). |
| Repository | Interface that interacts directly with the database (Spring Data JPA). |
| Service | Contains business logic and uses the repository for data operations. |
| Controller | Receives HTTP requests, delegates to the service, and returns responses. |
CRUD Operations Mapping
| Operation | Function | HTTP Method | Spring Annotation | Repository Method | Purpose |
|---|---|---|---|---|---|
| Create | Insert Data | POST | @PostMapping | save(entity) | Add a new record to the database. |
| Read | Retrieve Data | GET | @GetMapping | findAll(), findById(id) | Fetch one or all records. |
| Update | Modify Data | PUT | @PutMapping | save(entity) | Change an existing record’s data. |
| Delete | Remove Data | DELETE | @DeleteMapping | deleteById(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.