Springboot +JPA+thymeleaf+MySQL+BootStrap的学生管理系统

目录

前言

一、环境搭建

1.1创建项目

1.2、项目结构

​编辑

1.3配置数据库

二.Entity层

2.1 Course

2.2 Student

2.3 StudentDAO

三.Controller层

3.1 CourseController

3.2 IndexController

3.3 StudentController

四.Repository层

4.1 CourseRepository

4.2 StudentRepository

五.Service层

5.1 CourseService

5.2 StudentService

六.HTML文件:

6.1 Addcourse.html:

6.2 Addstudent.html:

6.3 Course.html:

6.4 Index.html:

6.5 Student.html :

6.6 Header.html:导航栏


前言

主要是代码展示等相关如何实现学生管理系统等.


一、环境搭建

1.1创建项目

选择依赖

1.2、项目结构

1.3配置数据库

这些配置是Spring Boot应用程序的数据库和其他相关配置。下面是对每个配置项的解释:

- `spring.datasource.url`:指定数据库的URL。在这个例子中,使用的是MySQL数据库,连接到本地主机(localhost)的3306端口上的testdb数据库。设置了一些其他参数来确保正确的时区和字符编码设置。

- `spring.datasource.username`:指定数据库的用户名。在这个例子中,用户名为root。

- `spring.datasource.password`:指定数据库的密码。在这个例子中,密码为123456。

- `server.error.whitelabel.enabled`:禁用Spring Boot默认的错误页面,以便自定义处理错误。

- `spring.jpa.hibernate.ddl-auto`:指定Hibernate在启动时对数据库进行的DDL操作。在这个例子中,设置为update,表示在启动时根据实体类的定义自动更新数据库结构。其他选项包括create(创建新的表)、create-drop(创建新表并在关闭时删除表)和validate(仅验证数据库结构与实体类定义的一致性,不执行任何更改)。

- `spring.jpa.open-in-view`:指定是否启用OpenEntityManagerInView模式。设置为false表示禁用此功能。

- `spring.thymeleaf.cache`:指定是否启用Thymeleaf模板的缓存。设置为false表示禁用缓存,方便开发阶段及时看到修改后的模板效果。

这些配置可以在Spring Boot应用程序的配置文件(如application.properties或application.yml)中进行设置,以便在应用程序启动时加载并应用这些配置。

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username= root
spring.datasource.password= 123456
server.error.whitelabel.enabled=false


spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false

二.Entity层

2.1 Course

这段代码是一个名为`Course`的实体类,表示课程信息。它使用了JPA(Java Persistence API)的注解来映射对象到数据库表。以下是代码的解释:

- `@Entity`:指示该类是一个实体类,将被持久化到数据库。

- `@Table(name="course")`:指定实体类对应的数据库表名为"course"。

- `@Id`:指定该属性为主键。

- `@GeneratedValue(strategy=GenerationType.IDENTITY)`:指定主键的生成策略为自增长。

- `@Column(name="coursename")`:指定属性对应数据库表中的列名为"coursename"。

- `private Long id;`:课程的唯一标识符,类型为`Long`。

- `private String coursename;`:课程名称,类型为`String`。

- `private int duration;`:课程时长,类型为`int`。

- `public Course()`:无参构造函数。

- `public Course(Long id, String coursename, int duration)`:带参构造函数,用于设置课程的属性值。

- `public Long getId()`和`public void setId(Long id)`:用于获取和设置课程的唯一标识符。

- `public String getCoursename()`和`public void setCoursename(String coursename)`:用于获取和设置课程的名称。

- `public int getDuration()`和`public void setDuration(int duration)`:用于获取和设置课程的时长。

- `@Override`和`public String toString()`:重写了`toString()`方法,用于以字符串形式表示课程对象的属性值。

该实体类表示一个课程对象,包含课程的唯一标识符、课程名称和时长属性。通过JPA注解,它可以映射到数据库表中的对应列,并具有持久化的能力。

package com.don.studentmanagementsystem.Entity;

import java.util.List;

import javax.persistence.*;


@Entity
@Table(name="course")
public class Course
{
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(name ="coursename")
    private String coursename;

