Spring MVC CRUD with Example

Last Updated : 24 Apr, 2026

Spring MVC CRUD refers to implementing Create, Read, Update, and Delete operations in a web application using the Spring MVC framework. It follows the MVC pattern to separate concerns and efficiently manage data through controllers, services, and views.

  • Uses Controller, Service, and Repository layers to handle application flow
  • Integrates with databases using Spring Data JPA or JDBC
  • Supports dynamic UI rendering using view technologies like JSP or Thymeleaf

Real-world example: In a Student Management System, administrators can add new students, view the list of students, update student details, and delete student records. These operations are handled using Spring MVC controllers, services, and views to manage student data efficiently.

CRUD Example of Spring MVC

Below down are the steps to create a simple course-tracking CRUD application focused on the Spring MVC module.

Step 1: Create Spring Boot Project

Create a Spring Boot project using Spring Initializr with required dependencies.

  • Choose Spring Boot project (Maven/Gradle)
  • Add dependencies: Spring Web, Spring Data JPA, Thymeleaf, Lombok
  • Import project into IDE (STS/IntelliJ/Eclipse)

The project structure for the Spring MVC application is as follows:

ProjectStructure

Step 2: Add Dependencies

Add required dependencies in build.gradle.

  • Include spring-boot-starter-web, spring-boot-starter-data-jpa
  • Add Thymeleaf for UI rendering
  • Add H2/MySQL database dependency

dependencies {

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.projectlombok:lombok:1.18.20'

developmentOnly 'org.springframework.boot:spring-boot-devtools'

runtimeOnly 'com.h2database:h2'

runtimeOnly 'mysql:mysql-connector-java'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

Step 3: Create Model Layer

Create an entity class Course using JPA annotations.

  • Use @Entity and @Table
  • Define primary key with @Id
  • Use Lombok annotations like @Data
Java
import lombok.*;
import javax.persistence.*;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "courses")
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "course_name")
    private String courseName;

    @Column(name = "instructor")
    private String instructor;

    @Column(name = "email")
    private String email;
}

Step 4: Create Repository Layer

Create a repository interface extending JpaRepository.

  • Extend JpaRepository<Course, Long>
  • No implementation required
  • Supports CRUD, pagination, and sorting
Java
import com.example.testing_001.model.Course;
import org.springframework.data.jpa.repository.JpaRepository;

@Repository
public interface CourseRepository extends JpaRepository<Course, Long> {

}

Step 5: Create Service Layer

Define service interface and implement it.

  • Create CourseService interface
  • Implement in CourseServiceImpl
  • Use @Service and @Autowired
Java
import com.example.testing_001.model.Course;
import java.util.List;
import org.springframework.data.domain.Page;

public interface CourseService {
    List<Course> getAllCourses();
    void saveCourse(Course course);
    Course getCourseById(long id);
    void deleteCourseById(long id);
    Page<Course> findPaginated(int pageNum, int pageSize,
                               String sortField,
                               String sortDirection);
}

Service Implemetation: CourseServiceImpl

The class CourseServiceImpl implements the CourseService interface and provides us with all the CRUD operations logic which is our business logic here.

Java
import com.example.testing_001.model.Course;
import com.example.testing_001.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CourseServiceImpl implements CourseService{

    @Autowired
    private CourseRepository courseRepository;

    @Override
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }

    @Override
    public void saveCourse(Course course) {
        this.courseRepository.save(course);
    }

    @Override
    public Course getCourseById(long id) {
        Optional<Course> optionalCourse = courseRepository.findById(id);
        Course course = null;
        if (optionalCourse.isPresent()) {
            course = optionalCourse.get();
        } else {
            throw new RuntimeException("Course not found for id : " + id);
        }
        return course;
    }

    @Override
    public void deleteCourseById(long id) {
        this.courseRepository.deleteById(id);
    }

    @Override
    public Page<Course> findPaginated(int pageNum, int pageSize, String sortField, String sortDirection) {
        Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
                Sort.by(sortField).descending();

        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return this.courseRepository.findAll(pageable);
    }
}

Step 6: Create Controller Layer

Create a controller class to handle requests.

  • Use @Controller annotation
  • Map requests using @GetMapping, @PostMapping
  • Use Model to pass data
Java
import com.example.testing_001.model.Course;
import com.example.testing_001.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/")
    public String viewHomePage(Model model) {
        return findPaginated(1, "courseName", "asc", model);
    }

    @GetMapping("/add")
    public String showNewCourseForm(Model model) {
        Course Course = new Course();
        model.addAttribute("course", Course);
        return "new_course";
    }

    @PostMapping("/save")
    public String saveCourse(@ModelAttribute("course") Course course) {
        // save Course to database
        courseService.saveCourse(course);
        return "redirect:/";
    }

    @GetMapping("/update/{id}")
    public String showFormForUpdate(@PathVariable( value = "id") long id, Model model) {

        Course course = courseService.getCourseById(id);
        model.addAttribute("course", course);
        return "update_course";
    }

    @GetMapping("/delete/{id}")
    public String deleteCourse(@PathVariable (value = "id") long id) {

        this.courseService.deleteCourseById(id);
        return "redirect:/";
    }


    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        int pageSize = 5;

        Page<Course> page = courseService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<Course> listCourses = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());

        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listCourses", listCourses);
        return "index";
    }
}


