In this tutorial, we will learn how to develop a Spring MVC web application using Spring Boot, Thymeleaf, Hibernate, JPA, Maven, and MySQL database.
We are building a simple Spring MVC web application using Thymeleaf as a view. Output: HTML page using Thymeleaf which displays a list of users from MySQL database.
There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.
Add below Maven dependencies to your Spring Boot project:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency>
Here is the project structure for your reference:
Let's create a User JPA entity with the following content into it:
import jakarta.persistence.*; @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name; public User() { } public User(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Let's create UserRepository interface that extends JpaRepository interface:
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository{ }
Let's create HomeController Spring MVC controller with home() handler method that returns index Thyemelaf view:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HomeController { @Autowired UserRepository userRepo; @RequestMapping("/") public String home(Model model) { model.addAttribute("users", userRepo.findAll()); return "index"; } }
Configure application.properties to connect to your MySQL database.
Let's open an application.properties file and add the following database configuration to it.
logging.level.org.springframework=INFO ################### DataSource Configuration ########################## spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/users_database spring.datasource.username=root spring.datasource.password=root ################### Hibernate Configuration ########################## spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
Create a message.properties under the resources folder and add the following content to it -
app.title=SpringMVC JPA Demo (With SpringBoot)
Once you will run this application will create users table in a database and use the below insert SQL script to populate a few records in a users table.
INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('1', 'Salman'); INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('2', 'SRK'); INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('3', 'AMIR'); INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('4', 'Tiger'); INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('5', 'Prabhas');
Let's create a Thymeleaf view to show the list of users. Locate the index.html file under src/main/resources/templates folder of this project.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"/> <title>Home</title> </head> <body> <h2 th:text="#{app.title}">App Title</h2> <table> <thead> <tr> <th>Id</th> <th>Name</th> </tr> </thead> <tbody> <tr th:each="user : ${users}"> <td th:text="${user.id}">Id</td> <td th:text="${user.name}">Name</td> </tr> </tbody> </table> </body> </html>
Now run Spring boot application and hit this link in the browser: http://localhost:8080/ You will see below HTML page on the screen: