Spring MVC RESTful with JPA, Hibernate & MySQL database beginner in 13 steps

This tutorial combines both

1) Spring 4 MVC RESTful Web Service Beginner Tutorial step by step, which focuses on the RSTful web service and

2) JPA with Hibernate & MySQL database tutorial step by step, which focuses on JPA and Hibernate.

Controller –> Service Layer –> DAO Layer –> JPA/Hibernate –> MySQL Database

Step 1: Create a maven based Java project from a command-line.

Step 2: Import the project into Eclipse as “File” –> “Import” –> “Existing Maven Projects”.

Step 3: The pom.xml with “jackson-databind“, which will be used to convert the response data into JSON string, and “jackson-dataformat-xml“, which will be used to convert the response data to XML. Spring MVC, JPA, and Hibernate libraries are included as well.

Define the model class Account.java

Step 4: Define the Account.java with 3 fields & getters/setters in the package “com.mytutorial.model”. This has JPA annotations to map “learnjavadb” databse table “account” columns via Hibernate as implementation.

Define the controller class AccountController.java

Step 5: Define the AccountController.java to handle HTTP requests in the package “com.mytutorial.controller”.

Spring Java Configs for wiring up

Step 6: Spring Java configs in the package “com.mytutorial.config”

Bootstrap the Spring dispatcher Servlet with the mapping URI of “/entry/*” & the above “SimpleWebConfiguration” for the controllers

Step 7: The Service layer that talks to the DAO layer. Create a package “com.mytutorial.service” and add the following interface & implementation.

Step 8: Before writing the DAO layer, define the “persistence.xml” in “src/main/resources/META-INF” folder.

Step 9: Use “jpa-example” persistent unit to get an entity manager. Create a package “com.mytutorial.util”.

Step 10: The DAO layer that talks to the MySQL database. Create a package “com.mytutorial.dao” and add the following interface & implementation.

Build simpleWeb.war & deploy to Tomcat

Step 11: Copy the “simpleWeb.war” to Tomcat’s “webapps” folder and start the Tomcat server.

Start the MySQL database server

Step 12: The dataase server must be running. The “learnjavadb” database was already created with the table “account”.

Start the RESTClient from WizTools.org

Step 13: Double-Clicking “c:\tools\restclient-ui-3.2.2-jar-with-dependencies.jar” on windows or

RESTClient from WizTools to test RESTful web services

XML Data

http://localhost:8080/simpleWeb/entry/v1/forecasting/accounts

JSON Data

http://localhost:8080/simpleWeb/entry/v1/forecasting/accounts.json


🔥 300+ Java Interview FAQs

Java & Big Data Tutorials

Top