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:

3. Implement Customer repository

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

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:

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


Related Posts

Got Something To Say:

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