With the fourth release of Spring Boot, developing enterprise applications for the web has become so much easier. Something that still requires a lot of time on the developer's behalf is modeling an existing database in for an example Hibernate to get an object-oriented view of the data. In this tutorial, we are going to explore how to use the open-source tool Speedment together with Spring to generate entities, managers, and controllers with the press of a button, enabling you to get started with the development so much faster.

About Speedment

Speedment is an open-source Java toolkit that makes it possible for a developer to rapidly generate all the glue required to communicate with a database. Using a graphical tool, you can connect to a database and generate Java sources in seconds. Speedment is built in a modular fashion, just like Spring, making it easy to learn and use only the parts you are interested in. In this article, we are going to use a plugin for Speedment to generate Spring controllers in addition to the standard files.

Step 1: Creating a New Spring Boot Project

Spring Boot consists of a number of templates that make it easy to get started with a new application. We are going to use one called spring-boot-starter-web to set the stage for our web application.

Begin by creating a new Maven Project and add the following to your pom.xml file:

This will configure your project as a Spring Boot application and tell it to use Speedment with the Spring Generator Plugin.

Step 2: Using Speedment to Generate Sources

Once the pom-file has been modified, a number of new Maven goals will be available in the IDE. Speedment can be used both graphically or from the command line. In this tutorial, we are going to use the UI. To start the Speedment Tool, execute the following Maven goal on the project:

mvn speedment:tool

A dialog will open that let you connect to a database. Once connected, you will see a window with an overview of the database on the left and various configuration options in the center. For this tutorial the default settings will suffice, so simply press Generate in the toolbar.

If you switch back to the IDE you will see the newly generated sources. You will notice that every class exists in two copies, one of them with the generated prefix. The reason for this is to allow modifications without the risk of overwriting your changes should you need to regenerate the sources at some point. Files with the generated prefix will always be overwritten and files without it will only be created once.

Step 3: Create a Main File

Speedment has generated a complete object-oriented model of the database, but we still need to create an entry point for the application. We will put this in the main package and call it Main.java.

If we start the application, Spring Boot will set up a self-contained web application with a generated controller for every table in the database. We can try it out by going to the following path in a browser:

http://localhost:8080/hare/

A JSON representation of the hare table in my database will now be returned.

There was an unexpected error (type=Internal Server Error, status=500).
Could not write content: No value present (through reference chain:
java.util.ArrayList[0]...

...it probably means that you have nullable columns in your database that Speedment chooses to implement as OptionalLong, OptionalInt etc. You can turn this feature off in the Speedment Tool by setting the "Nullable Implementation" field to WRAPPER instead of OPTIONAL for those columns.

...you will need to create an application.properties-file in the root of the project and add the authentication details for your database.

application.properties

jdbc.username=root
jdbc.password=password

Summary

In this article, we have used Speedment and the Spring Generator plugin to automatically create a complete Spring Boot Application. Speedment has generated entities, managers, and REST controllers for communicating with the database. If you want to know more about Speedment and how you can control the generated code, check out the many examples at the Speedment GitHub page!