In this tutorial, we will learn how to build a Spring Boot MVC web application using Spring MVC and Spring Data JPA.
We will be using the H2 in-memory database for storing the data.
Tools and Technologies Used
- Java 17+
- Spring boot 3+
- Spring framework 6+
- Spring Data JPA
- H2 Database
- Eclipse STS
Development Process
1. Create a Spring Boot Project
Use the below guide to create a Spring boot project in Eclipse STS IDE:-> Create Spring Boot Project in Spring Tool Suite [STS]
<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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>2. Create JPA Entity
import jakarta.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "name")
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;
}
} 3. Configure Database
logging.level.org.springframework=INFO
################### Hibernate Configuration ##########################
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=trueSQL Script - Sample Data
delete from users;
insert into users(id, name) values(1,'Admin');
insert into users(id, name) values(2,'Ram');
insert into users(id, name) values(3,'Krishna');4. Create JPA Repository
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer>
{
}5. Create Spring MVC Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import net.sourcecodeexamples.springboot.repositories.UserRepository;
/**
* @author https://www.sourcecodeexamples.net/
* HomeController handles HTTP Get request
*
*/
@Controller
public class HomeController
{
@Autowired UserRepository userRepository;
@GetMapping("/")
public String home(Model model)
{
model.addAttribute("users", userRepository.findAll());
return "index";
}
}6. Create a Thymeleaf template
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<title>Spring Boot Web App using Spring MVC and Spring Data JPA</title>
</head>
<body>
<h1>Spring Boot Web App using Spring MVC and Spring Data JPA</h1>
<hr />
<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>7. Run the Spring boot project
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}$ mvn spring-boot:run8. Demo
Conclusion
In conclusion, this tutorial provided a comprehensive guide on building a Spring Boot MVC web application using Spring MVC and Spring Data JPA. We explored the key concepts and components of a Spring Boot web application, including controllers, views, and data access objects. By leveraging Spring MVC, we were able to handle HTTP requests and map them to appropriate controller methods. Additionally, Spring Data JPA facilitated seamless integration with the database, enabling efficient data persistence and retrieval. Armed with the knowledge gained from this tutorial, you are now equipped to embark on your journey of developing robust and scalable web applications using the Spring Boot framework. So go ahead and start building amazing web applications with Spring Boot!Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner


Comments
Post a Comment
Leave Comment