Home Spring boot Student Management System using Spring Boot

Student Management System using Spring Boot

32 min read
0
0
1,090

This tutorils will teach about complete student management project.the purpose of this project is to develop a rest api  using Spring boot to manage students,teacher details.

Functional Requirements

1 .The student shall be able to register for an account and manage their details.
2. The teacher shall be able to register for an account and manage their details.
3.The admin shall be able to manage the course details.
4.The admin shall be able to manage the batch details.
5.The admin shall be able to manage the enrollment details.

 

Lets Started Project

Project Set up

First Step go to Spring Initializer to configure the project using the following dependencies.

1.  Spring Data JPA
2.  Spring Web
3.  Lombok
4.  MySQL Driver

Create the Package entity

Inside the Package create the class Student

package com.lbs.studentmanagementapp.entity;
import jakarta.persistence.*;
import java.util.Set;

@Entity
@Table(name="student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="student_id",length = 11)
    private int studentid;

    @Column(name="student_name",length = 45)
    private String studentname;

    @Column(name="address",length = 60)
    private String address;

    @Column(name="phone",length = 12)
    private String phone;

    @OneToMany(mappedBy = "student")
    private Set<Enrollment> entrollments;

    public Student() {
    }

    public Student(int studentid, String studentname, String address, String phone) {
        this.studentid = studentid;
        this.studentname = studentname;
        this.address = address;
        this.phone = phone;
    }

    public Student(String studentname, String address, String phone) {
        this.studentname = studentname;
        this.address = address;
        this.phone = phone;
    }

    public int getStudentid() {
        return studentid;
    }

    public void setStudentid(int studentid) {
        this.studentid = studentid;
    }

    public String getStudentname() {
        return studentname;
    }

    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentid=" + studentid +
                ", studentname='" + studentname + '\'' +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

Create the Package DTO

Inside the Package create the class StudentDTO and StudentSaveDTO and StudentUpdateDTO

StudentDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class StudentDTO 
{
    private int studentid;
    private String studentname;
    private String address;
    private String phone;
}

StudentSaveDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class StudentSaveDTO 
{
    private String studentname;
    private String address;
    private String phone;
}

StudentUpdateDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class StudentUpdateDTO 
{
    private int studentid;
    private String studentname;
    private String address;
    private String phone;
}

Create the Package Controller

nside the Package create the class StudentController

@RestController
@CrossOrigin
@RequestMapping("api/v1/student")
public class StudentController {

        @Autowired
        private StudentService studentService;


        @PostMapping(path = "/save")
        public String saveStudent(@RequestBody StudentSaveDTO studentSaveDTO)
        {
            String stname = studentService.addStudent(studentSaveDTO);
            return stname;

        }

        @GetMapping("/getAllStudents")
        public List<StudentDTO> getAllStudents()
        {
            List<StudentDTO> allStudents = studentService.getAllStudents();
            return allStudents;
        }

    @PutMapping(path = "/update")
    public String updateStudent(@RequestBody StudentUpdateDTO studentUpdateDTO)
    {
        String stname = studentService.updateStudent(studentUpdateDTO);
        return stname;

    }

    @DeleteMapping(path = "/delete/{id}")
    public String deleteStudent(@PathVariable(value = "id")int id)
    {
      boolean deleteStudent = studentService.deleteStudent(id);
      return "deletedddddd!!!!";

    }

}

 

Create the Package Student Service

Inside the Package create the interface StudentService.java and StudentServiceIMPL.java.

StudentService

public interface StudentService {
    String addStudent(StudentSaveDTO studentSaveDTO);

    List<StudentDTO> getAllStudents();

    String updateStudent(StudentUpdateDTO studentUpdateDTO);

    boolean deleteStudent(int id);
}

StudentServiceIMPL

@Service
public class StudentServiceIMPL implements StudentService {

    @Autowired
    private StudentRepo studentRepo;

    @Override
    public String addStudent(StudentSaveDTO studentSaveDTO) {

        Student student = new Student(
                studentSaveDTO.getStudentname(),
                studentSaveDTO.getAddress(),
                studentSaveDTO.getPhone()

        );

        studentRepo.save(student);

        return student.getStudentname();
    }

    @Override
    public List<StudentDTO> getAllStudents() {

        List<Student> getStudents = studentRepo.findAll();
        List<StudentDTO> studentDTOList = new ArrayList<>();

        for(Student student:getStudents)
        {
            StudentDTO studentDTO = new StudentDTO(
                    student.getStudentid(),
                    student.getStudentname(),
                    student.getAddress(),
                    student.getPhone()

            );
            studentDTOList.add(studentDTO);
        }

        return studentDTOList;
    }

    @Override
    public String updateStudent(StudentUpdateDTO studentUpdateDTO) {

        if(studentRepo.existsById(studentUpdateDTO.getStudentid()))
        {
            Student student = studentRepo.getById(studentUpdateDTO.getStudentid());
            student.setStudentname(studentUpdateDTO.getStudentname());
            student.setAddress(studentUpdateDTO.getAddress());
            student.setPhone(studentUpdateDTO.getPhone());
            studentRepo.save(student);
            return student.getStudentname();
        }
        else
        {
            System.out.println("Student ID Not Found");
        }
        return null;
    }

    @Override
    public boolean deleteStudent(int id) {


        if(studentRepo.existsById(id))
        {
            studentRepo.deleteById(id);
        }
        else
        {
            System.out.println("Student ID Not Found");
        }
        return false;
    }
}

Create the Package Repo 

StudentRepo

public interface StudentRepo extends JpaRepository<Integer,Student> {
}

Teacher entity

Inside the Package create the class Teacher

@Entity
@Table(name="techer")
public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="teacher_id",length = 11)
    private int teacherid;

    @Column(name="teacher_name",length = 45)
    private String teachername;

    @Column(name="address",length = 60)
    private String address;


    @Column(name="phone",length = 12)
    private String phone;

    public Teacher() {
    }

    public Teacher(int teacherid, String teachername, String address, String phone) {
        this.teacherid = teacherid;
        this.teachername = teachername;
        this.address = address;
        this.phone = phone;
    }


    public Teacher(String teachername, String address, String phone) {
        this.teachername = teachername;
        this.address = address;
        this.phone = phone;
    }

    public int getTeacherid() {
        return teacherid;
    }

    public void setTeacherid(int teacherid) {
        this.teacherid = teacherid;
    }

    public String getTeachername() {
        return teachername;
    }

    public void setTeachername(String teachername) {
        this.teachername = teachername;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public String toString() {
        return "Teacher{" +
                "teacherid=" + teacherid +
                ", teachername='" + teachername + '\'' +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

Teacher DTO

Inside the Package create the class TeacherDTO and TeacherSaveDTO and TeacherUpdateDTO

TeacherDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class TeacherDTO {

    private int teacherid;
    private String teachername;
    private String address;
    private String phone;
}

TeacherSaveDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class TeacherSaveDTO 
{
    private String teachername;
    private String address;
    private String phone;
}

TeacherUpdateDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class TeacherUpdateDTO {
    private int teacherid;
    private String teachername;
    private String address;
    private String phone;
}

Create the Package Controller

nside the Package create the class TeacherController

TeacherController

@RestController
@CrossOrigin
@RequestMapping("api/v1/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @PostMapping(path = "/save")
    public String saveTeacher(@RequestBody TeacherSaveDTO teacherSaveDTO)
    {
        String teachername = teacherService.addTeacher(teacherSaveDTO);
        return teachername;
    }

    @GetMapping("/getAllTeachers")
    public List<TeacherDTO> getAllTeachers()
    {
        List<TeacherDTO> allTeachers = teacherService.getAllTeachers();
        return allTeachers;
    }


    @PutMapping(path = "/update")
    public String updateTeacher(@RequestBody TeacherUpdateDTO teacherUpdateDTO)
    {
        String teachername = teacherService.updateTeacher(teacherUpdateDTO);
        return teachername;
    }

    @DeleteMapping(path = "/delete/{id}")
    public String deleteTeacher(@PathVariable(value = "id")int id)
    {
        boolean deleteTeacher = teacherService.deleteTeacher(id);
        return "deletedddddd!!!!";
    }

}

Teacher Service

Inside the Package create the interface TeacherService.java and TeacherServiceIMPL.java.

TeacherService

public interface TeacherService {
    String addTeacher(TeacherSaveDTO teacherSaveDTO);

    List<TeacherDTO> getAllTeachers();

    String updateTeacher(TeacherUpdateDTO teacherUpdateDTO);

    boolean deleteTeacher(int id);
}

TeacherServiceIMPL

package com.lbs.studentmanagementapp.service.IMPL;

import com.lbs.studentmanagementapp.dto.StudentDTO;
import com.lbs.studentmanagementapp.dto.TeacherDTO;
import com.lbs.studentmanagementapp.dto.TeacherSaveDTO;
import com.lbs.studentmanagementapp.dto.TeacherUpdateDTO;
import com.lbs.studentmanagementapp.entity.Student;
import com.lbs.studentmanagementapp.entity.Teacher;
import com.lbs.studentmanagementapp.repo.StudentRepo;
import com.lbs.studentmanagementapp.repo.TeacherRepo;
import com.lbs.studentmanagementapp.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceIMPL implements TeacherService {

    @Autowired
    private TeacherRepo teacherRepo;


    @Override
    public String addTeacher(TeacherSaveDTO teacherSaveDTO) {

        Teacher teacher = new Teacher(
                teacherSaveDTO.getTeachername(),
                teacherSaveDTO.getAddress(),
                teacherSaveDTO.getPhone()
        );
        teacherRepo.save(teacher);
        return teacher.getTeachername();


    }

    @Override
    public List<TeacherDTO> getAllTeachers() {
        List<Teacher> getTeachers = teacherRepo.findAll();
        List<TeacherDTO> teacherDTOList = new ArrayList<>();

        for(Teacher teacher:getTeachers)
        {
            TeacherDTO teacherDTO = new TeacherDTO(
                    teacher.getTeacherid(),
                    teacher.getTeachername(),
                    teacher.getAddress(),
                    teacher.getPhone()

            );
            teacherDTOList.add(teacherDTO);
        }
        return teacherDTOList;

    }

    @Override
    public String updateTeacher(TeacherUpdateDTO teacherUpdateDTO) {
        if(teacherRepo.existsById(teacherUpdateDTO.getTeacherid()))
        {
            Teacher teacher = teacherRepo.getById(teacherUpdateDTO.getTeacherid());
            teacher.setTeachername(teacherUpdateDTO.getTeachername());
            teacher.setAddress(teacherUpdateDTO.getAddress());
            teacher.setPhone(teacherUpdateDTO.getPhone());
            teacherRepo.save(teacher);
            return teacher.getTeachername();
        }
        else
        {
            System.out.println("Teacher ID Not Found");
        }
        return null;
    }

    @Override
    public boolean deleteTeacher(int id) {



        if(teacherRepo.existsById(id))
        {
            teacherRepo.deleteById(id);
        }
        else
        {
            System.out.println("Teacher ID Not Found");
        }

        return false;
    }
}

TeacherRepo

public interface TeacherRepo extends JpaRepository<Teacher,Integer> {
}

Course

Inside the Package create the class Course

package com.lbs.studentmanagementapp.entity;

import jakarta.persistence.*;

import java.util.Set;

@Entity
@Table(name="course")
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="course_id",length = 11)
    private int courseid;

    @Column(name="course_name",length = 45)
    private String coursename;

    @Column(name="syllabus",length = 60)
    private String syllabus;


    @Column(name="duration",length = 12)
    private String duration;

    @OneToMany(mappedBy = "course")
    private Set<Batch> batches;



    public Course(int courseid, String coursename, String syllabus, String duration) {
        this.courseid = courseid;
        this.coursename = coursename;
        this.syllabus = syllabus;
        this.duration = duration;
    }

    public Course(String coursename, String syllabus, String duration) {
        this.coursename = coursename;
        this.syllabus = syllabus;
        this.duration = duration;
    }


    public Course() {
    }


    public int getCourseid() {
        return courseid;
    }

    public void setCourseid(int courseid) {
        this.courseid = courseid;
    }

    public String getCoursename() {
        return coursename;
    }

    public void setCoursename(String coursename) {
        this.coursename = coursename;
    }

    public String getSyllabus() {
        return syllabus;
    }

    public void setSyllabus(String syllabus) {
        this.syllabus = syllabus;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseid=" + courseid +
                ", coursename='" + coursename + '\'' +
                ", syllabus='" + syllabus + '\'' +
                ", duration='" + duration + '\'' +
                '}';
    }
}

