springboot项目教务系统
时间: 2023-06-16 18:05:18 浏览: 105
好的,下面是一个简单的教务系统的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项目的实现。当然,这只是一个基础的实现,你可以根据自己的需求进行扩展和优化。
阅读全文