This tutorial will teach you to make the Student Management System Project step by step. The following system will use to manage the Student Management. This simple project will be helpful to understand to create the future projects of spring boot application.
Feature of projects
Course
The System shall be able to record the Course details.
The System shall be to able retrieve the Course details.
The System shall be to able Edit the Course details.
The System shall be to able Delete the Course details.
Student
The System shall be able to record the Student details.
The System shall be to able retrieve the Student details.
The System shall be to able Edit the Student details.
The System shall be to able Delete the Student details.
Let’s get Started the Project
Course
The package structure, you must following the standard package structure of spring boot and spring framework. For the example i have created the project name Student Management.
com.example.studentmanagement -------------------->StudentmanagementApplication.java // This is the main class of the programme where the programming runs. Spring Boot MVC Structure | -------------------->Courseservice.java com.example.studentmanagement.Service | --------------------->CourseController com.example.studentmanagement.Controller | ----------------------> CourseRepository.java com.example.studentmanagement.Repository | -----------------------> Course.java com.example.studentmanagement.Domain
Inside the template the folder create the file name course.html
course.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cosmo/bootstrap.min.css" /> <script src= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></script> </head> <body> <div> <h2 >Course Creation</h2> <tr> <div align = "left" > <h3><a th:href="@{'/Course/addcourse'}">Add new</a> </div> </tr> <div class="col-sm-8" align = "center"> <div class="panel-body" align = "center" > <table class="table"> <thead class="thead-dark"> <tr> <th>Course ID</th> <th>Course Name</th> <th>Duration</th> <th>edit</th> <th>delete</th> </tr> </thead> <tbody> <tr th:each="course : ${listcourse}"> <td th:text="${course.id}">Course ID</td> <td th:text="${course.coursename}">Course Name</td> <td th:text="${course.duration}">Duration</td> <td> <a th:href="@{'/Course/edit/' + ${course.id}}">Edit</a> </td> <td> <a th:href="@{'/Course/delete/' + ${course.id}}">Delete</a> </td> </tr> </tbody> </table> </div> </div> </tr> </tbody> </table> <div> </body> </html>
After that Create the addcourse.html
addcourse.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cosmo/bootstrap.min.css" /> <script src= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></script> </head> <body> <div align="center"> <h1>Create New Course</h1> <br /> <div class="col-sm-4"> <form action="#" th:action="@{/Course/save}" th:object="${course}" method="post"> <div alight="left"> <tr> <label class="form-label" >Course Name</label> <td> <input type="hidden" th:field="*{id}" /> <input type="text" th:field="*{coursename}" class="form-control" placeholder="Course Name" /> </td> </tr> </div> <div alight="left"> <tr> <label class="form-label" >Duration</label> <td> <input type="text" th:field="*{duration}" class="form-control" placeholder="Duration" /> </td> </tr> </div> <br> <tr> <td colspan="2"><button type="submit" class="btn btn-info">Save</button> </td> </tr> </form> </div> </body> </html>
Create the package com.example.studentmanagement .Controller . inside the package you have to create the class
CourseController.java
package com.example.studentmanagement.Controller; import java.util.List; 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 org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.studentmanagement.Domain.Course; import com.example.studentmanagement.Service.CourseService; @Controller @RequestMapping("/Course") public class CourseController { @Autowired private CourseService service; @GetMapping("/addcourse") public String add(Model model) { List<Course> listcourse = service.listAll(); model.addAttribute("listcourse", listcourse); model.addAttribute("course", new Course()); return "addcourse"; } @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveCourse(@ModelAttribute("course") Course course) { service.save(course); return "redirect:/course"; } @RequestMapping("/edit/{id}") public ModelAndView showEditCoursePage(@PathVariable(name = "id") int id) { ModelAndView mav = new ModelAndView("addcourse"); Course course = service.get(id); mav.addObject("course", course); return mav; } @RequestMapping("/delete/{id}") public String deleteCoursePage(@PathVariable(name = "id") int id) { service.delete(id); return "redirect:/course"; } }
After that Create the package com.example.studentmanagement.Service . inside the package you have to create the class
CourseService.java
package com.example.studentmanagement.Service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.studentmanagement.Domain.Course; import com.example.studentmanagement.Repository.CourseRepository; @Service public class CourseService { @Autowired private CourseRepository repo; public List<Course> listAll() { return repo.findAll(); } public void save(Course course) { repo.save(course); } public Course get(long id) { return repo.findById(id).get(); } public void delete(long id) { repo.deleteById(id); } }
After that Create the package com.example.studentmanagement.Repository . inside the package you have to create the interface
CourseRepository.java
package com.example.studentmanagement.Repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.studentmanagement.Domain.Course; @Repository public interface CourseRepository extends JpaRepository<Course, Long>{ }
After that Create the package com.example.studentmanagement.course . inside the package you have to create the class
Course.java
package com.example.studentmanagement.Domain; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="course") public class Course { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; private String coursename; private int duration; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getCoursename() { return coursename; } public void setCoursename(String coursename) { this.coursename = coursename; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } }
Student
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cosmo/bootstrap.min.css" /> <script src= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></script> </head> <body> <div> <h2 >Student Registation</h2> <tr> <div align = "left" > <h3><a th:href="@{'/Student/addstudent'}">Add new</a></h3> </div> </tr> <div class="col-sm-8" align = "center"> <div class="panel-body" align = "center" > <table class="table"> <thead class="thead-dark"> <tr> <th>Student ID</th> <th>Student Name</th> <th>Course Name</th> <th>edit</th> <th>delete</th> </tr> </thead> <tbody> <tr th:each="student : ${liststudent}"> <td th:text="${student.id}">Student ID</td> <td th:text="${student.stname}">Student Name</td> <td th:text="${student.coursename}">Course</td> <td> <a th:href="@{'/Student/edit/' + ${student.id}}">Edit</a> </td> <td> <a th:href="@{'/Student/delete/' + ${student.id}}">Delete</a> </td> </tr> </tbody> </table> </div> </div> </tr> </tbody> </table> <div> </body> </html>
addstudent
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cosmo/bootstrap.min.css" /> <script src= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></script> </head> <body> <div> <h2 >Student Registation</h2> <tr> <div align = "left" > <h3><a th:href="@{'/Student/addstudent'}">Add new</a></h3> </div> </tr> <div class="col-sm-8" align = "center"> <div class="panel-body" align = "center" > <table class="table"> <thead class="thead-dark"> <tr> <th>Student ID</th> <th>Student Name</th> <th>Course Name</th> <th>edit</th> <th>delete</th> </tr> </thead> <tbody> <tr th:each="student : ${liststudent}"> <td th:text="${student.id}">Student ID</td> <td th:text="${student.stname}">Student Name</td> <td th:text="${student.coursename}">Course</td> <td> <a th:href="@{'/Student/edit/' + ${student.id}}">Edit</a> </td> <td> <a th:href="@{'/Student/delete/' + ${student.id}}">Delete</a> </td> </tr> </tbody> </table> </div> </div> </tr> </tbody> </table> <div> </body> </html>
Create the package com.example.studentmanagement .Controller . inside the package you have to create the class
StudentController.java
package com.example.studentmanagement.Controller; import java.util.List; 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 org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.studentmanagement.Domain.Course; import com.example.studentmanagement.Domain.Student; import com.example.studentmanagement.Service.CourseService; import com.example.studentmanagement.Service.StudentService; @Controller @RequestMapping("/Student") public class StudentController { @Autowired private StudentService service; @Autowired private CourseService services; @GetMapping("/addstudent") public String add(Model model) { List<Student> liststudent = service.listAll(); List<Course> listcourse = services.listAll(); model.addAttribute("listcourse", listcourse); model.addAttribute("liststudent", liststudent); model.addAttribute("student", new Student()); return "addstudent"; } @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveStudent(@ModelAttribute("student") Student std) { service.save(std); return "redirect:/student"; } @RequestMapping("/edit/{id}") public ModelAndView showEditStudentPage(@PathVariable(name = "id") int id) { ModelAndView mav = new ModelAndView("addstudent"); List<Course> listcourse = services.listAll(); Student std = service.get(id); mav.addObject("student", std); return mav; } @RequestMapping("/delete/{id}") public String deleteStudentPage(@PathVariable(name = "id") int id) { service.delete(id); return "student"; } }
After that Create the package com.example.studentmanagement.Service . inside the package you have to create the class
StudentService.java
package com.example.studentmanagement.Service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.studentmanagement.Domain.Student; import com.example.studentmanagement.Repository.StudentRepository; @Service public class StudentService { @Autowired private StudentRepository repo; public List<Student> listAll() { System.out.println(repo.findAll()); return repo.findAll(); } public void save(Student std) { repo.save(std); } public Student get(long id) { return repo.findById(id).get(); } public void delete(long id) { repo.deleteById(id); } }
After that Create the package com.example.studentmanagement.Repository . inside the package you have to create the interface
StudentRepository.java
package com.example.studentmanagement.Repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.example.studentmanagement.Domain.Student; @Repository public interface StudentRepository extends JpaRepository<Student, Long> { @Query(value="select s.id, s.stname, c.coursename from student s Inner JOIN course c ON s.course=c.id", nativeQuery=true) List<Object[]> findStudent(); }
Student.java
package com.example.studentmanagement.Domain; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String stname; private int fee; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getStname() { return stname; } public void setStname(String stname) { this.stname = stname; } public int getFee() { return fee; } public void setFee(int fee) { this.fee = fee; } }
we have to merge two tables course and student table.
package com.example.studentmanagement.Domain; public class StudentDAO { private Long id; private String stname; private String coursename; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getStname() { return stname; } public void setStname(String stname) { this.stname = stname; } public String getCoursename() { return coursename; } public void setCoursename(String coursename) { this.coursename = coursename; } }
Establish the Database Connection
spring.datasource.url=jdbc:mysql://localhost:3306/linda?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC server.error.whitelabel.enabled=false server.port=7090 spring.datasource.username=root spring.datasource.password= spring.jpa.open-in-view=false spring.thymeleaf.cache=false
i have attached the video link below. which will do this tutorials step by step.