Spring Boot Web application using Spring MVC and Spring Data JPA



In this tutorial, we will learn how to build a SpringBoot based 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 8

- Spring boot 2.4.2

- Spring framework 5+

- Spring Data JPA 2.4+

- H2 Database

- Eclipse STS

Development Process

1. Create Spring boot Project
2. Create JPA Entity
3. Configure Database
4. Create JPA Repository
5. Create Spring MVC Controller
6. Create Thymeleaf template
7. Run Spring boot project
8. Demo

1. Create 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]

Create project structure as per the below screenshot:

2. Create JPA Entity

Let's create a User entity under the domain package and add the following content to it:
package net.sourcecodeexamples.springboot.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author https://www.sourcecodeexamples.net/ 
 * 
 * JPA entity class
 *
 */
@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

Spring boot automatically configure database details for H2 in-memory database so we no need to explicitly add the database configuration in the application.properties file.

Let's add the following properties to the application.properties file:
logging.level.org.springframework=INFO

################### Hibernate Configuration ##########################
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

SQL Script - Sample Data

Create data.sql file under /resources folder and add the following content to it:
    	
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

Let's create a UserRepository for User entity which gives CRUD database operations for User:
package net.sourcecodeexamples.springboot.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import net.sourcecodeexamples.springboot.domain.User;


/**
 * @author https://www.sourcecodeexamples.net/
 * JPA Repository for User entity
 *
 */
public interface UserRepository extends JpaRepository
{

}

5. Create Spring MVC Controller

Let's create UserController to handle HTTP GET request and return a simple view ( thymeleaf):
    	
package net.sourcecodeexamples.springboot.web.controllers;

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 Thymeleaf template

Let's create an index.html file under the /resources/templates folder and add the following content to it:
<!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 Spring boot project

This is Spring boot's main entity class.  From your IDE, run the Application. main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.
package net.sourcecodeexamples.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author https://www.sourcecodeexamples.net/
 * Spring boot main entry class
 *
 */
@SpringBootApplication
public class Application
{
	public static void main(String[] args)
	{
		SpringApplication.run(Application.class, args);
	}
}

Or use the below command to run the Spring boot app:
$ mvn spring-boot:run

8. Demo

Hit this URL in a browser: http://localhost:8080/



Comments