Spring Data JPA example using spring boot

In this example, we will see Spring Data JPA example using spring boot. We will create a maven project from the beginning and implement Spring data using spring boot.

prerequisites –

  • JDK 1.8
  • Oracle 10g
  • Eclipse
  • maven
  • postman

We will use the spring boot library (will provide dependency in pom.xml) to make sure all necessary jar/library is easily available for our application. Spring boot will take care of all jar. Let’ s start.

Spring Data JPA example using spring boot.

Create a maven project.

Modify the pom.xml with below code.

 

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

 

Let maven download all necessary jar. Once it is done we will able to see maven dependency folder which contains different jar files.

 

We can start to write our controller classes, ServiceImpl and Repository. The folder structure of application-

Spring Data JPA example using spring boot

 

Define main class SpringDataExampleUsingOracle.java

SpringDataExampleUsingOracle.java

 

Define entity class i.e Book.java

Book.java

Define repository interface extending CrudRepository.

BookRepository.java

 

Define service interface i.e BookService.java

BookService.java

Define service impl class.

BookServiceImpl.java

 

Define controller class.

BookController.java

 

Define JpaConfig class.

JpaConfig.java

 

Define application.properties file

application.properties

 

 

We are almost done. Since we are using oracle as a database we need to use ojdbc14.jar. I tried to add the dependency in pom.xml but getting some problem, so we added it explicitly.

right click on application/properties/java build path/libraries/add external jar/add  ojdbc14.jar

Run the SpringDataExampleUsingOracle.java as java application. Our application will deploy and book table should created.

 

We can see tomcat started as in above screen.

Verify book table is created or not. If the book table is there just insert dummy record and later we will pass from postman and retrieve the record using Spring data.

Spring Data JPA example using spring boot

 

Open postman and perform get operation. We are performing only get operation here to keep this article simple. We will have a separate article where we will perform CRUD operation.

 

Spring Data JPA example using spring boot

 

That’s all about Spring Data JPA example using spring boot.

You may like.

Spring Data JPA docs see here.

Top