CourseDTO

Inside the Package create the class CourseDTO and CourseSaveDTO and CourseUpdateDTO

CourseDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class CourseDTO 
{
    private int courseid;
    private String coursename;
    private String syllabus;
    private String duration;
}

CourseSaveDTO

@AllArgsConstructor
@NoArgsConstructor
@Data

public class CourseSaveDTO 
{
    private String coursename;
    private String syllabus;
    private String duration;

}

CourseUpdateDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class CourseUpdateDTO {

    private int courseid;
    private String coursename;
    private String syllabus;
    private String duration;

}

 

inside the Package create the class CourceController

CourceController

@RestController
@CrossOrigin
@RequestMapping("api/v1/course")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @PostMapping(path = "/save")
    public String saveCourse(@RequestBody CourseSaveDTO courseSaveDTO)
    {
        String course = courseService.addCourse(courseSaveDTO);
        return course;

    }

    @GetMapping("/getAllCourses")
    public List<CourseDTO> getAllStudents()
    {
        List<CourseDTO> allCourses = courseService.getAllCourses();
        return allCourses;
    }

    @PutMapping(path = "/update")
    public String updateCourse(@RequestBody CourseUpdateDTO courseUpdateDTO)
    {
        String course = courseService.updateCourse(courseUpdateDTO);
        return course;

    }

    @DeleteMapping(path = "/delete/{id}")
    public String deleteCourse(@PathVariable(value = "id")int id)
    {
        boolean deleteCourse = courseService.deleteCourse(id);
        return "deletedddddd!!!!";

    }

}

