Spring Boot, Hibernate, MySQL example: CRUD app
Tags: spring, mysql, java, webdev
Published: 2021-05-13 03:08:36

In this tutorial, we're gonna build a Spring Boot, Hibernate CRUD example that interacts with MySQL database. You'll know:
<ul>
    	<li>How to configure Spring Data JPA, Hibernate to work with MySQL Database</li>
    	<li>How to define Data Models and Repository interfaces</li>
	<li>Way to create Spring Rest Controller to process HTTP requests</li>
   	<li>Way to use Spring Data JPA to interact with MySQL Database</li>
</ul>

Full Article: [Spring Boot, Hibernate, MySQL example: Build CRUD app](https://bezkoder.com/spring-boot-jpa-crud-rest-api/)

<h2>Overview of Spring Boot, Hibernate, MySQL example</h2>
We will build a Spring Boot + MySQL CRUD example for a Tutorial application in that:
<ul>
	<li>Each Tutotial has id, title, description, published status.</li>
	<li>Apis help to create, retrieve, update, delete Tutorials.</li>
	<li>Apis also support custom finder methods such as find by published status or by title.</li>
</ul>

These are APIs that we need to provide:
<div class="table-responsive">
<table class="table table-hover  table table-hover" width="100%">
<thead>
<tr>
<th>Methods</th>
<th>Urls</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>POST</td>
<td>/api/tutorials</td>
<td>create new Tutorial</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials</td>
<td>retrieve all Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials/:id</td>
<td>retrieve a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>PUT</td>
<td>/api/tutorials/:id</td>
<td>update a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>DELETE</td>
<td>/api/tutorials/:id</td>
<td>delete a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>DELETE</td>
<td>/api/tutorials</td>
<td>delete all Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials/published</td>
<td>find all published Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials?title=[keyword]</td>
<td>find all Tutorials which title contains <code>keyword</code></td>
</tr>
</tbody>
</table>
</div>
- We make CRUD operations & finder methods with Spring Data JPA's <code>JpaRepository</code>.
- The database could be PostgreSQL or MySQL depending on the way we configure project dependency & datasource.

<h2>Technology</h2>
<ul>
    	<li>Java 8</li>
    	<li>Spring Boot 2 (with Spring Web MVC, Spring Data JPA)</li>
    	<li>MySQL</li>
    	<li>Maven</li>
</ul>

<h2>Project Structure</h2>
![spring-boot-hibernate-mysql-example-crud-project-structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glc66vjscv4174g2cbn2.png)

Let me explain it briefly.

– <code>Tutorial</code> data model class corresponds to entity and table <em>tutorials</em>.
– <code>TutorialRepository</code> is an interface that extends <a href="https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html">JpaRepository</a> for CRUD methods and custom finder methods. It will be autowired in <code>TutorialController</code>.
– <code>TutorialController</code> is a <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html">RestController</a> which has request mapping methods for RESTful requests such as: <em>getAllTutorials</em>, <em>createTutorial</em>, <em>updateTutorial</em>, <em>deleteTutorial</em>, <em>findByPublished</em>...
– Configuration for Spring Datasource, JPA & Hibernate in <strong>application.properties</strong>.
– <strong>pom.xml</strong> contains dependencies for Spring Boot and MySQL/PostgreSQL.

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

{% youtube wJx3xxp2urc %}

## Source Code
For step by step instruction and Github, please visit:
[Spring Boot, Hibernate, MySQL example: Build CRUD app](https://bezkoder.com/spring-boot-jpa-crud-rest-api/)

## Further Reading
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)

If you want to add Pagination to this Spring project, you can find the instruction at:
<a href="https://bezkoder.com/spring-boot-pagination-filter-jpa-pageable/">Spring Boot Pagination & Filter example | Spring JPA, Pageable</a>

To sort/order by multiple fields:
<a href="https://bezkoder.com/spring-data-sort-multiple-columns/">Spring Data JPA Sort/Order by multiple Columns | Spring Boot</a>

Handle Exception for this Rest APIs is necessary:
- <a href="https://bezkoder.com/spring-boot-controlleradvice-exceptionhandler/">Spring Boot @ControllerAdvice & @ExceptionHandler example</a>
- <a href="https://bezkoder.com/spring-boot-restcontrolleradvice/">@RestControllerAdvice example in Spring Boot</a>

Or way to write Unit Test for the JPA Repository:
<a href="https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/">Spring Boot Unit Test for JPA Repositiory with @DataJpaTest</a>

You can also know:
- how to deploy this Spring Boot App on AWS (for free) with <a href="https://bezkoder.com/deploy-spring-boot-aws-eb/">this tutorial</a>.
- way to upload an Excel file and store the data in MySQL database with <a href="https://bezkoder.com/spring-boot-upload-excel-file-database/">this post</a>
- upload CSV file and store the data in MySQL with <a href="https://bezkoder.com/spring-boot-upload-csv-file/">this post</a>.

More Practice:
- <a href="https://bezkoder.com/spring-boot-jwt-authentication/">Secure Spring Boot App with Spring Security & JWT Authentication</a>
- <a href="https://bezkoder.com/spring-boot-rest-xml/">Spring Boot Rest XML example – Web service with XML Response</a>
- <a href="https://bezkoder.com/spring-boot-graphql-mysql-jpa/">Spring Boot + GraphQL + MySQL example</a>
- <a href="https://bezkoder.com/spring-boot-file-upload/">Spring Boot Multipart File upload example</a>
- <a href="https://bezkoder.com/spring-boot-pagination-sorting-example/">Spring Boot Pagination and Sorting example</a>

Dockerize: [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)

Fullstack:
- <a href="https://bezkoder.com/spring-boot-vue-js-crud-example/">Spring Boot + Vue.js example</a>
- <a href="https://bezkoder.com/angular-spring-boot-crud/">Angular 8 + Spring Boot example</a>
- <a href="https://bezkoder.com/angular-10-spring-boot-crud/">Angular 10 + Spring Boot example</a>
- <a href="https://bezkoder.com/angular-11-spring-boot-crud/">Angular 11 + Spring Boot example</a>
- <a href="https://bezkoder.com/react-spring-boot-crud/">React + Spring Boot example</a>