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:
- Retrieve all objects:
- Update status of an item:
- Update an object in its own page:
- Search by name:
- Delete an item:
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
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 inCourseController
. 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:
- Spring Boot @ControllerAdvice & @ExceptionHandler example
- @RestControllerAdvice example in Spring Boot
Or way to write Unit Test:
Fullstack CRUD App:
- Vue + Spring Boot example
- Angular 8 + Spring Boot example
- Angular 10 + Spring Boot example
- Angular 11 + Spring Boot example
- Angular 12 + Spring Boot example
- Angular 13 + Spring Boot example
- Angular 14 + Spring Boot example
- Angular 15 + Spring Boot example
- Angular 16 + Spring Boot example
- React + Spring Boot example
More Practice:
- Spring Boot with Spring Security & JWT Authentication
- Spring Boot Rest XML example – Web service with XML Response
- Spring Boot Multipart File upload example
- Spring Boot Pagination and Sorting example
Associations: