Introduction to Displaying Data
Explore how to display data using Thymeleaf in a Spring Boot app by generating random users for testing, setting up the UserService interface, and configuring your database connection properties. This lesson helps you understand database initialization and service layer integration to prepare your application for data display.
We'll cover the following...
Generate random users
The first thing we need is a few users in our database for testing. We could create a database script to do that, but it’s easier to write a simple Java class that can create things in a loop.
Let’s start by creating a CommandLineRunner implementation. Spring will run any such beans at the startup of the application. By only enabling the bean when the init-db profile is active, we can toggle if the database should be populated at startup or not.
This is the code for the DatabaseInitializer:
- Only have this
@Componentactive when theinit-dbprofile is active. - Implement
CommandLineRunnerinterface so that