DEV Community

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

Posted on • Edited on • Originally published at bezkoder.com

14

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:

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️