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:

package com.javasampleapproach.mariadb.model;


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

@Table(name = "customer")
public class Customer implements Serializable {

	private static final long serialVersionUID = -3009157732242241606L;
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@Column(name = "firstname")
	private String firstName;

	@Column(name = "lastname")
	private String lastName;

	protected Customer() {

	public Customer(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;

	public String toString() {
		return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);

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.

package com.javasampleapproach.mariadb.repo;

import java.util.List;


import com.javasampleapproach.mariadb.model.Customer;

public interface CustomerRepository extends CrudRepository{
	List findByLastName(String lastName);

Open, 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

package com.javasampleapproach.mariadb.controller;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.mariadb.model.Customer;
import com.javasampleapproach.mariadb.repo.CustomerRepository;

public class WebController {
	CustomerRepository repository;
	public String process(){ Customer("Jack", "Smith"), 
										new Customer("Adam", "Johnson"),
										new Customer("Kim", "Smith"),
										new Customer("David", "Williams"),
										new Customer("Peter", "Davis")));
		return "Done";
	public String findAll(){
		String result = "";
		for(Customer cust : repository.findAll()){
			result += cust + "
"; } return result; } @GetMapping("/findbyid") public String findById(@RequestParam("id") long id){ String result = ""; result = repository.findOne(id).toString(); return result; } @GetMapping("/findbylastname") public String fetchDataByLastName(@RequestParam("lastname") String lastName){ String result = ""; for(Customer cust: repository.findByLastName(lastName)){ result += cust + "
"; } return result; } }

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

<S extends T> Iterable<S> save(Iterable<S> entities); //for @GetMapping("/save")
T findOne(ID id); //for @GetMapping("/findbyid")
Iterable<T> findAll(); //for @GetMapping("/findall")

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

List<Customer> findByLastName(String lastName);

5. Deployment

Open MariaDB client, create jsadb database:

create database jsadb

Then creating customer table:

> use jsadb;

> CREATE TABLE customer(
    firstname VARCHAR(20) NOT NULL,
    lastname VARCHAR(20) NOT NULL,

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


11 thoughts on “MariaDB – How to use Spring JPA MariaDB by Spring Boot”

  1. I must voice my affection for your kindness supporting people who must have guidance on your concept. Your personal commitment to getting the message all over had become extremely helpful and have empowered guys and women much like me to attain their objectives. Your new insightful guidelines indicates a lot a person like me and a whole lot more to my office colleagues. Many thanks; from each one of us.

  2. My brother recommended I may like this website. He was totally right. This put up actually made my day. You can not consider just how a lot time I had spent for this information! Thank you!

  3. hey there and thank you to your information ?I have definitely picked up anything new from proper here. I did alternatively expertise a few technical points using this web site, as I skilled to reload the website lots of instances prior to I may just get it to load correctly. I were thinking about if your web hosting is OK? Now not that I am complaining, but slow loading instances times will often affect your placement in google and can injury your high quality ranking if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Well I am adding this RSS to my e-mail and could glance out for a lot extra of your respective exciting content. Ensure that you replace this once more soon..

  4. I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work!

  5. certainly like your web site but you have to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very troublesome to tell the truth nevertheless I will surely come back again.

  6. Heya just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.

  7. Excellent items from you, man. I’ve keep in mind your stuff previous to and you are just extremely great. I really like what you’ve acquired right here, certainly like what you’re saying and the best way through which you say it. You are making it enjoyable and you still take care of to stay it sensible. I cant wait to read far more from you. This is really a wonderful web site.

  8. Hey! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no back up. Do you have any methods to protect against hackers?

  9. I cherished up to you will obtain performed proper here. The caricature is attractive, your authored subject matter stylish. nonetheless, you command get bought an impatience over that you would like be delivering the following. in poor health unquestionably come further formerly once more since exactly the same nearly very often inside case you shield this increase.

Leave a Reply

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