- Setting Up the backend environment
- Creating the project with Spring Initializr
- Spring Boot JPA Configuration
In this series, we will create a modern web application using Spring Boot 2.1.5 and React. We will start from the backend creating an API in Spring Boot and using the PostgreSQL database. The frontend will be created using the React.
The application that we will develop will be a currency portfolio. We will be able to add, edit and delete currencies from the database. In addition, we will secure our application using the JWT token, so that we only have access to our data.
- Java knowledge
- Basic Spring Boot knowledge
- Basic Maven knowledge
- Basic knowledge of SQL databases
Setting Up the backend environment
We are going to create and run Spring Boot project. To do this you must have Java SDK installed (version 8 or higher) any IDE (I will use Intellij).
Creating the project with Spring Initializr
We will create the Spring Boot project with
Spring Initializr. It is a web tool that’s used to create Spring projects. You can use web-based initializr (https://start.spring.io/) or provided by IDE (in the case of Intellij).
We will generate a Maven project with the latest Spring Boot version (2.1.5). In the Dependencies section, we will select dependencies that are needed in our project. Please start with following dependencies:
- Web: Spring Web Starter
- JPA: Spring Data JPA
- H2 Database
- PostgreSQL Driver
- Spring Boot DevTools
The DevTools provides facilities for developers (e.g. automatic restart after changes). Development is much faster because the application is automatically restarted after new changes have been detected.
After all, your pom.xml should look something like this:
If you do not want to generate a project in the initializr - you can use my pom.xml.
Initializer has generated a directory structure with the main application class:
In addition to the usual class with the
main() method, you probably noticed the @SpringBootApplication annotation. It is a combination of multiple annotations. The most important of them are:
@EnableAutoConfiguration - tells Spring Boot how you will want to configure Spring, based on the jar dependencies that you have added to the pom.xml.
@ComponentScan - tells Spring to look for other components, configurations, and services in the specified package.
@Configure - Defines the class that can be used as a source of bean definitions.
Spring Boot JPA Configuration
Now, we will create a database by using JPA. Firstly, we will use H2 in-memory SQL database (good for fast development, we do not have to run any databases - Spring will do it for us, storing data in memory).
JPA and Hibernate
JPA (Java Persistent API) provides object-relational mapping for Java applications. In the case of databases, we are talking about entities and therefore the most important annotation in the case of JPA is
@Entity class presents the structure of a database table and the fields of an
@Entity class present the columns.
Hibernate is the most popular JPA implementation and is used in Spring Boot by default.
Creating the entity classes in Spring Boot
Entity classes use the standard JavaBean naming convention and have getters/setters. All fields have private visibility.
JPA creates tables with the names of the classes. If you want to use another name for the table, you should use @Table annotation
Let’s create a package for our Entity class and classes related to retrieving records from the database (repositories). We will use
domain name: net.devdiaries.wallet.domain. Next, we create entity classes - Our database will contain currency portfolios. My
@Id annotation defines the primary key. The
@GeneratedValue tells that the ID is automatically generated by the database. We use AUTO strategy. It means that the JPA provider selects the best strategy for a particular database.
Instead of creating getters/setters methods, we used Lombok (@Getter, @Setter), which will create it for us during compilation.
We already have a class that will create a table with columns in the database. Now it’s time to create Repository class - which provides functionality for Creating/Removing/Updating/Deleting data from the database. We will start from repository for
CurrencyRepository extends the
CrudRepository<Currency, Long>. It defines that this repository is for the Currency entity class and the type of Id is long.
CrudRepository provides multiple CRUD methods. Most important of them:
|long count()||returns number of entities|
|< S extends T > save(S entity)||saves an entity|
|Iterable ||returns all records of provided type|
|Optional ||returns one record for given ID|
|void delete(T entity)||deletes entity|
Let`s check that everything is done correctly:
1 - Add settings to application.properties
It enables the H2 console and logs all Hibernate queries.
2 - Run WalletApplication application
3 - Go to localhost:8080/h2-console and use
jdbc:h2:mem:testdb as JDBC URL:
4 - Use
SELECT * FROM CURRENCY query and click Run button. H2 console should return no rows:
CommandLineRunner to inject demo data
Now we will add some demo data to the H2 database. For that, we will use the
CommandLineRunner class. This class provides the functionality to run specific pieces of code when the application is fully started. Let’s use it in the main application class:
We injected CurrencyRepository using
@Autowired which enables dependency injection (described in https://www.devdiaries.net/blog/Spring-Design-Patterns-Dependency-Injection/ post).
Next, we used the
save() method from CrudRepository to save objects in the database.
Now repeat the 4. point described above. You will see that new records have appeared in the database:
The first part was done! In the next post, we will create a RESTful service. We will add the controller and use Spring MVC. The second part of the tutorial is available at PART 2.