Course Service

Inside the Package create the interface CourseService.java and CourseServiceIMPL.java.

CourseService

public interface CourseService {
    String addCourse(CourseSaveDTO courseSaveDTO);

    List<CourseDTO> getAllCourses();

    String updateCourse(CourseUpdateDTO courseUpdateDTO);

    boolean deleteCourse(int id);
}

CourseServiceIMPL

@Service
public class CourseServiceIMPL implements CourseService {


    @Autowired
    private CourseRepo courseRepo;

    @Override
    public String addCourse(CourseSaveDTO courseSaveDTO) {

        Course course = new Course(
                courseSaveDTO.getCoursename(),
                courseSaveDTO.getSyllabus(),
                courseSaveDTO.getDuration()
        );
        courseRepo.save(course);
        return course.getCoursename();
    }

    @Override
    public List<CourseDTO> getAllCourses()
    {
        List<Course> getCourses = courseRepo.findAll();
        List<CourseDTO> courseDTOList = new ArrayList<>();
        for(Course course:getCourses)
        {
            CourseDTO courseDTO = new CourseDTO(
                  course.getCourseid(),
                  course.getCoursename(),
                  course.getSyllabus(),
                  course.getDuration()
            );
            courseDTOList.add(courseDTO);
        }
        return courseDTOList;
    }

