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:

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

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

List 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


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

  1. I have to thank you for the efforts you’ve put in penning
    this blog. I really hope to check out the same high-grade blog
    posts by you later on as well. In truth, your creative writing abilities has encouraged
    me to get my own, personal blog now 😉

  2. Hello there, jᥙhst became alert to yoг lοg through Google, and
    fοund that it’s really informative. I am going to watch out foг brussels.
    I’lⅼ appreciate if you continue this іn futurе. A lot
    of people will bе benefited fгom your writing.

  3. Thanks a lot for sharing this with all folks you actually recognize what you’re speaking approximately!
    Bookmarked. Kindly additionally discuss with my web site =).

    We could have a hyperlink exchange agreement among us

  4. Hеy there! I just wanted tto ask if you eveer һaᴠe any trouble with hackers?
    My last blog (wordpress) was һacoeԁ and I еnded up losing
    months of hard work Ԁue to no back up. Do you have any solutions to protect against hackers?

  5. I don’t even knoow the way I ended up right here, however I thought this submit was once good.
    I do not understand whoo you might ƅe howеver certainly you’re going to a well-known blogger when yoou
    aren’t already. Cheers!

  6. Hеllo, i read your blog froom time to time
    and i own a similar oone and i wass ϳust wondering if you get a
    lot of spam feedback? If so how do үou protect against it, any plkugin օr anything yoᥙ can recommend?
    I get so much latelyy it’s dгiving me crrɑzy so any assisetance iѕ ѵerу much appreciated.

  7. Woww tһat was odd. I juѕt ԝrote an very long comment but ɑfter I clicкed submit my comment didn’t show up.
    Grгrr… well I’m noօt writing all that oveг
    again. Anyway, just wanted tto say great blog!

  8. It’s a pity you don’t have a donate button! I’d without a doubt
    donate to this excellent blog! I guess for now i’ll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to fresh
    updates and will talk about this website with my Facebook group.

    Talk soon!

  9. I pay a quick visit each day some blogs and websites to read articles
    or reviews, but this weblog provides quality based content.

  10. Howdy! This post couldn’t be written much better! Going through this article reminds me of my
    previous roommate! He continually kept preaching about this.
    I’ll forward this post to him. Pretty sure he’s going to have a great read.
    Thank you for sharing!

  11. I think tһat is among the such a lot important info forr me.
    And i’m gⅼad sudying your article. But want tto statement
    on few comҝmon issues, The web site taste is perfect, the
    articlеs is really nice : D. Just right process, cheers

  12. What’s Happening i’m new to this, I stumbled upon this I have
    found It positively useful and it has helped me out loads.
    I hope to give a contribution & aid different customers like its helped me.
    Great job.

  13. Pretty section of content. I just stumbled upon your
    weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts.

    Anyway I’ll be subscribing to your augment and even I achievement you access consistently

  14. Hi there, just became aware of your blog through Google, and found that it is truly
    informative. I’m gonna watch out for brussels. I will appreciate if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  15. The very next time I read a blog, I hope that it won’t disappoint me as much as this one. I mean, I know it was my choice to read, nonetheless I genuinely believed you’d have something useful to say. All I hear is a bunch of complaining about something you could possibly fix if you were not too busy seeking attention.

  16. After going over a few of the blog articles on your site, I really like your
    way of blogging. I bookmarked it to my bookmark website list and will
    be checking back soon. Please check out my
    website as well and tell me how you feel.

  17. After exploring a handful of the blog posts on your web
    site, I truly like your way of blogging. I book-marked it to my bookmark webpage list and will be checking back soon. Please visit
    my website too and tell me your opinion.

Leave a Reply

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