Note: Change the GET mapping for course update and delete course to PUT mapping and DELETE mapping respectively. It is considered best practice to use mappings based on the functionality of the api.

Step 7: Create Thymeleaf Views

Create HTML templates for UI.

  • Create pages: index.html, new_course.html, update_course.html
  • Use th:text, th:each, th:href
  • Implement forms for add/update

Home page:

HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
  <meta charset="ISO-8859-1">
  <title>Course Tracker</title>

  <link rel="stylesheet"
        href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
        integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
        crossorigin="anonymous">

</head>
<body>

<div class="container my-2">
  <h1>Courses List</h1>

  <a th:href = "@{/add}" class="btn btn-primary btn-sm mb-3"> Add Course </a>

  <table border="1" class = "table table-striped table-responsive-md">
    <thead>
    <tr>
      <th>
        <a th:href="@{'/page/' + ${currentPage} + '?sortField=courseName&sortDir=' + ${reverseSortDir}}">
          Course Name</a>
      </th>
      <th>
        <a th:href="@{'/page/' + ${currentPage} + '?sortField=instructor&sortDir=' + ${reverseSortDir}}">
          Course Instructor</a>
      </th>
      <th>
        <a th:href="@{'/page/' + ${currentPage} + '?sortField=email&sortDir=' + ${reverseSortDir}}">
          Course Email</a>
      </th>
      <th> Actions </th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="course : ${listCourses}">
      <td th:text="${course.courseName}"></td>
      <td th:text="${course.instructor}"></td>
      <td th:text="${course.email}"></td>
      <td> <a th:href="@{/update/{id}(id=${course.id})}" class="btn btn-primary">Update</a>
        <a th:href="@{/delete/{id}(id=${course.id})}" class="btn btn-danger">Delete</a>
      </td>
    </tr>
    </tbody>
  </table>

  <div th:if = "${totalPages > 1}">
    <div class = "row col-sm-10">
      <div class = "col-sm-5">
        Total Rows: [[${totalItems}]]
      </div>
      <div class = "col-sm-3">
                    <span th:each="i: ${#numbers.sequence(1, totalPages)}">
                        <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">[[${i}]]</a>
                        <span th:unless="${currentPage != i}">[[${i}]]</span>  &nbsp; &nbsp;
                    </span>
      </div>
      <div class = "col-sm-1">
        <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">Next</a>
        <span th:unless="${currentPage < totalPages}">Next</span>
      </div>

      <div class="col-sm-1">
        <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">Last</a>
        <span th:unless="${currentPage < totalPages}">Last</span>
      </div>
    </div>
  </div>
</div>
</body>
</html>

Add course page:

HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="ISO-8859-1">
    <title>Course </title>
    <link rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
          crossorigin="anonymous">
</head>
<body>
<div class="container">
    <h1>Course Tracker</h1>
    <hr>
    <h2>Save Course</h2>

    <form action="#" th:action="@{/save}" th:object="${course}"
          method="POST">
        <input type="text" th:field="*{courseName}"
               placeholder="Course Name" class="form-control mb-4 col-4">

        <input type="text" th:field="*{instructor}"
               placeholder="Instructor Name" class="form-control mb-4 col-4">

        <input type="text" th:field="*{email}"
               placeholder="Course Email" class="form-control mb-4 col-4">

        <button type="submit" class="btn btn-info col-2"> Save Course</button>
    </form>

    <hr>

    <a th:href = "@{/}"> Back to Course List</a>
</div>
</body>
</html>

Update course page:

HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="ISO-8859-1">
    <title>Course Tracker</title>

    <link rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Course Tracker</h1>
    <hr>
    <h2>Update Course</h2>

    <form action="#" th:action="@{/save}" th:object="${course}"
          method="POST">

        <!-- Add hidden form field to handle update -->
        <input type="hidden" th:field="*{id}" />
        <input type="text" th:field="*{courseName}" class="form-control mb-4 col-4" placeholder="Course name">
        <input type="text" th:field="*{instructor}" class="form-control mb-4 col-4" placeholder="Course instructor">
        <input type="text" th:field="*{email}" class="form-control mb-4 col-4" placeholder="Email">
        <button type="submit" class="btn btn-info col-2"> Update Course</button>
    </form>

    <hr>

    <a th:href = "@{/}"> Back to Course List</a>
</div>
</body>
</html>

Step 8: Test the CRUD Operations

1. Creating a course:

CreatingCourse

2. Reading courses:

ReadingCourse
CourseList

3. Updating course:

UpdatingCourse

4. Course updated successfully:

CourseUpdate

5. Deleting course:

DeletingCourse
Comment

Explore