    @Override
    public String updateCourse(CourseUpdateDTO courseUpdateDTO) {
        if(courseRepo.existsById(courseUpdateDTO.getCourseid()))
        {
            Course course = courseRepo.getById(courseUpdateDTO.getCourseid());
            course.setCoursename(courseUpdateDTO.getCoursename());
            course.setSyllabus(courseUpdateDTO.getSyllabus());
            course.setDuration(courseUpdateDTO.getDuration());

            courseRepo.save(course);
            return course.getCoursename();
        }
        else
        {
            System.out.println("Course ID Not Found");
        }
        return null;
    }

    @Override
    public boolean deleteCourse(int id) {

        if(courseRepo.existsById(id))
        {
            courseRepo.deleteById(id);
        }
        else
        {
            System.out.println("Course ID Not Found");
        }
        return false;
    }


}

CourseRepo

public interface CourseRepo extends JpaRepository<Course,Integer> {
}

Batch

Inside the Package create the class Batch

@Entity
@Table(name="batch")
public class Batch {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="batch_id",length = 11)
    private int batchid;

    @Column(name="batch_name",length = 45)
    private String batchname;

    @Column(name="start_date",length = 12)
    private String startdate;

    @ManyToOne
    @JoinColumn(name = "course_id",nullable = false)
    private Course course;

    @OneToMany(mappedBy = "batch")
    private Set<Enrollment> entrollments;



    public Batch(int batchid, String batchname, String startdate, Course course) {
        this.batchid = batchid;
        this.batchname = batchname;
        this.startdate = startdate;
        this.course = course;
    }

    public Batch() {
    }

    public Batch(String batchname, String startdate, Course course) {
        this.batchname = batchname;
        this.startdate = startdate;
        this.course = course;
    }


    public int getBatchid() {
        return batchid;
    }

    public void setBatchid(int batchid) {
        this.batchid = batchid;
    }

    public String getBatchname() {
        return batchname;
    }

    public void setBatchname(String batchname) {
        this.batchname = batchname;
    }

    public String getStartdate() {
        return startdate;
    }

    public void setStartdate(String startdate) {
        this.startdate = startdate;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    @Override
    public String toString() {
        return "Batch{" +
                "batchid=" + batchid +
                ", batchname='" + batchname + '\'' +
                ", startdate='" + startdate + '\'' +
                ", course=" + course +
                '}';
    }

BatchDTO

Inside the Package create the class BatchDTO and BatchSaveDTO and BatchUpdateDTO

BatchDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class BatchDTO {

    private int batchid;
    private String batchname;
    private Course course;
    private String startdate;
    
}

BatchSaveDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class BatchSaveDTO {

    private String batchname;

    private int courseid;

    private String startdate;
}

BatchUpdateDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class BatchUpdateDTO
{
    private int batchid;
    private String batchname;
    private int courseid;
    private String startdate;

}

inside the Package create the class batchController

batchController

@RestController
@CrossOrigin
@RequestMapping("api/v1/batch")
public class BatchController {

    @Autowired
    private BatchService batchService;

    @PostMapping(path = "/save")
    public String saveBatch(@RequestBody BatchSaveDTO batchSaveDTO)
    {
        String batch = batchService.addBatch(batchSaveDTO);
        return batch;
    }

    @GetMapping("/getAllBatches")
    public List<BatchDTO> getAllBatches()
    {
        List<BatchDTO> allBatches = batchService.getAllBatches();
        return allBatches;
    }


    @PutMapping(path = "/update")
    public String updateBatch(@RequestBody BatchUpdateDTO batchUpdateDTO)
    {
        String course = batchService.updateBatch(batchUpdateDTO);
        return course;

    }

    @DeleteMapping(path = "/delete/{id}")
    public String deleteBatch(@PathVariable(value = "id")int id)
    {
        boolean deleteCourse = batchService.deleteBatch(id);
        return "deletedddddd!!!!";

    }

}

 

Inside the Package create the interface BatchService.java and BatchServiceIMPL.java.

BatchService

public interface BatchService {
    String addBatch(BatchSaveDTO batchSaveDTO);


    List<BatchDTO> getAllBatches();

    String updateBatch(BatchUpdateDTO batchUpdateDTO);

    boolean deleteBatch(int id);
}

BatchServiceIMPL

@Service
public class BatchServiceIMPL implements BatchService {

    @Autowired
    private CourseRepo courseRepo;

    @Autowired
    private BatchRepo batchRepo;

    @Override
    public String addBatch(BatchSaveDTO batchSaveDTO) {

        Batch batch = new Batch(
                batchSaveDTO.getBatchname(),
                batchSaveDTO.getStartdate(),
                courseRepo.getById(batchSaveDTO.getCourseid())

        );
        batchRepo.save(batch);
        return batch.getBatchname();
    }

    @Override
    public List<BatchDTO> getAllBatches() {

        List<Batch> getBatches = batchRepo.findAll();
        List<BatchDTO> batchDTOList = new ArrayList<>();
        for(Batch batch:getBatches)
        {
            BatchDTO batchDTO = new BatchDTO(
                    batch.getBatchid(),
                    batch.getBatchname(),
                    batch.getCourse(),
                    batch.getStartdate()
            );
            batchDTOList.add(batchDTO);
        }
        return batchDTOList;

    }

    @Override
    public String updateBatch(BatchUpdateDTO batchUpdateDTO) {

        if(batchRepo.existsById(batchUpdateDTO.getBatchid()))
        {
            Batch batch = batchRepo.getById(batchUpdateDTO.getBatchid());
            batch.setBatchname(batchUpdateDTO.getBatchname());
            batch.setCourse(courseRepo.getById(batchUpdateDTO.getCourseid()));
            batch.setStartdate(batchUpdateDTO.getStartdate());

            batchRepo.save(batch);
            return batch.getBatchname();
        }
        else
        {
            System.out.println("Course ID Not Found");
        }
        return null;
    }

    @Override
    public boolean deleteBatch(int id) {

        if(batchRepo.existsById(id))
        {
            batchRepo.deleteById(id);
        }
        else
        {
            System.out.println("Course ID Not Found");
        }
        return false;

    }
}

BatchRepo

public interface BatchRepo extends JpaRepository<Batch,Integer> {
}

Enrollment

Inside the Package create the class Enrollment

@Entity
@Table(name="entrollment")
public class Enrollment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="enroll_id",length = 11)
    private int enrollid;

    @ManyToOne
    @JoinColumn(name = "student_id",nullable = false)
    private Student student;


    @ManyToOne
    @JoinColumn(name = "batch_id",nullable = false)
    private Batch batch;


    @Column(name="join_date",length = 12)
    private String joindate;


    @Column(name="fee",length = 12)
    private int fee;

    public Enrollment(int enrollid, Student student, Batch batch, String joindate, int fee) {
        this.enrollid = enrollid;
        this.student = student;
        this.batch = batch;
        this.joindate = joindate;
        this.fee = fee;
    }


    public Enrollment() {
    }


    public Enrollment(Student student, Batch batch, String joindate, int fee) {
        this.student = student;
        this.batch = batch;
        this.joindate = joindate;
        this.fee = fee;
    }

    public int getEnrollid() {
        return enrollid;
    }

    public void setEnrollid(int enrollid) {
        this.enrollid = enrollid;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Batch getBatch() {
        return batch;
    }

    public void setBatch(Batch batch) {
        this.batch = batch;
    }

    public String getJoindate() {
        return joindate;
    }

    public void setJoindate(String joindate) {
        this.joindate = joindate;
    }

    public int getFee() {
        return fee;
    }

    public void setFee(int fee) {
        this.fee = fee;
    }

    @Override
    public String toString() {
        return "Entrollment{" +
                "enrollid=" + enrollid +
                ", student=" + student +
                ", batch=" + batch +
                ", joindate='" + joindate + '\'' +
                ", fee=" + fee +
                '}';
    }
}