    @Column(name = "duration")
    private int duration;

    public Course() {

    }

    public Course(Long id, String coursename, int duration) {
        this.id = id;
        this.coursename = coursename;
        this.duration = 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;
    }

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

2.2 Student

这段代码是一个名为`Student`的实体类,表示学生信息。同样地,它也使用了JPA的注解来映射对象到数据库表。以下是代码的解释:

- `@Entity`:指示该类是一个实体类,将被持久化到数据库。

- `@Table(name="student")`:指定实体类对应的数据库表名为"student"。

- `@Id`:指定该属性为主键。

- `@GeneratedValue(strategy=GenerationType.IDENTITY)`:指定主键的生成策略为自增长。

- `private Long id;`:学生的唯一标识符,类型为`Long`。

- `private String stname;`:学生的姓名,类型为`String`。

- `private int course;`:学生所选择的课程,类型为`int`。

- `private int fee;`:学生缴纳的费用,类型为`int`。

- `public Long getId()`和`public void setId(Long id)`:用于获取和设置学生的唯一标识符。

- `public String getStname()`和`public void setStname(String stname)`:用于获取和设置学生的姓名。

- `public int getCourse()`和`public void setCourse(int course)`:用于获取和设置学生所选择的课程。

- `public int getFee()`和`public void setFee(int fee)`:用于获取和设置学生缴纳的费用。

该实体类表示一个学生对象,包含学生的唯一标识符、姓名、所选课程和缴纳的费用属性。通过JPA注解,它可以映射到数据库表中的对应列,并具有持久化的能力。

package com.don.studentmanagementsystem.Entity;

import javax.persistence.*;

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String stname;
    private int course;
    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 getCourse() {
        return course;
    }

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

    public int getFee() {
        return fee;
    }

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

2.3 StudentDAO

这段代码是一个名为`StudentDAO`的实体类,表示学生的数据访问对象(Data Access Object)。它包含以下属性和方法:

- `id`:学生的唯一标识符,类型为`Long`。

- `stname`:学生的姓名,类型为`String`。

- `coursename`:课程名称,类型为`String`。

该类提供了以下方法:

- `getId()`和`setId(Long id)`:用于获取和设置学生的唯一标识符。

- `getStname()`和`setStname(String stname)`:用于获取和设置学生的姓名。

- `getCoursename()`和`setCoursename(String coursename)`:用于获取和设置课程名称。

package com.don.studentmanagementsystem.Entity;

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;
    }


}

三.Controller层

3.1 CourseController

该代码片段是一个CourseController类,用于处理与课程相关的请求和操作。下面是对代码的解释:

- `@Controller`注解表示这是一个控制器类,处理HTTP请求并返回视图。

- `@RequestMapping("/Course")`指定了处理该控制器中所有请求的基本URL路径。

- `@Autowired`注解用于自动注入`CourseService`依赖,以便在控制器中使用课程服务。

- `@GetMapping("/search")`处理GET请求,并根据查询参数查询课程列表。

- `@RequestParam("query")`指定了查询参数的名称为"query"。

- `ResponseEntity<List<Course>>`表示返回一个包含课程列表的HTTP响应实体。

- `@PostMapping`处理POST请求,并创建一个新的课程。

- `@RequestBody Course course`指定了请求体中的数据将被映射到Course对象。

- `@GetMapping("/addcourse")`处理GET请求,显示添加课程的页面。

- `Model`对象用于向视图传递数据。

- `model.addAttribute("listcourse", listcourse)`将课程列表添加到模型中,以便在视图中使用。

- `model.addAttribute("course", new Course())`将一个新的Course对象添加到模型中,以便在视图中使用。

- `return "addcourse"`指定了要返回的视图名称。

- `@RequestMapping(value = "Course/save", method = RequestMethod.POST)`处理POST请求,保存课程信息。

- `@ModelAttribute("course") Course course`指定了从表单中获取的数据将被映射到Course对象。

- `service.save(course)`保存课程信息。

- `return "redirect:/"`重定向到主页。

- `@RequestMapping("/edit/{id}")`处理带有路径变量的GET请求,用于显示编辑课程的页面。

- `@PathVariable(name = "id") int id`指定了路径变量的名称为"id",并将其映射到整数类型的id变量。

- `ModelAndView`对象用于指定要返回的视图和传递给视图的模型数据。

- `mav.addObject("course", course)`将课程对象添加到模型中,以便在视图中使用。

- `@RequestMapping("/delete/{id}")`处理带有路径变量的GET请求,用于删除课程。

- `service.delete(id)`删除指定id的课程。

- `return "redirect:/"`重定向到主页。

package com.don.studentmanagementsystem.Controller;
import java.util.List;

import com.don.studentmanagementsystem.Repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.don.studentmanagementsystem.Entity.Course;
import com.don.studentmanagementsystem.Service.CourseService;

@Controller
@RequestMapping("/Course")
public class CourseController {
    @Autowired
    private CourseService service;

    @GetMapping("/search")
    public ResponseEntity<List<Course>> searchCourses(@RequestParam("query") String query){
        return ResponseEntity.ok(service.searchServices(query));
    }

    @PostMapping
    public Course createCourse(@RequestBody Course course) {
        return service.createCourse(course);
    }

    @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 = "Course/save", method = RequestMethod.POST)
    public String saveCourse(@ModelAttribute("course") Course course) {
        service.save(course);
        return "redirect:/";
    }

    @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:/";
    }

//    @Autowired
//    private CourseRepository courseRepository;
//
//    @GetMapping("/getAllCourses")
//    public List<Course> getAllCourses() {
//        return courseRepository.findAll();}



}

3.2 IndexController

该代码片段是一个IndexController类,用于处理与主页、学生页面和课程页面相关的请求和操作。下面是对代码的解释:

- `@Controller`注解表示这是一个控制器类,处理HTTP请求并返回视图。

- `@RequestMapping("/")`指定了处理根路径的请求。

- `@Autowired`注解用于自动注入`CourseService`、`StudentService`和`StudentRepository`依赖,以便在控制器中使用课程服务、学生服务和学生存储库。

- `@RequestMapping(value = "/student", method = RequestMethod.GET)`处理GET请求,显示学生页面。

- `Model`对象用于向视图传递数据。

- `List<StudentDAO> li = new ArrayList<>()`创建一个空的学生DAO对象列表。

- `for(Object[] o : studentRepository.findStudent())`循环遍历通过`studentRepository.findStudent()`方法查询的学生数据。

- `StudentDAO student = new StudentDAO()`创建一个新的学生DAO对象。

- `student.setId(Long.parseLong(String.valueOf(o[0])))`设置学生DAO对象的ID属性。

- `student.setStname((String) o[1])`设置学生DAO对象的stname属性。

- `student.setCoursename((String) o[2])`设置学生DAO对象的coursename属性。

- `li.add(student)`将学生DAO对象添加到学生DAO列表中。

- `model.addAttribute("liststudent", li)`将学生DAO列表添加到模型中,以便在视图中使用。

- `return "Student"`指定了要返回的视图名称。

- `@RequestMapping(value = "/index", method = RequestMethod.GET)`处理GET请求,显示主页。

- `return "index"`指定了要返回的视图名称。

- `@RequestMapping(value = "/course", method = RequestMethod.GET)`处理GET请求,显示课程页面。

- `List<Course> listcourse = service.listAll()`获取所有课程列表。

- `model.addAttribute("listcourse", listcourse)`将课程列表添加到模型中,以便在视图中使用。

- `public StudentService getStudent()`和`public void setStudent(StudentService student)`是用于获取和设置学生服务的方法。

package com.don.studentmanagementsystem.Controller;

import com.don.studentmanagementsystem.Entity.Course;
import com.don.studentmanagementsystem.Entity.StudentDAO;
import com.don.studentmanagementsystem.Repository.StudentRepository;
import com.don.studentmanagementsystem.Service.CourseService;
import com.don.studentmanagementsystem.Service.StudentService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private CourseService service;
    @Autowired
    private StudentService student;
    @Autowired
    private StudentRepository studentRepository;

    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public String viewStudentPage(Model model){
        List<StudentDAO> li = new ArrayList<>();
        for(Object[] o : studentRepository.findStudent()){
            StudentDAO student = new StudentDAO();
            student.setId(Long.parseLong(String.valueOf(o[0])));
            student.setStname((String) o[1]);
            student.setCoursename((String) o[2]);
            li.add(student);
        }
        model.addAttribute("liststudent", li);
        return "Student";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/course", method = RequestMethod.GET)
    public String viewHomePage(Model model) {
        List<Course> listcourse = service.listAll();
        model.addAttribute("listcourse", listcourse);
//        model.addAttribute("course", new Course());
        return "course";
    }

    public StudentService getStudent() {
        return student;
    }

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

3.3 StudentController

该代码片段是一个StudentController类,用于处理与学生相关的请求和操作。下面是对代码的解释:

- `@Controller`注解表示这是一个控制器类,处理HTTP请求并返回视图。

- `@RequestMapping("/Student")`指定了处理以"/Student"开头的请求。

- `@Autowired`注解用于自动注入`StudentService`和`CourseService`依赖,以便在控制器中使用学生服务和课程服务。

- `@GetMapping("/addstudent")`处理GET请求,显示添加学生页面。

- `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)`处理POST请求,保存学生信息。

- `@ModelAttribute("student")`用于将请求中的表单数据绑定到`Student`对象。

- `service.save(std)`保存学生信息。

- `return "redirect:/student"`重定向到学生页面。

- `@RequestMapping("/edit/{id}")`处理带有指定ID的学生编辑页面的请求。

- `ModelAndView`对象用于指定要返回的视图和模型数据。

- `List<Course> listcourse = services.listAll()`获取所有课程列表。

- `Student std = service.get(id)`根据指定的ID获取学生对象。

- `mav.addObject("student", std)`将学生对象添加到模型中,以便在视图中使用。

- `return mav`返回包含学生信息和课程列表的视图。

- `@RequestMapping("/delete/{id}")`处理带有指定ID的学生删除请求。

- `service.delete(id)`删除指定ID的学生。

- `return "student"`返回学生页面。

该控制器处理了添加、保存、编辑和删除学生的请求,并在相应的视图中显示所需的数据。

package com.don.studentmanagementsystem.Controller;

import com.don.studentmanagementsystem.Entity.Course;
import com.don.studentmanagementsystem.Entity.Student;
import com.don.studentmanagementsystem.Service.CourseService;
import com.don.studentmanagementsystem.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@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";
    }

}

四.Repository层

4.1 CourseRepository

该代码片段是一个CourseRepository接口,用于访问和操作与课程相关的数据。下面是对代码的解释:

- `@Repository`注解表示这是一个存储库接口,用于持久化和检索数据。

- `CourseRepository`接口继承了`JpaRepository<Course, Long>`,它是Spring Data JPA提供的一个通用存储库接口,提供了基本的CRUD操作和其他常用的数据库操作方法。

- `@Query`注解用于定义自定义查询方法。

- `SELECT p FROM Course p WHERE p.coursename LIKE CONCAT('%',:query, '%')`是一个查询语句,用于根据课程名称进行模糊查询。

- `searchServices(String query)`方法执行上述查询,并返回符合条件的课程列表。

- `SELECT c FROM Course c WHERE c.coursename LIKE %:keyword%`是另一个查询语句,用于根据关键字查询课程名称。

- `searchCourses(@Param("keyword") String keyword)`方法执行上述查询,并返回符合条件的课程列表。

通过使用`CourseRepository`接口,可以轻松地执行基本的CRUD操作以及自定义的查询方法,从而与数据库进行交互并检索所需的课程数据。

package com.don.studentmanagementsystem.Repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.don.studentmanagementsystem.Entity.Course;

import java.util.List;

@Repository
public interface CourseRepository extends JpaRepository<Course, Long>{
    @Query("SELECT p FROM Course p WHERE " + "p.coursename LIKE CONCAT('%',:query, '%') ")
    List<Course> searchServices(String query);


    @Query("SELECT c FROM Course c WHERE c.coursename LIKE %:keyword%")
    List<Course> searchCourses(@Param("keyword") String keyword);

}

