In this tutorials, we will see how to integrate Spring Boot with data JPA as  Spring Boot JPA Example.

Spring Boot JPA Integration :

Here we are going to use the below technologies:

  • Spring Boot 1.5.1.RELEASE
  • Spring Data JPA 1.11.0.RELEASE
  • Java 8
  • Mysql
  • STS

Spring Boot JPA Project Structure:

A typical maven project directory structure,

Spring Boot JPA


Here, the most important point is to define the dependencies for Spring Boot JPA related.

Unlike the spring MVC with hibernate integration, we do not need to define separate dependencies for both spring boot and spring data jpa.

A single spring-boot-starter-data-jpa brings all the dependencies for Spring boot and data jpa.

Since we are working with Mysql database, we should declare the dependency for MySQL connector too.

Recommended: Spring Boot with JPA multiple Data Sources Example

<project xmlns="" xmlns:xsi=""
  <properties>         <!-- Generic properties -->
      <name>Spring Releases</name>
      <name>JBoss Maven Release Repository</name>
      <name>Spring Releases</name>

Create person table :

Database Table

Creating a property file, having all database configuration details:

The above-mentioned properties are provided by spring-boot, typically called as application-level properties to manage our application configurations.

By using these properties, we can eliminate the boilerplate code to configure the data sources and transactions.

Creating a repository interface.
package com.onlinetutorialspoint.repository;
import org.springframework.stereotype.Repository;

import com.onlinetutorialspoint.entity.Person;
public interface PersonRepository extends CrudRepository<Person, Long>{

CrudRepository is an interface given by the spring data, which gives us the basic crud operations on a repository for a specific type.

In any typical hibernate using applications like spring with hibernate integration, we need to write all the CRUD operations of our own. Whereas in Spring Data JPA, CrudRepository provides in-built CRUD operations for the entity class.

Creating Entity class :
package com.onlinetutorialspoint.entity;

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

@Table(name = "person")
public class Person {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String city;

    public Person() {
        // TODO Auto-generated constructor stub

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCity() {
        return city;

    public void setCity(String city) { = city;

    public String toString() {
        return "Person [pid=" + id + ", pName=" + name + ", pCity=" + city
                + "]";


Creating a service class :
package com.onlinetutorialspoint.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.onlinetutorialspoint.dto.PersonDTO;
import com.onlinetutorialspoint.entity.Person;
import com.onlinetutorialspoint.repository.PersonRepository;

public class PersonService {
    PersonRepository personRepo;

    public void savePersonDetails(PersonDTO personDto) {
        try {
            Person person = new Person();
        } catch (Exception e) {

    public List getAllPersons() {
        return (List) personRepo.findAll();

    public Person getPerson(long id) {
        return personRepo.findOne(id);

    public Person savePerson(Person person) {

Here @Service is a spring stereotype annotation, used to tell the spring context, it is a service layered class.

Creating DTO class :
package com.onlinetutorialspoint.dto;

public class PersonDTO {
    private Long pid;
    private String pName;
    private String pCity;
    public Long getPid() {
        return pid;
    public void setPid(Long pid) { = pid;
    public String getpName() {
        return pName;
    public void setpName(String pName) {
        this.pName = pName;
    public String getpCity() {
        return pCity;
    public void setpCity(String pCity) {
        this.pCity = pCity;

Spring Boot Application class :
package com.onlinetutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.onlinetutorialspoint.entity.Person;
import com.onlinetutorialspoint.repository.PersonRepository;
import com.onlinetutorialspoint.service.PersonService;

public class Application {
    public static void main(String[] args) {;
    PersonService personService;
    public CommandLineRunner run(PersonRepository repository) {
        return (args) -> {
            Person person = new Person();
            person.setName("Chandra Shekhar Goka");
            Person p = savePersonDetails(person);
            System.out.println("Person Id : "+p.getId() +" Person Name : "+p.getName());
    public Person savePersonDetails(Person p){
        return personService.savePerson(p);
    public Person getPerson(Person person){
        return personService.getPerson(person.getId());

@SpringBootApplication annotation tells to Spring Boot, this is a starting point of our Spring Boot Application.

CommandLineRunner is an interface provided by the spring boot. It is used to indicate that a bean should run when it is contained within a spring application.

In the run method, I have created a Person class and call a savePerson method on PersonService class in Java 8 lambda expression to save the person details.

Let’s run the to save the person details. You can run this application as simple as Java standalone application by running the main class.

If everything goes well you will see the output like this:

Spring Boot JPA Output

On the above output, you can see the SQL command for insert person data.

Happy Learning:)

Download Example