Search Tutorials

Spring Boot Tutorial-Spring Data JPA Simple Example | JavaInUse

Spring Boot Tutorial-Spring Data JPA Simple Example

In this post we create a Employee management system which adds and retrieves employee details.


This tutorial is explained in the below Youtube Video.

Lets Begin-

We make use of the h2 database. Maven will be as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->









Create the as follows-
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {, args);

Create the Entity class as follows-
package com.javainuse.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Employee {
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	private String name;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getDept() {
		return dept;

	public void setDept(String dept) {
		this.dept = dept;

	private String dept;

	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";

The Controller we define methods to add Employee record and display employee records as list. Define the controller as follows-
package com.javainuse.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.javainuse.model.Employee;

public class EmployeeController {

	private EmployeeRepository employeeData;

	@RequestMapping(value = "/addNewEmployee.html", method = RequestMethod.POST)
	public String newEmployee(Employee employee) {;
		return ("redirect:/list.html");


	@RequestMapping(value = "/addNewEmployee.html", method = RequestMethod.GET)
	public ModelAndView addNewEmployee() {

		Employee emp = new Employee();
		return new ModelAndView("newEmployee", "form", emp);


	@RequestMapping(value = "/listEmployees.html", method = RequestMethod.GET)
	public ModelAndView employees() {
		List<Employee> allEmployees = employeeData.findAll();
		return new ModelAndView("allEmployees", "employees", allEmployees);



Next we define the EmployeeRepository which is an interface that extends the Spring Framework class JpaRepository. JpaRepository class is a generics and takes the following two parameters as arguments-
  • What type of Object will this repository be working with- In our case Employee
  • Id will be what type of object- Long(since id defined in the Employee class is long)
Thats the only configuration required for the repository class. The required operations like adding and retrieving employee details from DB will now be handled.


import com.javainuse.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee
			, Long> {

Define the newEmployee.jsp
<%@ taglib uri="" prefix="c" %>

<h1>Employees page</h1>

<c:forEach items="" var="employee">

Define the allEmployees.jsp
<%@ taglib prefix="spring" uri=""%>
<%@ taglib prefix="form" uri="" %>
    <h1>Add new employee</h1>
    <form:form modelAttribute="form">
        <form:errors path="" element="div" />
            <form:label path="name">Name</form:label>
            <form:input path="name" />
            <form:errors path="name" />
            <input type="submit" />

The will be as follows-


Compile and the run the as a Java application.
Go to localhost:8080/addNewEmployee.html
Click add. We will be directed to the allEmployees.jsp


Download Source Code

Download it -
Spring Boot JPA Data Application

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Hello World Application- Create simple controller and jsp view using Gradle Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions