🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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!My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business


Comments
Post a Comment
Leave Comment