About Us Contact Us Write for Us Advertise
Home > Java > Build a Spring Boot CRUD App with a Database (Step by Step)
Java

Build a Spring Boot CRUD App with a Database (Step by Step)

Build a complete Spring Boot CRUD application for products, backed by a database: entity, repository, service with a proper update, controller with all four operations, and testing.

Shiv Pandey
Shiv Pandey
Oct 12, 2026 | 128 views
Build a Spring Boot CRUD App with a Database (Step by Step)

"CRUD" — Create, Read, Update, Delete — is the beating heart of almost every application. If you can build a solid CRUD app, you can build most business software. In this project you'll create a complete Spring Boot CRUD application for managing products, backed by a real database, with all four operations working end to end. It's the perfect portfolio piece.

What is CRUD? The four fundamental things you do with data: Create (add new), Read (fetch), Update (change), Delete (remove). They map to HTTP POST, GET, PUT, DELETE. Master these and you understand the backbone of backend development. Builds on the REST API project.

The plan: a Product CRUD app

We'll manage products (name, price, quantity) with all four operations, using the layered architecture: Controller → Service → Repository → Database. This time we'll emphasise the Update operation, which the basic REST project skipped.

The entity and repository

@Entity
public class Product {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;
    private int quantity;
    // getters & setters
}

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> { }

The service — including a proper Update

The interesting one is update: fetch the existing record, change its fields, and save. This "find, modify, save" pattern is how updates work throughout Spring apps:

@Service
public class ProductService {
    private final ProductRepository repo;
    public ProductService(ProductRepository repo) { this.repo = repo; }

    public List<Product> getAll() { return repo.findAll(); }

    public Product create(Product p) { return repo.save(p); }

    public Product update(Long id, Product updated) {
        Product p = repo.findById(id)
            .orElseThrow(() -> new RuntimeException("Product not found"));
        p.setName(updated.getName());        // modify the existing record
        p.setPrice(updated.getPrice());
        p.setQuantity(updated.getQuantity());
        return repo.save(p);                  // save = UPDATE (same id)
    }

    public void delete(Long id) { repo.deleteById(id); }
}

The controller — all four operations

@RestController
@RequestMapping("/api/products")
public class ProductController {
    private final ProductService service;
    public ProductController(ProductService service) { this.service = service; }

    @GetMapping                      // READ all
    public List<Product> all() { return service.getAll(); }

    @PostMapping                     // CREATE
    public Product create(@RequestBody Product p) { return service.create(p); }

    @PutMapping("/{id}")             // UPDATE
    public Product update(@PathVariable Long id, @RequestBody Product p) {
        return service.update(id, p);
    }

    @DeleteMapping("/{id}")          // DELETE
    public void delete(@PathVariable Long id) { service.delete(id); }
}

Test all four operations

# CREATE
curl -X POST localhost:8080/api/products -H "Content-Type: application/json" \
  -d '{"name":"Laptop","price":999.0,"quantity":5}'
# READ
curl localhost:8080/api/products
# UPDATE (id 1)
curl -X PUT localhost:8080/api/products/1 -H "Content-Type: application/json" \
  -d '{"name":"Laptop","price":899.0,"quantity":3}'
# DELETE (id 1)
curl -X DELETE localhost:8080/api/products/1

Watch a product get created, listed, updated (price drops!), and deleted. That's a complete CRUD cycle — the foundation of real applications. 🎉

Take it further

CRUD is deceptively important — it feels basic, but honestly, a huge portion of professional backend work is CRUD with business rules layered on top. Once I'd built a couple of solid CRUD apps, I realised most "complex" applications are really just many CRUD operations working together, plus validation, auth, and a nice UI. So don't underestimate this project: nailing the find-modify-save update pattern and the full request-to-database cycle means you genuinely understand backend development. Build it, add a Thymeleaf UI to make it visual, and it becomes a standout portfolio piece you can confidently walk an interviewer through.

Next projects

← REST API project · More project ideas →

Related Articles

Build Your First Spring Boot App (Step-by-Step Beginner Guide)
Java

Build Your First Spring Boot App (Step-by-Step Beginner Guide)

Build a Spring Boot REST API Project (Step by Step)
Java

Build a Spring Boot REST API Project (Step by Step)

Build a Blog Platform with Spring Boot (Capstone Project)
Java

Build a Blog Platform with Spring Boot (Capstone Project)

Build a Java Console Project: A To-Do App (Step by Step)
Java

Build a Java Console Project: A To-Do App (Step by Step)