4.2 StudentRepository

该代码片段是一个StudentRepository接口,用于访问和操作与学生相关的数据。下面是对代码的解释:

- `@Repository`注解表示这是一个存储库接口,用于持久化和检索数据。

- `StudentRepository`接口继承了`JpaRepository<Student, Long>`,它是Spring Data JPA提供的一个通用存储库接口,提供了基本的CRUD操作和其他常用的数据库操作方法。

- `@Query`注解用于定义自定义查询方法。

- `select s.id, s.stname, c.coursename from student s Inner JOIN course c ON s.course=c.id`是一个查询语句,用于通过内连接检索学生信息和对应的课程名称。

- `findStudent()`方法执行上述查询,并返回一个包含学生信息和课程名称的对象数组列表。

通过使用`StudentRepository`接口,可以轻松地执行基本的CRUD操作以及自定义的查询方法,从而与数据库进行交互并检索所需的学生数据。

package com.don.studentmanagementsystem.Repository;

import com.don.studentmanagementsystem.Entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@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();
}

五.Service层

5.1 CourseService

该代码片段是CourseService类,用于提供与课程相关的业务逻辑操作。下面是对代码的解释:

- `@Service`注解表示这是一个服务类,用于处理业务逻辑。

- `CourseService`类包含对课程进行操作的方法,如获取所有课程、保存课程、通过ID获取课程、删除课程等。

- `@Autowired`注解用于将`CourseRepository`注入到`CourseService`中,以便在服务类中使用存储库中定义的方法来操作数据库。

- `listAll()`方法返回所有课程的列表。

- `save()`方法用于保存课程。

- `get()`方法通过ID获取指定的课程。

- `delete()`方法根据ID删除指定的课程。

- `createCourse()`方法用于创建新的课程。

- `searchServices()`方法执行自定义查询来搜索符合条件的课程。

通过使用`CourseService`类,可以在业务逻辑层面对课程进行操作,并通过调用相关方法与数据库进行交互。

package com.don.studentmanagementsystem.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.don.studentmanagementsystem.Entity.Course;
import com.don.studentmanagementsystem.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);
    }

    public Course createCourse(Course course) {
        return repo.save(course);
    }

    public List<Course> searchServices(String query) {
        List<Course> courses = repo.searchServices(query);
        return courses;
    }
}

5.2 StudentService

该代码片段是StudentService类,用于提供与学生相关的业务逻辑操作。下面是对代码的解释:

- `@Service`注解表示这是一个服务类,用于处理业务逻辑。

- `StudentService`类包含对学生进行操作的方法,如获取所有学生、保存学生、通过ID获取学生、删除学生等。

- `@Autowired`注解用于将`StudentRepository`注入到`StudentService`中,以便在服务类中使用存储库中定义的方法来操作数据库。

- `listAll()`方法返回所有学生的列表。

- `save()`方法用于保存学生。

- `get()`方法通过ID获取指定的学生。

- `delete()`方法根据ID删除指定的学生。

通过使用`StudentService`类,可以在业务逻辑层面对学生进行操作,并通过调用相关方法与数据库进行交互。

package com.don.studentmanagementsystem.Service;
import java.util.List;

import com.don.studentmanagementsystem.Entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.don.studentmanagementsystem.Entity.Student;
import com.don.studentmanagementsystem.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);
    }

}

六.HTML文件:

网页的内容和结构,包括文本、图像、链接、表格、表单等元素的布局和呈现方式。

6.1 Addcourse.html:

添加课程的页面

<!DOCTYPE html>
<html>

<head th:replace="common/header :: header"/>

<body>
<div th:replace="common/header :: navbar"/>
<div align="center">
    <h1>创建新课程</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" >课程添加</label>
                    <td>
                        <input type="hidden" th:field="*{id}" />
                        <input type="text" th:field="*{coursename}" class="form-control" placeholder="课程名字" />
                    </td>
                </tr>
            </div>


            <div alight="left">
                <tr>
                    <label class="form-label" >期间</label>

                    <td>
                        <input type="text" th:field="*{duration}" class="form-control" placeholder="期间" />
                    </td>
                </tr>
            </div>

            <br>
            <tr>
                <td colspan="2"><button type="submit" class="btn btn-info">保存</button> </td>
            </tr>

        </form>
    </div>
