Spring Boot, Hibernate, MySQL example: CRUD app

In this tutorial, we’re gonna build a Spring Boot, Hibernate, MySQL CRUD example that interacts with MySQL database. You’ll know:

How to configure Spring Data JPA, Hibernate to work with MySQL Database
How to define Data Models and Re…

In this tutorial, we’re gonna build a Spring Boot, Hibernate, MySQL CRUD example that interacts with MySQL database. You’ll know:

  • How to configure Spring Data JPA, Hibernate to work with MySQL Database
  • How to define Data Models and Repository interfaces
  • Way to create Spring Rest Controller to process HTTP requests
  • Way to use Spring Data JPA to interact with MySQL Database

Full Article: Spring Boot, Hibernate, MySQL example: Build CRUD app

Overview of Spring Boot, Hibernate, MySQL example

We will build a Spring Boot + MySQL CRUD example for a Tutorial application in that:

  • Each Tutotial has id, title, description, published status.
  • Apis help to create, retrieve, update, delete Tutorials.
  • Apis also support custom finder methods such as find by published status or by title.

These are APIs that we need to provide:

Methods Urls Actions
POST /api/tutorials create new Tutorial
GET /api/tutorials retrieve all Tutorials
GET /api/tutorials/:id retrieve a Tutorial by :id
PUT /api/tutorials/:id update a Tutorial by :id
DELETE /api/tutorials/:id delete a Tutorial by :id
DELETE /api/tutorials delete all Tutorials
GET /api/tutorials/published find all published Tutorials
GET /api/tutorials?title=[keyword] find all Tutorials which title contains keyword
  • We make CRUD operations & finder methods with Spring Data JPA’s JpaRepository.
  • The database could be PostgreSQL or MySQL depending on the way we configure project dependency & datasource.

Technology

  • Java 8
  • Spring Boot 2 (with Spring Web MVC, Spring Data JPA)
  • MySQL
  • Maven

Project Structure

spring-boot-hibernate-mysql-example-crud-project-structure

Let me explain it briefly.

Tutorial data model class corresponds to entity and table tutorials.
TutorialRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController.
TutorialController is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
pom.xml contains dependencies for Spring Boot and MySQL/PostgreSQL.

Video

This is demo video and brief instruction of Spring Boot Rest Apis with Hibernate, MySQL example using Spring Data JPA:



Source Code

For step by step instruction and Github, please visit:
Spring Boot, Hibernate, MySQL example: Build CRUD app



Further Reading

If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable

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 for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest

You can also know:

  • how to deploy this Spring Boot App on AWS (for free) with this tutorial.
  • way to upload an Excel file and store the data in MySQL database with this post
  • upload CSV file and store the data in MySQL with this post.

More Practice:

Fullstack:


Print Share Comment Cite Upload Translate
APA
Tien Nguyen | Sciencx (2024-03-29T10:03:50+00:00) » Spring Boot, Hibernate, MySQL example: CRUD app. Retrieved from https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/.
MLA
" » Spring Boot, Hibernate, MySQL example: CRUD app." Tien Nguyen | Sciencx - Thursday May 13, 2021, https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/
HARVARD
Tien Nguyen | Sciencx Thursday May 13, 2021 » Spring Boot, Hibernate, MySQL example: CRUD app., viewed 2024-03-29T10:03:50+00:00,<https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/>
VANCOUVER
Tien Nguyen | Sciencx - » Spring Boot, Hibernate, MySQL example: CRUD app. [Internet]. [Accessed 2024-03-29T10:03:50+00:00]. Available from: https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/
CHICAGO
" » Spring Boot, Hibernate, MySQL example: CRUD app." Tien Nguyen | Sciencx - Accessed 2024-03-29T10:03:50+00:00. https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/
IEEE
" » Spring Boot, Hibernate, MySQL example: CRUD app." Tien Nguyen | Sciencx [Online]. Available: https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/. [Accessed: 2024-03-29T10:03:50+00:00]
rf:citation
» Spring Boot, Hibernate, MySQL example: CRUD app | Tien Nguyen | Sciencx | https://www.scien.cx/2021/05/13/spring-boot-hibernate-mysql-example-crud-app/ | 2024-03-29T10:03:50+00:00
https://github.com/addpipe/simple-recorderjs-demo