Spring Boot CRUD Application with MySQL and JPA (Step-by-Step Guide)

Spring Boot CRUD Application with MySQL and JPA (Step-by-Step Guide)

Introduction

A CRUD application is one of the most common projects developers build when learning Spring Boot.

CRUD stands for:

  • Create
  • Read
  • Update
  • Delete

In this tutorial, you will build a complete REST API using:

  • Spring Boot 3
  • Spring Data JPA
  • MySQL
  • Maven

By the end of this guide, you'll have a working backend application capable of managing employee records.


What You Will Build

The application will support:

POST   /employees
GET    /employees
GET    /employees/{id}
PUT    /employees/{id}
DELETE /employees/{id}

Prerequisites

Before starting, ensure you have:

  • Java 21 (or Java 17+)
  • Spring Boot 3
  • Maven
  • MySQL Server
  • IntelliJ IDEA or Eclipse

Related Reading:



Spring Boot CRUD Maven Dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
Spring Boot CRUD examples commonly use Spring Web, Spring Data JPA, and the MySQL driver. 

Project Structure

src
 └── main
      ├── controller
      ├── service
      ├── repository
      ├── entity
      └── resources

Step 1: Create Database

CREATE DATABASE employee_db;

Step 2: Configure application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/employee_db

spring.datasource.username=root

spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

Step 3: Create Entity

Employee.java

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy =
        GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String email;

    private String department;

}

Step 4: Create Repository

EmployeeRepository.java

@Repository
public interface EmployeeRepository
       extends JpaRepository<Employee, Long> {
}

Step 5: Create Service Layer

EmployeeService.java

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository repository;

    public List<Employee> getAllEmployees() {
        return repository.findAll();
    }

    public Employee save(Employee employee) {
        return repository.save(employee);
    }
}

Step 6: Create Controller

EmployeeController.java

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    @GetMapping
    public List<Employee> getEmployees() {
        return service.getAllEmployees();
    }

    @PostMapping
    public Employee createEmployee(
           @RequestBody Employee employee) {

        return service.save(employee);
    }
}

CRUD Operations Flow

Client Request
      ↓
Controller
      ↓
Service
      ↓
Repository
      ↓
MySQL Database
      ↓
Response

Testing with Postman

Create Employee

POST /employees

Request Body:

{
   "name":"Muhammad Zeeshan",
   "email":"zeeshan@example.com",
   "department":"IT"
}

Get All Employees

GET /employees

Update Employee

PUT /employees/1

Delete Employee

DELETE /employees/1

Common Errors

Database Connection Failed

Check:

  • MySQL running
  • Username
  • Password
  • Database exists

Entity Not Found

Verify:

@Id
@GeneratedValue

configuration.


Port Already In Use

Change:

server.port=8081

Best Practices

Use DTOs

Avoid exposing entities directly.

Add Validation

Use:

@NotBlank
@Email

Add Exception Handling

Implement:

@RestControllerAdvice

Secure APIs

Use Spring Security and JWT.


FAQ

What is CRUD in Spring Boot?

CRUD stands for Create, Read, Update, and Delete operations used to manage data in applications.

Why use Spring Data JPA?

Spring Data JPA reduces boilerplate code and simplifies database operations.

Can I use PostgreSQL instead of MySQL?

Yes. Spring Boot supports PostgreSQL, MySQL, Oracle, SQL Server, and many other databases.

Is this a REST API?

Yes. The controller exposes REST endpoints using HTTP methods.

Post a Comment

Please enter relevant questions and information.

Previous Post Next Post