EnrollmentDTO

Inside the Package create the class EnrollmentDTO and EnrollmentSaveDTO and EnrollmentUpdateDTO

EnrollmentDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EnrollmentDTO {

    private int enrollid;
    private Student student;
    private Batch batch;
    private String joindate;
    private int fee;


}

EnrollmentSaveDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EnrollmentSaveDTO {


    private int student_id;
    private int batch_id;
    private String joindate;
    private int fee;
}

EnrollmentUpdateDTO

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EnrollmentUpdateDTO {

    private int enrollid;
    private int student_id;
    private int batch_id;
    private String joindate;
    private int fee;
}

Controller

Inside the Package create the class EnrollmentController

@RestController
@CrossOrigin
@RequestMapping("api/v1/enrollment")
public class EnrollmentController {

    @Autowired
    private EnrollmentService enrollmentService;

    @PostMapping(path = "/save")
    public String saveEnrollment(@RequestBody EnrollmentSaveDTO entrollmentSaveDTO)
    {
        String enrollment = enrollmentService.addEnrollment(entrollmentSaveDTO);
        return "enrollment addedd";
    }

    @GetMapping("/getAllEnrollments")
    public List<EnrollmentDTO> getAllEnrollments()
    {
        List<EnrollmentDTO> allEnrollments = enrollmentService.getAllEnrollments();
        return allEnrollments;
    }

