This is a Spring Boot tutorial to use Spring Data JPA to access the database. In this post, we will introduce how to access data in MySQL by Spring Data JPA.

It is easy to get started with using Spring Data JPA to operate data in MySQL, but there are still a lot of things involved. Even you are newer to learn Spring Boot, it’s also easy to learn that.

Starting the project

Firstly, you can create a new Spring Boot project by Spring Initializr or IDE. (Read: Spring Boot Getting Started Tutorial)

This JPA demo project needs the Spring Web Starter, Spring Data JPA, Test, Lombok, and MySQL Driver dependencies. The pom.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>





Configure database connection and JPA configuration

Then, go to configure database connection information and JPA configuration.

The content of

#print the sql sentence

About spring.jpa.hibernate.ddl-auto=createthis configuration option, there are four common options for this attributor:

  • create: Every time you restart the project, the table structure will be re-innovated, which will result in data loss.
  • create-drop: Create the table structure every time you start the project, delete the table structure when you close the project
  • update: The table structure updated every time when the project is started
  • validate: Verify the table structure without making any changes to the database

Create a database in MySQL

mysql> create database springboot_jpa;

Entity Class

Now, we need to create an Entity class,  it is a persistent class.

package com.etbye.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

public class Person {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String name;
    private Integer age;

    public Person(String name, Integer age) { = name;
        this.age = age;

By now, you can run the project and view the output to verify that you completed the above steps correctly.

The SQL statement printed by the console looks like this:

Hibernate: drop table if exists person
Hibernate: create table person (id bigint not null auto_increment, age integer, name varchar(255), primary key (id)) engine=InnoDB
Hibernate: alter table person add constraint UK_p0wr4vfyr2lyifm8avi67mqw5 unique (name)

When you see the output info like that, then you can go the following steps.

Create a Repository interface

We need to Create a Repository interface for the operational database. Usually, the new repository extends the JpaRepository interface. JpaRepository contains some database operations.

You can see the detail of the JpaRepository interface:


import java.util.List;

public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
    List<T> findAll();

    List<T> findAll(Sort var1);

    List<T> findAllById(Iterable<ID> var1);

    <S extends T> List<S> saveAll(Iterable<S> var1);

    void flush();

    <S extends T> S saveAndFlush(S var1);

    void deleteInBatch(Iterable<T> var1);

    void deleteAllInBatch();

    T getOne(ID var1);

    <S extends T> List<S> findAll(Example<S> var1);

    <S extends T> List<S> findAll(Example<S> var1, Sort var2);

Here, we create a PersonRepository interface that extends from JpaRepository.

package com.etbye.repository;

import com.etbye.entity.Person;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

public interface PersonRepository extends JpaRepository<Person, Long> {

    //find Person by name
    Optional<Person> findByName(String name);
    //find Person by age range
    List<Person> findByAgeGreaterThan(int age);

    //Custom SQL qeruy
    @Query("select from Person p where = :id ")
    String findPersonNameById(@Param("id") Long id);

    //Custom SQL qeruy
    @Query("select p from Person p where = :name")
    Optional<Person> findByNameCustomQuery(@Param("name") String name);

    //Custom SQL qeruy
    @Query("update Person p set = ?1 where = ?2")
    void updatePersonNameById(String name, Long id);

Test class

We have added some query methods in our PersonRepository interface. Now, we will go to test its wether works fine.

package com.etbye;

import com.etbye.entity.Person;
import com.etbye.repository.PersonRepository;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

public class DemoApplicationTests {

    private PersonRepository repository;
    private Long id;

     * save Person data to database
    public void setUp() {
        Person person = new Person("Jannies", 25);
        Person savedPerson = repository.saveAndFlush(person);;
        id = savedPerson.getId();

     * to find Person by JPA
    public void should_get_person() {
        Optional<Person> person = repository.findById(id);
        assertEquals("Jannies", person.get().getName());
        assertEquals(Integer.valueOf(25), person.get().getAge());

        List<Person> personList = repository.findByAgeGreaterThan(18);
        assertEquals(1, personList.size());


     * find Person by custom SQL queries
    public void should_get_person_use_custom_query() {
        Optional<Person> person = repository.findByName("Jannies");
        assertEquals(Integer.valueOf(25), person.get().getAge());

        String name = repository.findPersonNameById(id);
        assertEquals("Jannies", name);

        repository.updatePersonNameById("UpdatedName", id);
        Optional<Person> updatedPerson = repository.findByNameCustomQuery("UpdatedName");



Run the tester class, if all tests work fine, meaning the custom query methods working fine.

Spring Data JPA to access the MySQL

Good luck!