MINI PROJECT UNIT – 4
QUESTION
Project 1: Student Management System with Spring Boot (CRUD + RESTful API).
Overview :
This project builds a Student Management System using Spring Boot + JPA. It introduces CRUD
operations and prepares the base for relational mapping.
Functional Requirements
Create, read, update, and delete student records.
Expose all operations via REST API.
Endpoints
POST /students → Add new student.
GET /students → Get all students.
GET /students/{id} → Get student by ID.
PUT /students/{id} → Update student.
DELETE /students/{id} → Delete student.
CODE :
A. Model Layer: Student.java
package com.example.springapp.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private String email;
// Constructors
public Student() {}
public Student(String name, String department, String email) {
this.name = name;
this.department = department;
this.email = email;
}
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
B. Repository Layer: StudentRepository.java
package com.example.springapp.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.springapp.model.Student;
public interface StudentRepository extends JpaRepository<Student, Long> {
// No need to write SQL queries — JPA handles CRUD automatically
}
C. Service Layer: StudentService.java
package com.example.springapp.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.example.springapp.model.Student;
import com.example.springapp.repository.StudentRepository;
@Service
public class StudentService {
@Autowired
private StudentRepository repository;
public Student addStudent(Student student) {
return repository.save(student);
}
public List<Student> getAllStudents() {
return repository.findAll();
}
public Student getStudentById(Long id) {
return repository.findById(id).orElse(null);
}
public Student updateStudent(Long id, Student studentDetails) {
Student student = repository.findById(id).orElse(null);
if (student != null) {
student.setName(studentDetails.getName());
student.setDepartment(studentDetails.getDepartment());
student.setEmail(studentDetails.getEmail());
return repository.save(student);
}
return null;
}
public String deleteStudent(Long id) {
repository.deleteById(id);
return "Student deleted successfully!";
}
}
D. Controller Layer: StudentController.java
package com.example.springapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.example.springapp.model.Student;
import com.example.springapp.service.StudentService;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService service;
@PostMapping
public Student createStudent(@RequestBody Student student) {
return service.addStudent(student);
}
@GetMapping
public List<Student> getAllStudents() {
return service.getAllStudents();
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable Long id) {
return service.getStudentById(id);
}
@PutMapping("/{id}")
public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
return service.updateStudent(id, student);
}
@DeleteMapping("/{id}")
public String deleteStudent(@PathVariable Long id) {
return service.deleteStudent(id);
}
}
OUTPUT :
[
{
"id": 1,
"name": "John Doe",
"department": "Computer Science",
"email": "[email protected]"
}
]