//Complete CRUD Application with Spring Boot, Hibernate and MySQL

Complete CRUD Application with Spring Boot, Hibernate and MySQL

Hello guys, Bushan here, Welcome back to B2 Tech. Today in this article, you will learn how to build a complete CRUD application with Spring Boot, Hibernate and MySQL. It is not a simple hello world application, it’s a fully fledged end-to-end database application. You will learn to perform all the CRUD operations like CREATE, READ, UPDATE and DELETE. So by the end of this article, you are able to create your own production-ready Spring boot application.

Before that let’s understand, What is Spring Boot and What are the benefits we will get over Spring MVC?
  • The first is that Boot is the “future of Spring”. That means that with Boot you can benefit from many commitments of the Spring community. Most of the Spring projects today are completely integrated with Boot, even the community starts to develop many applications based on Boot. For example for managing and monitoring.
  • With Spring Boot you can benefit from very nice and useful features such as an actuator and remote shell for managing and monitoring, that improves your application with production-ready features that are very useful.
  • Very nice and powerful properties and configuration controls – you can configure your application with and application.properties extend the boot in a very simple and impressive way, even the management in terms of overriding is very powerful.
  • It is one of the first micro-service ready platforms, and in my opinion, nowadays it is the best! Even if you don’t build a micro-service project with boot you can benefit of using a modern approach in which you have an auto-consistent jar that can benefit from all the features that I described above or if you prefer you can impose the packaging as a classical war and deploy your war in any of the containers that you want.
  • Use of an intelligent and convention over configuration approach that reduces the startup and configuration phase of your project significantly. In fact, you have a set of starter Maven or Gradle dependencies that simplify the dependency management. Then with the auto-configuration characteristic, you can benefit from a lot of standard configurations, that are introduced through the Conditional Configuration framework in Spring 4. You can override it with your specific configurations just defining your bean according to with the convention that you can see in the auto-configure JAR of the Boot dependency. Remember that Spring is open-source and you can see the code. Also the documentation, in my opinion, is good.

Spring initializer is a cool tool attainable at this link: https://start.spring.io/ is a very cool tool just to create your project in a very fast way.

What are we going to build in this article?

We are going to build a Book Management System, where the user can Create a book, Read a book, Update a book, and Delete a book.

Cool, let’s get started.

NOTE: This is not a Spring JPA article, we are not using JPA, we will use Hibernate as an ORM framework.

Tools and Technologies used,
  • Java 1.8
  • Spring 2.0.5
  • Hibernate 5.1.0
  • MySQL
  • Eclipse Oxygen
  • JSTL taglib
  • JSP

First, let’s create Spring Boot starter project, go to https://start.spring.io/

Choose maven project and choose the spring boot version which is 2.0.5 or the latest version. Enter the group id, artifact id, and dependencies as shown in the above image and then click on the Generate project. This will gives the Spring boot starter project with all the dependencies that we have mentioned above.

Open the project in Eclipse IDE, and open the pom.xml, and add the following dependencies.

pom.xml

Next, let’s create a Database and respective table for the application

To create a Database, run this command

To use this Database, run this command

To create a table, run this command

The project configuration is pretty simple in Spring boot, we don’t have to maintain an XML file. So inside the application.properties file write the following code.

application.properties

SpringbootcrudapiApplication.java

Employee.java

EmployeeDAOImpl.java

EmployeeDAO.java

@Entity annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable interface.

And why @Entity annotation is mandatory? … well, it is the way how JPA is designed. When you create a new entity you have to do at least two things

  1. annotated it with @Entity
  2. create an id field and annotate it with @Id

Anything else is optional, for example, the table name is derived from entity class name (and therefore @Table annotation can be optional), table’s columns are derived from entities variables (and therefore @Column annotation can be optional), and so on …

JPA is trying to provide a fast and easy start to developers who want to learn/use this API, and giving developers option to configure a few things as possible to make something functional is one of the ways how this API wants to achieve this “easy to use/learn” goal. Hence the @Entity annotation (together with @Id annotation) is the minimum you have to do in order to create an entity.

EmployeeService.java

EmployeeServiceImpl.java

@Repository: This is the Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. all the Database related operations are done by the repository.

EmployeeController.java

@Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user does not directly call the persistence method so it will call this method using this annotation. It will request @Repository as per user request

@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts like a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

employeesAdd.jsp

employeesList.jsp

Now right click on the project, choose run as and click maven clean

Now again right click on the project, choose run as and click maven build,

It will ask us to enter the goals, enter ‘package‘ and click run

Once the project has build successfully, open the main class SpringbootcrudapiApplication.java and run the file as a java application, the final application looks like this,

That’s it for this article, i hope this article is helped you in one or the other way if so then please share this article with all social media profiles. If you have any queries regarding this article/post then leave your comments in the comment section, I will try to answer for your queries. Thank you so much for reading, I will see you in the next article.

Clone this 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.