软件工程
最近再新学springbooot
以下是新学的项目
`package org.example.student123;
import jakarta.persistence.*;
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
private String department;
// 构造方法
public Student() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
`package org.example.student123;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Student123Application {
public static void main(String[] args) {
SpringApplication.run(Student123Application.class, args);
}
}
``
`package org.example.student123;
import org.example.student123.Student;
import org.example.student123.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
// 显示所有学生
@GetMapping
public String listStudents(Model model) {
model.addAttribute("students", studentRepository.findAll());
return "students/list";
}
// 显示添加表单
@GetMapping("/add")
public String showAddForm(Model model) {
model.addAttribute("student", new Student());
return "students/add";
}
// 处理添加请求
@PostMapping("/add")
public String addStudent(@ModelAttribute Student student) {
studentRepository.save(student);
return "redirect:/students";
}
// 显示编辑表单
@GetMapping("/edit/{id}")
public String showEditForm(@PathVariable Long id, Model model) {
Student student = studentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("无效的学生ID:" + id));
model.addAttribute("student", student);
return "students/edit";
}
// 处理更新请求
@PostMapping("/update/{id}")
public String updateStudent(@PathVariable Long id, @ModelAttribute Student student) {
student.setId(id);
studentRepository.save(student);
return "redirect:/students";
}
// 删除学生
@GetMapping("/delete/{id}")
public String deleteStudent(@PathVariable Long id) {
studentRepository.deleteById(id);
return "redirect:/students";
}
}
package org.example.student123;
import org.example.student123.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
// 简单的CRUD操作已由JpaRepository提供
}`
学生列表
添加学生<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮箱</th>
<th>电话</th>
<th>专业</th>
<th>操作</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.email}"></td>
<td th:text="${student.phone}"></td>
<td th:text="${student.department}"></td>
<td>
<a th:href="@{/students/edit/{id}(id=${student.id})}"
class="btn btn-warning btn-sm">编辑</a>
<a th:href="@{/students/delete/{id}(id=${student.id})}"
class="btn btn-danger btn-sm"
onclick="return confirm('确定要删除吗?')">删除</a>
</td>
</tr>
</tbody>
</table>

浙公网安备 33010602011771号