Contents
Introduction
CRUD Operations Using Spring Boot With MySQL 2025: CRUD (Create, Read, Update, Delete) operations are the foundation of any web application. In this guide, we will build a simple Spring Boot application that performs CRUD operations using MySQL as the database.

This tutorial is beginner-friendly and includes step-by-step implementation. By the end, you will have a fully functional RESTful API using Spring Boot and MySQL. [CRUD Operations Using Spring Boot With MySQL 2025]
Prerequisites
Before we start, ensure you have the following installed:
- Java 17 or later
- Spring Tool Suite / IntelliJ IDEA
- Spring Boot 3.x
- MySQL Database
- Maven
- Postman (for testing API endpoints)
Project Setup
1. Create a Spring Boot Project
- Use Spring Initializr to generate a Spring Boot project.
- Select dependencies:
- Spring Web
- Spring Data JPA
- MySQL Driver
- Download and extract the project.
2. Configure application.properties
file
- Open
src/main/resources/application.properties
and configure the database. Add following code in that file. [CRUD Operations Using Spring Boot With MySQL 2025]
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/crud_db
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
- Create a database in your mysql server and put it at the place of crud_db.
- Put Your port number at the place of 3306. usually it is 3306 by default but if you created database on some another port then put that.
- Put your username of mysql server at the place of root and password at the place of yourpassword. [CRUD Operations Using Spring Boot With MySQL 2025]
Create the Entity Class
- Create an entity to represent the database table.
- Create a class as User.
- With the help of annotation @Entity the table will be auto generated as users.
package com.example.crud.model;
import jakarta.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
Create the Repository Interface
- Create a Class as UserRepository and extends it with JpaRepositoy.
package com.example.crud.repository;
import com.example.crud.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
}
Create the Service Layer
package com.example.crud.service;
import com.example.crud.model.User;
import com.example.crud.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}
public User saveUser(User user) {
return userRepository.save(user);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
Create the Controller
package com.example.crud.controller;
import com.example.crud.model.User;
import com.example.crud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public Optional<User> getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User updatedUser) {
Optional<User> existingUser = userService.getUserById(id);
if (existingUser.isPresent()) {
User user = existingUser.get();
user.setName(updatedUser.getName());
user.setEmail(updatedUser.getEmail());
return userService.saveUser(user);
}
return null;
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
Running the Application
1. Start MySQL and create the database crud_db
if not created.
2. Run the application using following Command or if you are in STS[Spring Tool Suite] then you can run as clicking on project folder and running as spring boot app. [CRUD Operations Using Spring Boot With MySQL 2025]
mvn spring-boot:run
3. Use Postman to test the API endpoints:
GET /api/users
– Retrieve all usersGET /api/users/{id}
– Retrieve a user by IDPOST /api/users
– Create a new userPUT /api/users/{id}
– Update a userDELETE /api/users/{id}
– Delete a user
Conclusion
In this blog, we built a simple CRUD REST API using Spring Boot and MySQL. This foundational knowledge will help in developing full-stack applications with Spring Boot. [CRUD Operations Using Spring Boot With MySQL 2025]
Additional Enhancements:
- Add validation using
javax.validation
. - Implement exception handling with
@ControllerAdvice
. - Secure APIs using Spring Security. [CRUD Operations Using Spring Boot With MySQL 2025]
Also Read: Angular vs React vs Vue: Pick the Most Powerful Framework for 2025