</div>
</body>

</html>

6.2 Addstudent.html:

添加学生的页面

<!DOCTYPE html>
<html>

<head th:replace="common/header :: header"/>

<body>
<div th:replace="common/header :: navbar"/>
  <div align = "center">
    <h1>创建新学生</h1>
    <br />
    <div class = "col-sm-4">
      <form action="#" th:action="@{/Student/save}" th:object="${student}" method="post">

        <div align="left">
          <tr>
            <label class="form-label" >学生名字</label>
            <td>
              <input type="hidden" th:field="*{id}" />
              <input type="text" th:field="*{stname}" class="form-control" placeholder = "学生名字" />
            </td>
          </tr>
    </div>
        <div align="left">
          <tr>
            <label class="form-label" >课程</label>
            <td>
              <select name = "course" id="course" class="form-control">
                  <option th:each="course : ${listcourse}"
                          th:value="${course.id}"
                          th:text="${course.coursename}">

                  </option>

            </td>
          </tr>
        </div>
        <div align="left">
          <tr>
            <label class="form-label" > Fee </label>
            <td>

              <input type="text" th:field="*{fee}" class="form-control" placeholder = "Fee" />
            </td>
          </tr>
        </div>
        <br>
        <tr>
        <td colspan="2"><button type="submit" class="btn btn-info">保存</button></td>
        </tr>
      </form>
  </div>
</body>

</html>

6.3 Course.html:

课程页面

<!DOCTYPE html>
<html>

<head th:replace="common/header :: header"/>

<body>
<div th:replace="common/header :: navbar"/>

<div>
    <h2 >课程管理</h2>
    <tr>
        <div align = "left" >
            <h3><a  th:href="@{'/Course/addcourse'}">增加新课程</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}}">修改</a>
                    </td>
                    <td>
                        <a th:href="@{'/Course/delete/' + ${course.id}}">删除</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

    </tr>

    </tbody>
    </table>
    <div>

</body>

</html>

6.4 Index.html:

主页页面

<!DOCTYPE html>


<head th:replace="common/header :: header"/>

<body>
  <div th:replace="common/header :: navbar"/>

<div class="container">
  <div class="row">
    <div class="col-sm-4">
      <h2> 学生管理系统首页</h2>
  </div>
  </div>
</div>
</body>

</html>

6.5 Student.html :

学生页面

<!DOCTYPE html>


<head th:replace="common/header :: header"/>

<body>
<div th:replace="common/header :: navbar"/>

<div>
    <h2 >学生注册</h2>
    <tr>
        <div align = "left" >
            <h3><a  th:href="@{'/Student/addstudent'}">学生添加</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}}">修改</a>
                    </td>
                    <td>
                        <a th:href="@{'/Student/delete/' + ${student.id}}">删除</a>
                    </td>
                </tr>
                </tbody>
            </table>

        </div>
    </div>

    </tr>

    </tbody>
    </table>
    <div>
    </div>
</div>
</body>

</html>

6.6 Header.html:
导航栏

<!DOCTYPE html>

<head th:fragment="header">

    <meta charset="UTF-8">
    <title>Student Management System</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cosmo/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></script>


    <script>
        $(document).ready(function () {
            $('.dropdown-toggle').dropdown();
        });
    </script>
</head>

<body>



<div th:fragment="navbar">

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">学生管理系统</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" th:href="@{'/index'}">主页<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" th:href="@{'/course'}">课程</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" th:href="@{'/student'}">学生</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    分页
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" th:href="@{'/index'}">主页</a>
                    <a class="dropdown-item" th:href="@{'/course'}">课程</a>
                    <a class="dropdown-item" th:href="@{'/student'}">学生</a>
                </div>
            </li>

        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="输入">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">查找</button>
        </form>
    </div>
</nav>
</div>
</body>


</html>


源码地址: 

StudentCourseSystem: final assignment - Gitee.com

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值