springboot项目教务系统

时间: 2023-06-16 07:05:18 浏览: 56
好的,下面是一个简单的教务系统的Spring Boot项目实现。 1. 创建Spring Boot项目 首先,在你的IDE中创建一个Spring Boot项目。你可以使用Spring Initializr或者手动创建。如果你使用Spring Initializr,可以选择Web和Thymeleaf模板引擎的依赖。 2. 添加依赖 在你的项目`pom.xml`文件中添加以下依赖: ```xml <!-- MySQL connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 3. 配置数据库 在`application.properties`文件中添加以下配置: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/edu_system?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update ``` 其中,`edu_system`是你的数据库名称,`root`和`123456`是你的数据库用户名和密码。 4. 创建数据模型 创建一个学生(`Student`)和课程(`Course`)的实体类。 ```java @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // Getters and Setters } @Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer credit; // Getters and Setters } ``` 5. 创建数据访问层 创建一个学生(`StudentRepository`)和课程(`CourseRepository`)的接口,继承`JpaRepository`。 ```java public interface StudentRepository extends JpaRepository<Student, Long> { } public interface CourseRepository extends JpaRepository<Course, Long> { } ``` 6. 创建服务层 创建一个学生(`StudentService`)和课程(`CourseService`)的服务类,用于处理业务逻辑。 ```java @Service public class StudentService { @Autowired private StudentRepository studentRepository; public List<Student> getAllStudents() { return studentRepository.findAll(); } public Student getStudentById(Long id) { return studentRepository.findById(id).orElse(null); } public void addStudent(Student student) { studentRepository.save(student); } public void updateStudent(Student student) { studentRepository.save(student); } public void deleteStudent(Long id) { studentRepository.deleteById(id); } } @Service public class CourseService { @Autowired private CourseRepository courseRepository; public List<Course> getAllCourses() { return courseRepository.findAll(); } public Course getCourseById(Long id) { return courseRepository.findById(id).orElse(null); } public void addCourse(Course course) { courseRepository.save(course); } public void updateCourse(Course course) { courseRepository.save(course); } public void deleteCourse(Long id) { courseRepository.deleteById(id); } } ``` 7. 创建控制器 创建一个学生(`StudentController`)和课程(`CourseController`)的控制器,用于处理客户端请求。 ```java @Controller public class StudentController { @Autowired private StudentService studentService; @GetMapping("/students") public String getAllStudents(Model model) { List<Student> students = studentService.getAllStudents(); model.addAttribute("students", students); return "students"; } @GetMapping("/students/{id}") public String getStudentById(@PathVariable Long id, Model model) { Student student = studentService.getStudentById(id); model.addAttribute("student", student); return "student"; } @GetMapping("/students/new") public String addStudent(Model model) { model.addAttribute("student", new Student()); return "add-student"; } @PostMapping("/students") public String saveStudent(@ModelAttribute("student") Student student) { studentService.addStudent(student); return "redirect:/students"; } @GetMapping("/students/{id}/edit") public String editStudent(@PathVariable Long id, Model model) { Student student = studentService.getStudentById(id); model.addAttribute("student", student); return "edit-student"; } @PostMapping("/students/{id}") public String updateStudent(@PathVariable Long id, @ModelAttribute("student") Student student) { student.setId(id); studentService.updateStudent(student); return "redirect:/students"; } @GetMapping("/students/{id}/delete") public String deleteStudent(@PathVariable Long id) { studentService.deleteStudent(id); return "redirect:/students"; } } @Controller public class CourseController { @Autowired private CourseService courseService; @GetMapping("/courses") public String getAllCourses(Model model) { List<Course> courses = courseService.getAllCourses(); model.addAttribute("courses", courses); return "courses"; } @GetMapping("/courses/{id}") public String getCourseById(@PathVariable Long id, Model model) { Course course = courseService.getCourseById(id); model.addAttribute("course", course); return "course"; } @GetMapping("/courses/new") public String addCourse(Model model) { model.addAttribute("course", new Course()); return "add-course"; } @PostMapping("/courses") public String saveCourse(@ModelAttribute("course") Course course) { courseService.addCourse(course); return "redirect:/courses"; } @GetMapping("/courses/{id}/edit") public String editCourse(@PathVariable Long id, Model model) { Course course = courseService.getCourseById(id); model.addAttribute("course", course); return "edit-course"; } @PostMapping("/courses/{id}") public String updateCourse(@PathVariable Long id, @ModelAttribute("course") Course course) { course.setId(id); courseService.updateCourse(course); return "redirect:/courses"; } @GetMapping("/courses/{id}/delete") public String deleteCourse(@PathVariable Long id) { courseService.deleteCourse(id); return "redirect:/courses"; } } ``` 8. 创建Thymeleaf视图 创建一个学生(`students.html`、`student.html`、`add-student.html`、`edit-student.html`)和课程(`courses.html`、`course.html`、`add-course.html`、`edit-course.html`)的Thymeleaf视图。 students.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Students</title> </head> <body> <h1>Students</h1> <p><a href="/students/new">Add Student</a></p> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Actions</th> </tr> </thead> <tbody> <tr th:each="student : ${students}"> <td th:text="${student.id}"></td> <td th:text="${student.name}"></td> <td th:text="${student.age}"></td> <td> <a th:href="@{/students/{id}(id=${student.id})}">View</a> <a th:href="@{/students/{id}/edit(id=${student.id})}">Edit</a> <a th:href="@{/students/{id}/delete(id=${student.id})}">Delete</a> </td> </tr> </tbody> </table> </body> </html> ``` student.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Student</title> </head> <body> <h1 th:text="${student.name}"></h1> <p>ID: <span th:text="${student.id}"></span></p> <p>Name: <span th:text="${student.name}"></span></p> <p>Age: <span th:text="${student.age}"></span></p> </body> </html> ``` add-student.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Student</title> </head> <body> <h1>Add Student</h1> <form th:action="@{/students}" th:object="${student}" method="post"> <p> <label>Name</label> <input type="text" th:field="*{name}"> </p> <p> <label>Age</label> <input type="text" th:field="*{age}"> </p> <p> <button type="submit">Add</button> </p> </form> </body> </html> ``` edit-student.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Edit Student</title> </head> <body> <h1>Edit Student</h1> <form th:action="@{/students/{id}(id=${student.id})}" th:object="${student}" method="post"> <p> <label>Name</label> <input type="text" th:field="*{name}"> </p> <p> <label>Age</label> <input type="text" th:field="*{age}"> </p> <p> <button type="submit">Save</button> </p> </form> </body> </html> ``` courses.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Courses</title> </head> <body> <h1>Courses</h1> <p><a href="/courses/new">Add Course</a></p> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Credit</th> <th>Actions</th> </tr> </thead> <tbody> <tr th:each="course : ${courses}"> <td th:text="${course.id}"></td> <td th:text="${course.name}"></td> <td th:text="${course.credit}"></td> <td> <a th:href="@{/courses/{id}(id=${course.id})}">View</a> <a th:href="@{/courses/{id}/edit(id=${course.id})}">Edit</a> <a th:href="@{/courses/{id}/delete(id=${course.id})}">Delete</a> </td> </tr> </tbody> </table> </body> </html> ``` course.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Course</title> </head> <body> <h1 th:text="${course.name}"></h1> <p>ID: <span th:text="${course.id}"></span></p> <p>Name: <span th:text="${course.name}"></span></p> <p>Credit: <span th:text="${course.credit}"></span></p> </body> </html> ``` add-course.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Course</title> </head> <body> <h1>Add Course</h1> <form th:action="@{/courses}" th:object="${course}" method="post"> <p> <label>Name</label> <input type="text" th:field="*{name}"> </p> <p> <label>Credit</label> <input type="text" th:field="*{credit}"> </p> <p> <button type="submit">Add</button> </p> </form> </body> </html> ``` edit-course.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Edit Course</title> </head> <body> <h1>Edit Course</h1> <form th:action="@{/courses/{id}(id=${course.id})}" th:object="${course}" method="post"> <p> <label>Name</label> <input type="text" th:field="*{name}"> </p> <p> <label>Credit</label> <input type="text" th:field="*{credit}"> </p> <p> <button type="submit">Save</button> </p> </form> </body> </html> ``` 9. 启动应用程序 现在,你可以启动应用程序并访问`http://localhost:8080/students`和`http://localhost:8080/courses`来测试你的应用程序了。 以上就是一个简单的教务系统的Spring Boot项目的实现。当然,这只是一个基础的实现,你可以根据自己的需求进行扩展和优化。

相关推荐

最新推荐

recommend-type

Java 实现简易教务管理系统的代码

主要介绍了Java 实现简易教务管理系统的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

C语言编写教务管理系统

C语言编写教务管理系统
recommend-type

太原理工大学软件工程导论教务管理系统实验报告

每个学期的期中,学校教务处会分类向各个学院发出下各学期的教学计划,包括课程名称、课程代码、课时、班级类别(本科、专科、成人教育、研究生)、班号等;学院教学主管人员根据教学任务要求给出各门课程的相关限制...
recommend-type

教务管理系统论文教务管理系统论文教务管理系统论文

教务管理系统论文教务管理系统论文教务管理系统论文
recommend-type

教务管理系统分析设计方案

系统管理员通过数据库后台...采用教务管理信息系统可取代原系统的单据手工传递工作,减少人工开支,节省资金,并且可大大提高信息量的取得,缩短信息处理周期,规划教学资源,提高学生信息,及时反馈教学信息的利用率。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。