    @PutMapping(path = "/update")
    public String updateEnrollment(@RequestBody EnrollmentUpdateDTO enrollmentUpdateDTO)
    {
        String enrollment = enrollmentService.updateEnrollment(enrollmentUpdateDTO);
        return enrollment;

    }

    @DeleteMapping(path = "/delete/{id}")
    public String deleteEnrollment(@PathVariable(value = "id")int id)
    {
        boolean deleteCourse = enrollmentService.deleteEnrollment(id);
        return "deletedddddd!!!!";

    }

}

 

 

 

 

 

Inside the Package create the interface EnrollmentService.java and EnrollmentServiceIMPL.java.

EnrollmentService

public interface EnrollmentService {
    String addEnrollment(EnrollmentSaveDTO entrollmentSaveDTO);

    List<EnrollmentDTO> getAllEnrollments();

    String updateEnrollment(EnrollmentUpdateDTO enrollmentUpdateDTO);

    boolean deleteEnrollment(int id);
}

EnrollmentServiceIMPL

@Service
public class EnrollmentServiceIMPL implements EnrollmentService {

    @Autowired
    private EnrollmentRepo enrollmentRepo;

    @Autowired
    private StudentRepo studentRepo;

    @Autowired
    private BatchRepo batchRepo;

    @Override
    public String addEnrollment(EnrollmentSaveDTO entrollmentSaveDTO) {

        Enrollment enrollment = new Enrollment(
                studentRepo.getById(entrollmentSaveDTO.getStudent_id()),
                batchRepo.getById(entrollmentSaveDTO.getBatch_id()),
                entrollmentSaveDTO.getJoindate(),
                entrollmentSaveDTO.getFee()

        );

        enrollmentRepo.save(enrollment);


        return enrollment.getJoindate();


    }

