Spring Boot Hibernate example

In this post, we are going to see how to create Spring boot hibernate example.

We will use Spring boot 1.5.3 Release version, it comes with hibernate 5. We will create a Spring boot hibernate application which will have JSP as user interface. It will provide user interface from which you can add, update or delete customer database.We will use controller, services and DAO classes to achieve these functionalities.We will connect to MySQL database using SessionFactory class of hibernate.

The spring-boot-starter-parent provides you all maven defaults required for any spring project.
Since we are developing a web application, we also need to add spring-boot-starter-web dependency and also we need to include pring-boot-starter-data-jpa to run this application with hibernate.You need to also put mysql-connector-java for MySql JDBC driver.If you are using any other database, you need to use different database connector.
Let’s do hibernate configuration first.

Hibernate Configuration

Step 3: Create a file named “HibernateConfiguration.java” in package .org.arpit.java2blog

Spring boot main file

Step 11: Create a file named “SpringBootHibernateApplication.java” in package .org.arpit.java2blog

1

2

3

4

5

6

7

8

9

10

11

12

13

14

packageorg.arpit.java2blog;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

publicclassSpringBootHibernateApplication{

publicstaticvoidmain(String[]args)

{

SpringApplication.run(SpringBootHibernateApplication.class,args);

}

}

We have just added @SpringBootApplication and it does all the work.
Let’s understand more about this annotation.
@SpringBootApplication is an annotation that adds all of the following:

@Configuration makes the class as a source of bean definitions for the application context.
@EnableAutoConfiguration enables Spring boot to add beans presents in classpath setting and various property setting.
Normally you would add @EnableWebMvc for a Spring MVC application, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the default package, allowing it to find the controllers.
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

Run the application

Step 12: It ‘s time to do maven build.

Right click on project -> Run as -> Maven buildStep 13: Provide goals as clean install spring-boot:run (given below) and click on run

Step 14: Once you are done with Maven build, let’s go to the browser and put following URL.

http://localhost:8080/getAllCustomers

You will see below screen.

Add follow details to Customer Name : as “John” and email as “John@gmail.com” and click on submit.