//Creating REST API using Spring Boot + Hibernate + MySQL

Creating REST API using Spring Boot + Hibernate + MySQL

Hey guys, Bushan here, Welcome back to B2 Tech. Today in this article we are going discuss how we can create REST API using Spring boot, hibernate and MySQL. We are going to create End-to-End REST API which is connected to Database and we will discuss all the CRUD operations, so let’s get started.



Tools and Technologies used,

  • Spring Boot 2.1.1
  • Hibernate
  • Spring MVC
  • MySQL
  • Maven
  • Eclipse Oxygen IDE

Creating the Project

To create a Spring boot project, Spring provides a web tool called Spring Initializer. Just go to https://start.spring.io/ and follow the below steps

Provide the details as follows

  • Group: in.bushansirgur.springbootcrud
  • Artifact: springbootcrudapi
  • Dependencies: Web, JPA, Devtools, MySQL



Once all the details entered, click on the Generate Project. Spring will create a starter project based on the dependencies you entered and download the project in zip format.
Unzip the project and open in Eclipse IDE or STS IDE.

Project Structure

RELATED: Create a Full Stack Application using Angular + Spring MVC + Hibernate + MySQL

pom.xml
Following are the dependencies which we added while creating the project



Configuring MySQL database

To configure MySQL database follow the below steps,
STEP 1: Go to MySQL database and create a database

STEP 2: Select the database

STEP 3: Create a table



STEP 4: Open application.properties and add the following in it

add the username and password as per your MySQL installation

Creating an Employee model

Employee.java
Create an Employee model class under the in.bushansirgur.springbootcrud.springbootcrudapi.model package

Creating the DAO

Create EmployeeDAO and EmployeeDAOImpl inside the in.bushansirgur.springbootcrud.springbootcrudapi.dao package



EmployeeDAO.java
The Interface defines the following 4 methods

EmployeeDAOImpl.java
This class implements the EmployeeDAO and provide the implementation for all the 4 methods.

EmployeeService.java
Create EmployeeService and EmployeeServiceImpl inside the in.bushansirgur.springbootcrud.springbootcrudapi.service package. The Interface defines the following 4 methods

EmployeeServiceImpl.java
This class implements the EmployeeService and provide the implementation for 4 methods




EmployeeController.java
Create EmployeeController inside the in.bushansirgur.springbootcrud.springbootcrudapi.controller package. This class defines the 5 following methods

CrudapiApplication.java
This is the main class, it is the entry point to the application

That’s it! Now the right click on the main class and choose Run as Java application. The application starts in default port 8080. You can test the application by using any RestClient like Postman etc..



That’s all for now. Thank you so much for tuning in, I hope this post helped you in one or the other way. If you have any questions regarding this post, reach out to me @ [email protected]

Fork it from Github Click here



Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.