    @Override
    public List<EnrollmentDTO> getAllEnrollments() {


        List<Enrollment> getEnrollments = enrollmentRepo.findAll();
        List<EnrollmentDTO> enrollmentDTOList = new ArrayList<>();
        for(Enrollment enrollment:getEnrollments)
        {
            EnrollmentDTO enrollmentDTO = new EnrollmentDTO(
                    enrollment.getEnrollid(),
                    enrollment.getStudent(),
                    enrollment.getBatch(),
                    enrollment.getJoindate(),
                    enrollment.getFee()
            );
            enrollmentDTOList.add(enrollmentDTO);
        }
        return enrollmentDTOList;


    }

    @Override
    public String updateEnrollment(EnrollmentUpdateDTO enrollmentUpdateDTO) {


        if(enrollmentRepo.existsById(enrollmentUpdateDTO.getEnrollid()))
        {
            Enrollment enrollment  = enrollmentRepo.getById(enrollmentUpdateDTO.getEnrollid());
            enrollment.setStudent(studentRepo.getById(enrollmentUpdateDTO.getStudent_id()));
            enrollment.setBatch(batchRepo.getReferenceById(enrollmentUpdateDTO.getBatch_id()));
            enrollment.setJoindate(enrollmentUpdateDTO.getJoindate());
            enrollment.setFee(enrollmentUpdateDTO.getFee());

            enrollmentRepo.save(enrollment);
            return enrollment.getJoindate();
        }
        else
        {
            System.out.println("Enrollment ID Not Found");
        }
        return null;

    }

    @Override
    public boolean deleteEnrollment(int id) {

        if(enrollmentRepo.existsById(id))
        {
            enrollmentRepo.deleteById(id);
        }
        else
        {
            System.out.println("Enrollment ID Not Found");
        }
        return false;
    }
}

EnrollmentRepo

public interface EnrollmentRepo extends JpaRepository<Enrollment,Integer> {
}

i have attached the video link below. which will do this tutorials step by step.

 

 

Load More Related Articles
Load More By admin
Load More In Spring boot

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Inventory management system using React

In today’s fast-paced business environment, an efficient inventory management system…