systemctl status mysql
command.mysql_secure_installation
to increase security of MySQLserver. We are given the choice to improve the MySQL root password, removeanonymous user accounts, disable root logins outside of localhost, and removetest databases.testdb
database.testdb
database.cities
.cities
table.source
command, we execute the SQL statements.spring-boot-starter-parent
has some common configurations for aSpring Boot application. The spring-boot-starter-web
is a starterfor building web, including RESTful, applications using Spring MVC. Thespring-boot-starter-freemarker
is a starter for building MVC webapplications using Freemarker views. The spring-boot-starter-data-jpa
is a starter for using Spring Data JPA with Hibernate.mysql-connector-java
dependency is for the MySQL database driver.spring-boot-maven-plugin
provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives. Its spring-boot:run
goal runs the Spring Boot application.application.properties
file we write various configurationsettings of a Spring Boot application. With the spring.main.banner-mode
property we turn off the Spring banner. With the logging.level.org.springframework
we set the logging level for spring framework to ERROR
.In the spring datasource properties we set up the MySQL datasource.City
entity. Each entity must have at least twoannotations defined: @Entity
and @Id
.@Entity
annotation specifies that the class is anentity and is mapped to a database table while the @Table
annotationspecifies the name of the database table to be used for mapping.@Id
annotation.The @GeneratedValue
gives a strategy for generating the values of primary keys.CrudRepository
, we will havesome methods for our data repository implemented, including findAll()
.This way we save a lot of boilerplate code.ICityService
provides the findAll()
contractmethod declaration to get all cities from the data source.CityService
contains the implementation of the findAll()
method. We use the repository to retrieve data from the database.CityRepository
is injected.findAll()
method of the repository returns the list ofcities.MyController
class is annotated with @Controller
.ICityService
into the countryService
field./cities
path to the controller'sfindCities
method. The @GetMapping
annotation maps a GET request to the method. The model gains a list of cities and the processing issent to the showCities.ftlh
Freemarker template file.showCities.ftlh
template file, we display the data inan HTML table.index.html
there is a link to show all cities.Application
sets up the Spring Boot application.The @SpringBootApplication
enables auto-configuration andcomponent scanning.localhost:8080
.