DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Spring Boot Thymeleaf CRUD example
Tien Nguyen
Tien Nguyen

Posted on • Updated on

Spring Boot Thymeleaf CRUD example

In this tutorial, we're gonna build a Spring Boot Thymeleaf CRUD example with Maven that use Spring Data JPA to interact with H2/MySQL/PostgreSQL database. You'll know:

  • How to configure Spring Data, JPA, Hibernate to work with Database
  • How to define Data Entity and Repository interfaces
  • Way to create Spring Controller to process HTTP requests
  • Way to use Spring Data JPA to interact with H2/MySQL/PostgreSQL Database
  • How to use Thymeleaf template engine for View layer

Full Article: Spring Boot Thymeleaf CRUD example

More Practice: Spring Boot Thymeleaf Pagination example

Overview of Spring Boot Thymeleaf example

We will build a Spring Boot CRUD example using Thymeleaf template engine for View layer and Spring Data JPA with Database in that:

  • Each Course (entity) has id, name, description, price, enabled status.
  • CRUD operations are supported: create, retrieve, update, delete Courses.
  • User can search Courses by name.

- Create new entity object:

spring-boot-thymeleaf-example-crud-create

- Retrieve all objects:

spring-boot-thymeleaf-example-crud-retrieve

- Update status of an item:

spring-boot-thymeleaf-example-crud-update-status

- Update an object in its own page:

spring-boot-thymeleaf-example-crud-update

- Search by name:

spring-boot-thymeleaf-example-crud-search

- Delete an item:

spring-boot-thymeleaf-example-crud-delete

Technology

  • Java 8
  • Spring Boot 2.7 (with Spring Web MVC, Spring Data JPA, Thymeleaf)
  • H2/MySQL/PostgreSQL Database
  • Maven 3.6.1
  • Bootstrap 4
  • jQuery 3.6.1
  • Font Awesome

Project Structure

spring-boot-thymeleaf-example-crud-project-structure

Let me explain it briefly.

  • Course class corresponds to entity and table courses.
  • CourseRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in CourseController.
  • CourseController is a Controller which has request mapping methods for RESTful requests such as: getAll, addCourse, saveCourse, editCourse, deleteCourse, updateCourseEnabledStatus.

  • static/css contains custom css style.

  • template stores HTML template files for the project.

  • Configuration for Spring Datasource, JPA & Hibernate in application.properties.

  • pom.xml contains dependencies for Spring Boot, Thymeleaf, Bootstrap and Database.

For more steps and Github source code, please visit:
Spring Boot Thymeleaf CRUD example

More Practice: Spring Boot Thymeleaf Pagination example

Further Reading

More Derived queries at:
JPA Repository query example in Spring Boot

Custom query with @Query annotation:
Spring JPA @Query example: Custom query in Spring Boot

To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot

Handle Exception for this Rest APIs is necessary:

Or way to write Unit Test:

Fullstack CRUD App:

More Practice:

Associations:

Top comments (0)

We want your help! Become a Tag Moderator.
Fill out this survey and help us moderate our community by becoming a tag moderator here at DEV.