How to access MySQL database with Spring Data Rest application

Spring Data REST is a combination between Spring HATEOAS and Spring Data JPA. So in the tutorial, JavaSampleApproach show you way to create a Spring Data Rest application to manipulate data with MySQL.

Related posts:
How to build SpringBoot MongoDb RestfulApi
How to use HAL Browser with Spring Data Rest | SpringBoot + MySQL
Spring Data Rest – How to create a RestAPIs with JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser

I. Technologies

– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE

II. Practice

In the tutorial, we use Spring Data REST and SpringBoot for building RestAPI application to access MySQL database.

Step to do:

– Create SpringBoot project
– Create Customer model
– Implement Customer repository
– Implement client to init database
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite to create a SpringBoot project. Then add dependencies:


2. Create Customer model

Create a Customer model:

package com.javasampleapproach.restdata.model;

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

public class Customer {
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String firstName;
	private String lastName;
	public Customer(){
	public Customer(String firstName, String lastName){
		this.firstName = firstName;
		this.lastName = lastName;
	public void setFirstName(String firstName){
		this.firstName = firstName;
	public String getFirstName(){
		return this.firstName;
	public void setLastName(String lastName){
		this.lastName = lastName;
	public String getLastName(){
		return this.lastName;

3. Implement Customer repository

Use @RepositoryRestResource of Spring Rest Data to build RestAPI repository:

package com.javasampleapproach.restdata.repository;

import java.util.List;


import com.javasampleapproach.restdata.model.Customer;

@RepositoryRestResource(collectionResourceRel = "customer", path = "customer")
public interface CustomerRepository extends PagingAndSortingRepository {
	List findByLastName(@Param("name") String name);

Open file to configure spring.datasource


4. Implement client for inital data

Implement the main class of SpringBoot application by using CustomerRepository to save data:

package com.javasampleapproach.restdata;

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

import com.javasampleapproach.restdata.model.Customer;
import com.javasampleapproach.restdata.repository.CustomerRepository;

public class SpringRestDataMySqlApplication implements CommandLineRunner{

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

	public void run(String... args) throws Exception { Customer("Jack", "Smith")); Customer("Adam", "Johnson")); Customer("Kim", "Smith")); Customer("David", "Williams")); Customer("Peter", "Davis"));

5. Run and check results

Build and run the SpringBoot application with commandlines {mvn clean install, mvn spring-boot:run}.

– Check the inital data by SELECT * FROM testdb.customer:

spring data rest - mysql - select all

Using PostMan to make requests:
– Select all customers:

spring data rest - rest - select all

– Get an individual customer:

spring data rest - rest - get individual customer

– Discover all queries:

spring data rest - rest - search queries

– Search customers by lastName param:

spring data rest - rest - search with lastname

POST a customer:

spring data rest - rest - post a customer

– Modify a customer:

spring data rest - rest - modify by Put

DELETE a customer

spring data rest - rest - delete

-> Final view with database:

III. Sourcecode


By grokonez | July 5, 2017.

Last updated on April 30, 2021.

Related Posts

5 thoughts on “How to access MySQL database with Spring Data Rest application”

  1. Awesome Videos which made me understand few of the basics in a short span in practical mode. Thanks you sooooo much.

  2. This is really awesome post I have seen. So simple and clear. But I have a question. How do you pass the page number and size though you used PagingAndSortingRepository?

Got Something To Say:

Your email address will not be published. Required fields are marked *