How to use Spring JPA MySQL | Spring Boot

In tradition approach, implementing Data Access Layer makes lots of boilerplate of code. Spring JPA is a part of Spring Data, helps us improve our codes and reduce efforts for development and maintenance. Spring JPA supports us the ways to write interface for repositories and custom finder methods, the implementation will be done automatically by Spring Framework.

In the tutorial, JavaSampleApproach will show you way to use Spring JPA MySQL using Spring Boot.

Related articles:
How to use Spring JPA with PostgreSQL | Spring Boot
How to use Spring JPA MariaDB | Spring Boot
How to access MySQL database with Spring Data Rest application
Angular 4 + Spring JPA + MySQL example | Angular 4 Http Client – Spring Boot RestApi Server

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.3.RELEASE

II. Overview
1. Project Structure

springboot  spring jpa mysql project overview

2. Step to do

– Create Spring Boot project & add Dependencies
– Configure Spring JPA
– Create DataModel Class
– Create Spring JPA Repository Interface
– Create Web Controller
– Create MySQL table
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project & add Dependencies

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then input project’s info. Press Next then Finish, a Spring Boot project will be created successfully.

Open pom.xml, add needed dependencies: Spring JPA, Web MVC, MySQL Connector

2. Configure Spring JPA


3. Create DataModel Class

Under package model, create class Customer.

Annotation @Entity indicates that Customer is an Entity and @Table specifies the primary table (name customer) for the annotated @Entity.

@ID specifies the primary key and @GeneratedValue indicates generation strategy for value of primary key.

@Column: mapped column (in the table) for persistent fields (in Java class).

We have 2 constructor methods:
– protected constructor will be used by Spring JPA.
– public constructor is for creating instances.

4. Create Spring JPA Repository Interface

This interface helps us do all CRUD functions for class Customer.

5. Create Web Controller

The controller receives requests from client, using repository to update/get data and return results.

Content of

In the web controller methods which are annotated by @RequestMapping, we have used some methods of autowired repository which are implemented interface CrudRepository:

and the method findByLastName that we create in our interface CustomerRepository.

6. Create MySQL table

7. Run Spring Boot Application & Enjoy Result

– Config maven build: clean install
– Run project with mode Spring Boot App
– Check results:
Request 1: http://localhost:8080/save
The browser returns Done and if checking database testdb with table customer, we can see some data rows has been added:
springboot spring jpa mysql

Request 2: http://localhost:8080/findall
springboot spring jpa mysql findall

Request 3: http://localhost:8080/findbyid?id=4
springboot spring jpa mysql findbyid

Request 4: http://localhost:8080/findbylastname?lastname=Smith
springboot spring jpa mysql findbylastname

III. Sourcecode


By JavaSampleApproach | January 5, 2017.

Last updated on August 29, 2017.

Related Posts

Got Something To Say:

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