MariaDB – How to use Spring JPA MariaDB by Spring Boot

In tradition approach, implementing Data Access Layer makes lots of boilerplate 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.
About MariaDB, it is an open source leader, provides the solutions to run on all infrastructure.

So in the tutorial, JavaSampleApproach shows you how to create Spring JPA MariaDB application using Spring Boot.

Related posts:
How to use Spring JPA with PostgreSQL | Spring Boot
How to use Spring JPA MySQL | Spring Boot

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– MariaDB 10.2.7

II. Overview

1. Goal

spring boot spring jpa - mariadb - architecture

2. Project Structure

spring jpa mariadb - project structure

3. Step to do

– Create Spring Boot project
– Create DataModel Class
– Create Spring JPA Repository Interface
– Create Web Controller
– Deployment

III. Practice

1. Create Spring Boot project

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, Mariadb Client:

2. 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.

3. Create Spring JPA Repository Interface

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

Open application.properties, configure spring.datasource and spring.jpa:

4. Create Web Controller

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

Content of WebController.java:

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

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

5. Deployment

Open MariaDB client, create jsadb database:

Then creating customer table:

spring jpa  mariadb create table

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

Make requests:

– Request 1: http://localhost:8080/save:

spring jpa - mariadb select all

– Request 2: http://localhost:8080/findall:

springboot jpa mariadb - select all request

– Request 3: http://localhost:8080/findbyid?id=1

springboot jpa mariadb - select one customer

– Request 4: http://localhost:8080/findbylastname?lastname=Smith

springboot jpa mariadb - find customer by lastname

III. Sourcecode

SpringBootJPAMariaDB

By JavaSampleApproach | August 14, 2017.


Related Posts


Got Something To Say:

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

*