Getting Started with Spring Boot, Hibernate, Jersey, and MySQL: Part 1

Adam Zink
4 min readMay 1, 2018

--

Spring Initializr is a handy tool that generates a Spring Boot skeleton project, and with a little extra configuration and coding, it is easy to complete the remaining pieces and create a functional REST API serving JSON data from MySQL.

Audience

Anyone who wants to learn how to create and run a Spring Boot application with MySQL and Flyway.

Java programming experience is helpful but not required.

Requirements

Let’s get started!

To create a Spring Boot skeleton project, go to Spring Initializr and choose the options below, then click Generate Project.

But it doesn’t work yet

After unzipping, building, and running SpringBootMysqlDemoApplication with IntelliJ, I get this error:

***************************
APPLICATION FAILED TO START
***************************
Description:Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.Reason: Failed to determine a suitable driver class

The MySQL properties are missing from application.properties.

First, create a new database using tool like MySQL Workbench:

create database MYSQL_DEMO;

Also, make sure to create a user account that has permission to modify the database.

Then, tell Spring Boot to look for the database by adding these lines to application.properties:

# MySQL properties
spring.datasource.url=jdbc:mysql://localhost:3306/MYSQL_DEMO
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

But it still doesn’t work

The second attempt to run the application gives a different error about Flyway:

java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)

Flyway requires a folder to look for database migration scripts. If there are new scripts that have not been applied to the database, then it will run them and log the script in a special flyway_schema_history table.

The default location of the folder can be configured as a property. For example, I want to have a specific MySQL migration folder in case I want to support another database in the future.

The MySQL-specific folder goes under src/main/resources and the new folder structure is created as db/mysql/migration.

In order for Flyway to find this folder, add the following lines to application.properties:

# Flyway properties
spring.flyway.locations=classpath:db/mysql/migration

We get the same error again

It turns out Flyway requires at least one migration script to initialize the database. Let’s create a USER table that we can use for our REST API. Add a script called V1_0__USER.sql and add the following lines:

CREATE TABLE IF NOT EXISTS MYSQL_DEMO.USER (
ID INT NOT NULL AUTO_INCREMENT,
FIRST_NAME VARCHAR(50) NOT NULL,
LAST_NAME VARCHAR(50) NOT NULL,
ADD_TS DATETIME(6) NOT NULL,
PRIMARY KEY (ID))
ENGINE = InnoDB;

It starts!

We finally have a running Spring Boot application at http://localhost:8080/

2018-04-22 23:33:55.010  INFO 8508 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-22 23:33:55.010 INFO 8508 --- [ main] c.g.a.s.SpringBootMysqlDemoApplication : Started SpringBootMysqlDemoApplication in 14.208 seconds (JVM running for 15.343)

The application doesn’t do much aside from the default 404 page, but now Flyway is working for us. The Spring Boot startup log shows the migration being applied, and MySQL Workbench shows the new row in flyway_schema_history.

2018-04-22 23:33:50.633  INFO 8508 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table: `MYSQL_DEMO`.`flyway_schema_history`
2018-04-22 23:33:51.420 INFO 8508 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `MYSQL_DEMO`: << Empty Schema >>
2018-04-22 23:33:51.426 INFO 8508 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `MYSQL_DEMO` to version 1.0 - USER
2018-04-22 23:33:51.488 INFO 8508 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema `MYSQL_DEMO` (execution time 00:00.856s)

--

--