Step 1: Start a new Spring Boot project
Use start.spring.io to create a “web” project. In the “Dependencies” dialog, search for and hit the “Generate” button, download the zip, and unpack it into a folder on your computer.
Step 2: Extract the folder to the desktop folder and import that folder into Eclipse.
Step 3: If we have run the server, we get the error because the database is not configured.
Step 4: DB configuration.
Step 5: Create a table in the DB and getter, setter, and constructors.
Step 6: Worked on the (MVC) patterns, now creating the Controller and View page.
Controller:-
This is to handle the request that is coming from the server, but this does not handle the rest API.
@org.springframework.stereotype.Controller
View:-
Controller and View page:-
Controller:- Create the router for the view page call.
Model, View, and Controller page:- Get all student data.
Controller.java
package Student.StudentManagementSystem.controller;
studentPage.html
<!DOCTYPE html>
<html xmlns:th="https://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 style=text-align:center th:text="${message}"><span>message will print here</span></h1>
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr th:each="studentData : ${students}">
<th scope="row" th:text="${studentData.id}"></th>
<td th:text="${studentData.firstName}"></td>
<td th:text="${studentData.lastName}"></td>
<td th:text="${studentData.email}"></td>
</tr>
</tbody>
StudentService.java
package Student.StudentManagementSystem.service;
import java.util.List;
import Student.StudentManagementSystem.entity.Student;
public interface StudentService {
public List<Student> getAllStudent();
}
package Student.StudentManagementSystem.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import Student.StudentManagementSystem.entity.Student;
import Student.StudentManagementSystem.repository.Studentrepository;
import Student.StudentManagementSystem.service.StudentService;
@Service
public class ServiceImpl implements StudentService{
@Autowired
private Studentrepository studentrepository;
@Override
public List<Student> getAllStudent() {
// TODO Auto-generated method stub
List<Student> List = studentrepository.findAll();
return List;
}
}
Studentrepository.java
package Student.StudentManagementSystem.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import Student.StudentManagementSystem.entity.Student;
@Repository
public interface Studentrepository extends JpaRepository<Student, Integer> {
}
Step 8: Create an anchor tag.
Step 9: Create a registration form.
Step 10: Save the data(Insert) into the database and redirect to the home page.
package Student.StudentManagementSystem.controller;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import Student.StudentManagementSystem.entity.Student;
import Student.StudentManagementSystem.service.StudentService;
import org.springframework.ui.Model;
@org.springframework.stereotype.Controller
public class Controller {
@Autowired
private StudentService service;
@GetMapping("/home")
public String home() {
return "home";//view page html file -> home.html
}
@GetMapping("/staticListPage")
//Rendering a collection
public String getAllListWithoutModel(Model model) {
String message = "Top 5 Cloud Service Providers";
// creating a collection
List<String> list = new ArrayList<>();
list.add("Amazon Web Services");
list.add("Microsoft Azure");
list.add("Google Cloud");
list.add("Alibaba Cloud");
list.add("IBM Cloud");
model.addAttribute("message", message);
// adding the collection attribute
model.addAttribute("cloudProvider", list);
return "view/StaticList";//view page html file -> StaticList.html
}
@GetMapping("/index")
public String getAllStudent(Model model) {
//model.addAttribute("articles", yourListOfArticles);
String msg = "Student Management System";
// adding the attribute(key-value pair)
model.addAttribute("message", msg);
model.addAttribute("students", service.getAllStudent());
return "studentPage"; //view
}
@GetMapping("/student/new")
public String createStudentForm(Model model) {
Student student = new Student(); //to hold the student data
model.addAttribute("studentGet", student);
return "create-student-from"; //view
}
@PostMapping("/students")
public String saveStudent(@ModelAttribute("studentGet") Student student) {
service.saveStudent(student);
return "redirect:/index"; //redirect to home page
}
} StudentService.java
package Student.StudentManagementSystem.service;
import java.util.List;
import Student.StudentManagementSystem.entity.Student;
public interface StudentService {
public List<Student> getAllStudent();
public Student saveStudent(Student student);
}
serviceimpl.java
package Student.StudentManagementSystem.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import Student.StudentManagementSystem.entity.Student;
import Student.StudentManagementSystem.repository.Studentrepository;
import Student.StudentManagementSystem.service.StudentService;
@Service
public class ServiceImpl implements StudentService{
@Autowired
private Studentrepository studentrepository;
@Override
public List<Student> getAllStudent() {
// TODO Auto-generated method stub
List<Student> List = studentrepository.findAll();
return List;
}
@Override
public Student saveStudent(Student student) {
return studentrepository.save(student);
}
}
create-student-from.html
<form th:action="@{/students}" th:object="${studentGet}" method="post">
<div class="form-group">
<label>Student First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="Enter First Name" name="firstName">
</div>
<div class="form-group">
<label>Student Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Enter Last Name" name="lastName">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" id="email" placeholder="Enter Email" name="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Step 11: Edit the data(Update), create the routing, and pass the ID through the controller.
Controller.java
@GetMapping("/students/edit/{id}")
public String editStudentForm(@PathVariable int id, Model model) {
model.addAttribute("student", service.getById(id));
return "edit_student";
}
StudentService.java
public interface StudentService {
public Student getById(int id);
}
serviceimpl.java
@Override
public Student getById(int id) {
return studentrepository.findById(id).get();
}
/StudentManagementSystem/src/main/resources/templates/edit_student.html
<body>
<h2>Edit Student</h2>
</body>
Step 12: Get the data. When we have clicked on the updated link get the data inside the form.
/StudentManagementSystem/src/main/resources/templates/edit_student.html
<form th:action="@{/students/edit/{id} (id=${student.id})}" th:object="${student}" method="post">
<div class="form-group">
<label>Student First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="Enter First Name" name="firstName" th:field="*{firstName}">
</div>
<div class="form-group">
<label>Student Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Enter Last Name" name="lastName" th:field="*{lastName}">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" id="email" placeholder="Enter Email" name="email" th:field="*{Email}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Step 13: Update data. When we have clicked on the Submit button to update the data.
@GetMapping("/students/edit/{id}")
public String editStudentForm(@PathVariable int id, Model model) {
model.addAttribute("student", service.getById(id));
return "edit_student";
}
@PostMapping("/students/edit/{id}")
public String updateStudent(@PathVariable int id, @ModelAttribute("student") Student student) {
Student exitingStudent = service.getById(id);
exitingStudent.setFirstName(student.getFirstName());
exitingStudent.setLastName(student.getLastName());
exitingStudent.setEmail(student.getEmail());
service.saveStudent(exitingStudent);
return "redirect:/index";
}
Step 13: Delete data. When we have clicked on the Deleted link to delete the data.
Controller.java@GetMapping("/students/{id}")
public String deleteById(@PathVariable int id) {
service.deleteById(id);
return "redirect:/index";
}
StudentService.java
public interface StudentService {
public void deleteById(int id);
}
serviceimpl.java
@Override
public void deleteById(int id) {
studentrepository.deleteById(id);
}
Full Code:-
application.properties spring.application.name=StudentManagementSystem
spring.datasource.url=jdbc:mysql://localhost:3306/students
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class.name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show_sql=true
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
package Student.StudentManagementSystem.entity;
import jakarta.annotation.Generated;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name ="students_mng")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String Email;
//default constructor
public Student() {
super();
// TODO Auto-generated constructor stub
}
//default perameter constructor
public Student(int id, String firstName, String lastName, String email) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
Email = email;
}
//Getter
public int getId() {
return id;
}
//Setter
public void setId(int id) {
this.id = id;
}
//Getter
public String getFirstName() {
return firstName;
}
//Setter
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//Getter
public String getLastName() {
return lastName;
}
//Setter
public void setLastName(String lastName) {
this.lastName = lastName;
}
//Getter
public String getEmail() {
return Email;
}
//Setter
public void setEmail(String email) {
Email = email;
}
}
package Student.StudentManagementSystem.controller;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import Student.StudentManagementSystem.entity.Student;
import Student.StudentManagementSystem.service.StudentService;
import org.springframework.ui.Model;
@org.springframework.stereotype.Controller
public class Controller {
@Autowired
private StudentService service;
@GetMapping("/home")
public String home() {
return "home";//view page html file -> home.html
}
@GetMapping("/staticListPage")
//Rendering a collection
public String getAllListWithoutModel(Model model) {
String message = "Top 5 Cloud Service Providers";
// creating a collection
List<String> list = new ArrayList<>();
list.add("Amazon Web Services");
list.add("Microsoft Azure");
list.add("Google Cloud");
list.add("Alibaba Cloud");
list.add("IBM Cloud");
model.addAttribute("message", message);
// adding the collection attribute
model.addAttribute("cloudProvider", list);
return "view/StaticList";//view page html file -> StaticList.html
}
@GetMapping("/index")
public String getAllStudent(Model model) {
//model.addAttribute("articles", yourListOfArticles);
String msg = "Student Management System";
// adding the attribute(key-value pair)
model.addAttribute("message", msg);
model.addAttribute("students", service.getAllStudent());
return "studentPage"; //view
}
@GetMapping("/student/new")
public String createStudentForm(Model model) {
Student student = new Student(); //to hold the student data
model.addAttribute("studentGet", student);
return "create-student-from"; //view
}
@PostMapping("/students")
public String saveStudent(@ModelAttribute("studentGet") Student student) {
service.saveStudent(student);
return "redirect:/index"; //redirect to home page
}
@GetMapping("/students/edit/{id}")
public String editStudentForm(@PathVariable int id, Model model) {
model.addAttribute("student", service.getById(id));
return "edit_student";
}
@PostMapping("/students/edit/{id}")
public String updateStudent(@PathVariable int id, @ModelAttribute("student") Student student) {
Student exitingStudent = service.getById(id);
exitingStudent.setFirstName(student.getFirstName());
exitingStudent.setLastName(student.getLastName());
exitingStudent.setEmail(student.getEmail());
service.saveStudent(exitingStudent);
return "redirect:/index";
}
@GetMapping("/students/{id}")
public String deleteById(@PathVariable int id) {
service.deleteById(id);
return "redirect:/index";
}
}
package Student.StudentManagementSystem.service;
import java.util.List;
import Student.StudentManagementSystem.entity.Student;
public interface StudentService {
public List<Student> getAllStudent();
public Student saveStudent(Student student);
public Student getById(int id);
public void deleteById(int id);
}
ServiceImpl.java
package Student.StudentManagementSystem.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import Student.StudentManagementSystem.entity.Student;
import Student.StudentManagementSystem.repository.Studentrepository;
import Student.StudentManagementSystem.service.StudentService;
@Service
public class ServiceImpl implements StudentService{
@Autowired
private Studentrepository studentrepository;
@Override
public List<Student> getAllStudent() {
// TODO Auto-generated method stub
List<Student> List = studentrepository.findAll();
return List;
}
@Override
public Student saveStudent(Student student) {
return studentrepository.save(student);
}
@Override
public Student getById(int id) {
return studentrepository.findById(id).get();
}
@Override
public void deleteById(int id) {
studentrepository.deleteById(id);
}
}
Studentrepository.java
package Student.StudentManagementSystem.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import Student.StudentManagementSystem.entity.Student;
@Repository
public interface Studentrepository extends JpaRepository<Student, Integer> {
}
UML Diagram for Your Spring Boot Student Management System
+----------------------+
| Student |
| (POJO / Entity) |
+----------------------+
| - id : int |
| - firstName : String |
| - lastName : String |
| - email : String |
+----------------------+
| + getters/setters |
+----------------------+
|
|
v
+------------------------------+
| Studentrepository |
| (Repository Layer) |
+------------------------------+
| extends JpaRepository |
+------------------------------+
| + save() |
| + findAll() |
| + findById() |
| + deleteById() |
+------------------------------+
^
|
|
+------------------------------+
| StudentService |
| (Service Interface) |
+------------------------------+
| + getAllStudent() |
| + saveStudent() |
| + getById() |
| + deleteById() |
+------------------------------+
^
|
|
+------------------------------+
| ServiceImpl |
| (Service Implementation) |
+------------------------------+
| - studentrepository |
+------------------------------+
| + getAllStudent() |
| + saveStudent() |
| + getById() |
| + deleteById() |
+------------------------------+
^
|
|
+------------------------------+
| Controller |
| (Controller Layer) |
+------------------------------+
| + home() |
| + getAllStudent() |
| + createStudentForm() |
| + saveStudent() |
| + editStudentForm() |
| + updateStudent() |
| + deleteById() |
+------------------------------+
|
|
v
+------------------------------+
| Thymeleaf Views |
| home.html |
| studentPage.html |
| create-student-form.html |
| edit_student.html |
+------------------------------+
|
v
+------------------------------+
| Database |
| students_mng |
+------------------------------+
Simple Flow
User Request
↓
Controller
↓
Service Interface
↓
ServiceImpl
↓
Repository
↓
Database
Layer Explanation
| Layer | Purpose |
|---|---|
| Entity / POJO | Stores student data |
| Repository | Database operations |
| Service | Business logic |
| ServiceImpl | Implements service methods |
| Controller | Handles HTTP requests |
| Thymeleaf | Frontend UI |
| Database | Stores records |
















No comments:
Post a Comment
If you have any problem please let me know.