In this tutorial we will learn how to create a basic application using Spring Boot CLI and MySQL as database. We will then import the application into an IDE to create a sample application which stores some data which is later retrieved by the SpringApplication class.

We wil start from Spring Command Line Interface. You can however surf to the Spring Initializr Web application if you prefer.
Our Web application, named samplewebapp, will require the following dependencies as specified by spring init:

$ spring init -d=web,jpa,mysql -n=jpa-demo.mysql jpa-demo-mysql

As you can see, in order to include mysql dependencies in your project it is sufficient to pass "mysql" to the "-d" (dependencies) and you will have the dependencies in your pom.xml

Now let's check what has been created for us:

├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── jpademomysql
    │   │               └──
    │   └── resources
    │       ├──
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── example
                    └── jpademomysql

And here is our pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath/> <!-- lookup parent from repository -->






The next thing to do is including in the file the Database JDBC Settings:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver = org.hibernate.dialect.MySQL5Dialect

As it is, the application won't do much so let's add an Entity to our project named Person:

package com.example.samplewebapp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Person {
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    String name;
    String surname;
    public Person(String name, String surname) {
        super(); = name;
        this.surname = surname;

    public Person() {

    public Long getId() {
        return id;

    private void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getSurname() {
        return surname;

    public void setSurname(String surname) {
        this.surname = surname;

    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";


One of the coolest features of Spring is the ability to create repository implementations automatically, at runtime, from a repository interface. Out of the box the repository interface already contains methods to find an Entity by id and to findAll Entity. We will add one more method named "findBySurname" which is defined in the following PersonRepository interface:

package com.example.samplewebapp;

import java.util.List;


public interface PersonRepository extends CrudRepository<Person, Long> {

    List<Person> findBySurname(String surname);

We are done with the backend. Now we will add in the @SpringBootApplication class some logic to insert a few Entity objects and retrieve them using the repository interface:

package com.example.samplewebapp;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemoApplication {
    private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {, args);
    public CommandLineRunner demo(PersonRepository repository) {
        return (args) -> {
            // save a couple of persons
   Person("Jack", "Smith"));
   Person("Joe", "Black"));
   Person("Martin", "Bauer"));

            // fetch all persons
  "Persons found with findAll():");
            for (Person person : repository.findAll()) {

            // fetch an individual person by ID
                .ifPresent(person -> {
          "Person found with findById(1L):");

            // fetch persons by last name
  "Person found with findBySurname('Black'):");
            repository.findBySurname("Black").forEach(smith -> {


As you can see, we have used the CommandLineRunner interface to run the demo. As a matter of fact, Spring Boot provides two interfaces, CommandLineRunner and ApplicationRunner to run specific pieces of code when an application is started. These interfaces get called just before run() once SpringApplication completes.
CommandLineRunner, used in our example, provides access to application arguments as string array.
You can run the application directly from your tool, for example if you are using Eclipse with Spring Tools, right click on the DemoApplication class and choose Run As > SpringBoot app

As an alternative, simply running:

mvn clean install


java -jar target/samplewebapp-0.0.1-SNAPSHOT.jar 

Here is the output provided by the SpringApplication class:

Persons found with findAll():

Person [id=1, name=Jack, surname=Smith]
Person [id=2, name=Joe, surname=Black]
Person [id=3, name=Martin, surname=Bauer]

Person found with findById(1L):
Person [id=1, name=Jack, surname=Smith]

Person found with findBySurname('Black'):
Person [id=2, name=Joe, surname=Black]

In this tutorial we have learned how to develop a basic application using Spring Boot API and javax.persistence API to store some data and use a repository class to retrieve them.

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials