//Complete CRUD Application in Spring MVC and Hibernate [XML Configuration]

Complete CRUD Application in Spring MVC and Hibernate [XML Configuration]

Hi, Welcome back to my another article on Java, Have you ever heard about Spring/Spring MVC in Java? One of the Most Popular Application Framework for building Java Enterprise Applications. Along the side, Hibernate is also one of the most popular ORM Framework, combining these two frameworks we can build Scalable, robust and industry standard Enterprise applications in Java. So today in this article, let’s discuss these two frameworks and build a complete CRUD Application.

Tools and Technologies Used:
  • Spring 4
  • Hibernate 5
  • MySQL
  • Apache Tomcat 8
  • Eclipse Oxygen

Let’s first Create a Database for our application,

NOTE: I am using MySQL Database, you can use any other Database you want.

Execute the following command inside the MySQL,

To select the database, execute the following command,

Let’s create a table inside the database, execute the following command,

Next step is to create a dynamic project in Eclipse, and set up a structure like this,

Next step is to create a property file inside the src folder,


First, let’s talk about the web.xml

web.xml is a Deployment Descriptor which means, it is the entry point to our application, when we run the application, the web container will search for the web.xml and it will scan the entire file.


Next step is to configure the Spring and Hibernate inside the spring-servlet.xml file


Now, create a Model class inside the entity package,

We will annotate this class with annotation @Entity, It defines that a class can be mapped to a table. And that is it, it is just a marker,


Next step is to create an interface, StudentDAO inside the DAO package,


Now let’s create an implementation class StudentDAOImpl, inside the DAO package,

We will annotate this class with @Repository, It is 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.

@Repository: You need to use this annotation within the persistence layer, this acts like database repository.


So now call these methods from the service layer, let’s create an interface, StudentService inside the service package


So now let’s create an implementation class, StudentServiceImpl,  inside the service package,

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

What’s special about @Service?

Apart from the fact that it is used to indicate that it’s holding the business logic, there’s no noticeable specialty that this annotation provides, but who knows, spring may add some additional exceptional in future.

Finally, let’s create a controller StudentController, inside the controller package,

We will annotate this class with @Controller: where your request mapping from presentation page done i.e. Presentation layer won’t go to any other file it goes directly to @Controller class and checks for requested path in @RequestMapping annotation which written before method calls if necessary.

What’s special about @Controller?

We cannot switch this annotation with any other like @Service or @Repository, even though they look same. The dispatcher scans the classes annotated with @Controller and detects @RequestMapping annotations within them. We can only use @RequestMapping on @Controller annotated classes.


Purpose of each annotation:

1) @Controller : Classes annotated with this, are intended to receive a request from the client side. The first request comes to the Dispatcher Servlet, from where it passes the request to the particular controller using the value of @RequestMapping annotation.

2) @Service : Classes annotated with this, are intended to manipulate data, that we receive from a client or fetch from the database. All the manipulation with data should be done in this layer.

3) @Repository : Classes annotated with this, are intended to connect with database. It can also be considered as DAO(Data Access Object) layer. This layer should be restricted to CRUD (create, retrieve, update, delete) operations only. If any manipulation is required, data should be sent be send back to @Service layer.

If we interchange their place(use @Repository in place of @Controller), our application will work fine.

The main purpose of using three different @annotations is to provide better Modularity to the Enterprise application.

So now, let’s create a folder called jsps inside the WEB-INF to keep all the JSP files,

inside the jsps, create list-students.jsp, this will be our landing page for our application, here we will display the student records


Next step is to create student-form.jsp inside the jsps folder to enter the student details,


Finally, let’s create a welcome file inside the WebContent folder. Here we will just forward the request a specific controller


At this point, if you run the application, you will see the following web page. Congratulations you made it. If you don’t see the webpage, then you made a mistake, double check the code.

That’s it for this article, I hope this article helped you in one or the other way, do let me know your thoughts about this article in the comment section down below if you like the content share it with your friends and I will see you in the next article.

Download the project – 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.