目录
一、运行环境
1、环境:
2、实现一个管理系统需要以下步骤:
二、创建项目
1.创建
2、选择依赖
3、项目结构
4、配置数据库
三、代码
1、Model层
2、Repository层
3、Service层
3.1接口
3.2、实现
4、Controller层
5、视图层
5.1、index.html
5.2、new_student.html
5.3、test.html
5.4、update_student.html
四、运行结果
IDE - IDEA
Spring Boot 3+
Spring Framework 6+
Maven
Java 17
Spring Data JPA ( Hibernate)
Thymeleaf
代码
spring.datasource.url=jdbc:mysql://localhost/testdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username= root
spring.datasource.password= 123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# for Spring Boot 2
# spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
# for Spring Boot 3
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
#?????hibernate-sql
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "student_name")
private String studentName;
@Column(name = "student_age")
private String studentAge;
@Column(name = "counsellor")//辅导员
private String counsellor;
}
StudentService
import java.util.List;
import en.edu.lzzy.s05mvcemployee.model.Student;
import org.springframework.data.domain.Page;
public interface StudentService {
//获取所有的学生
List getAllStudents();
//新增/更新一个学生
void saveStudent(Student student);
//获取指定ID的学生
Student getStudentById(long id);
//删除指定ID的学生
void deleteStudentById(long id);
//分页
Page findPaginated(int pageNo, int pageSize, String sortField, String sortDirection);
}
StudentServiceImpl
import java.util.List;
import java.util.Optional;
import en.edu.lzzy.s05mvcemployee.model.Student;
import en.edu.lzzy.s05mvcemployee.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public List getAllStudents() {
return studentRepository.findAll();
}
@Override
public void saveStudent(Student student) {
this.studentRepository.save(student);
}
@Override
public Student getStudentById(long id) {
//调用数据访问层查找指定ID的学生,返回Optional对象
Optional optional = studentRepository.findById(id);
Student student = null;
//如果存在指定id的学生
if (optional.isPresent()) {
//从Optional对象中获取学生对象
student = optional.get();
} else {
//否则抛出运行时异常
throw new RuntimeException(" 找不到学生ID :: " + id);
}
return student;
}
@Override
public void deleteStudentById(long id) {
this.studentRepository.deleteById(id);
}
@Override
public Page findPaginated(int pageNo, int pageSize, String sortField, String sortDirection) {
//设置排序参数,升序ASC/降序DESC?
Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name())
? Sort.by(sortField).ascending()
: Sort.by(sortField).descending();
//根据页号/每页记录数/排序依据返回某指定页面数据。
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
return this.studentRepository.findAll(pageable);
}
}
代码
import en.edu.lzzy.s05mvcemployee.model.Student;
import en.edu.lzzy.s05mvcemployee.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
// display list of employees
// @GetMapping("/")
// public String viewHomePage(Model model) {
// model.addAttribute("listEmployees", employeeService.getAllEmployees());
// return "index";
// }
@GetMapping("/")
public String viewHomePage(Model model) {
return findPaginated(1, "studentName", "asc", model);
}
@GetMapping("/showNewStudentForm")
public String showNewStudentForm(Model model) {
// create model attribute to bind form data
Student student = new Student();
model.addAttribute("student", student);
return "new_student";
}
@PostMapping("/saveStudent")
public String saveStudent(@ModelAttribute("student") Student student) {
// save employee to database
studentService.saveStudent(student);
return "redirect:/";
}
@GetMapping("/showFormForUpdate/{id}")
public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
// get employee from the service
Student student = studentService.getStudentById(id);
// set employee as a model attribute to pre-populate the form
model.addAttribute("student", student);
return "update_student";
}
@GetMapping("/deleteStudent/{id}")
public String deleteStudent(@PathVariable(value = "id") long id) {
// call delete employee method
this.studentService.deleteStudentById(id);
return "redirect:/";
}
//获取分页数据
@GetMapping("/page/{pageNo}")
public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
@RequestParam("sortField") String sortField,
@RequestParam("sortDir") String sortDir,
Model model) {
int pageSize = 5;
Page page = studentService.findPaginated(pageNo, pageSize, sortField, sortDir);
List listStudents = page.getContent();
model.addAttribute("currentPage", pageNo);
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("sortField", sortField);
model.addAttribute("sortDir", sortDir);
model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
model.addAttribute("listStudents", listStudents);
return "index";
}
}
Student Management System
Student Management System
Title
1 of 2
2 of 2
1 of 3
2 of 3
3 of 3
1 of 3
2 of 3
3 of 3
Student Management System