当前位置:   article > 正文

java设计并完成一个数据驱动的管理系统_用java和数据库做一个管理系统

用java和数据库做一个管理系统

目录

一、运行环境

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

四、运行结果


一、运行环境

1、环境:

IDE - IDEA

Spring Boot 3+

Spring Framework 6+

Maven

Java 17

Spring Data JPA ( Hibernate)

Thymeleaf

2、实现一个管理系统需要以下步骤:

  • 确定需求:首先需要了解管理系统的需求,包括功能模块和功能细节。
  • 设计数据库:根据管理系统的需求设计相应的数据库,包括表结构和表之间的关系。
  • 设计用户界面:为管理系统设计相应的用户界面,使用户操作方便。
  • 编写代码:根据设计的数据库和用户界面,使用 Java 编写管理系统的代码。
  • 测试与调试:对管理系统进行测试,检查代码是否有误,并对代码进行调试。
  • 发布与维护:将管理系统发布到相应的服务器上,并对管理系统进行维护。

二、创建项目

1.创建

2、选择依赖

3、项目结构

4、配置数据库

代码

  1. spring.datasource.url=jdbc:mysql://localhost/testdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
  2. spring.datasource.username= root
  3. spring.datasource.password= 123456
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. # for Spring Boot 2
  6. # spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
  7. # for Spring Boot 3
  8. spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
  9. # Hibernate ddl auto (create, create-drop, validate, update)
  10. spring.jpa.hibernate.ddl-auto= update
  11. #?????hibernate-sql
  12. logging.level.org.hibernate.SQL=DEBUG
  13. logging.level.org.hibernate.type=TRACE

 三、代码

1、Model层

  1. import jakarta.persistence.*;
  2. import lombok.Data;
  3. @Data
  4. @Entity
  5. @Table(name = "students")
  6. public class Student {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.IDENTITY)
  9. private long id;
  10. @Column(name = "student_name")
  11. private String studentName;
  12. @Column(name = "student_age")
  13. private String studentAge;
  14. @Column(name = "counsellor")//辅导员
  15. private String counsellor;
  16. }

2、Repository层

3、Service层

3.1接口

StudentService

  1. import java.util.List;
  2. import en.edu.lzzy.s05mvcemployee.model.Student;
  3. import org.springframework.data.domain.Page;
  4. public interface StudentService {
  5. //获取所有的学生
  6. List <Student> getAllStudents();
  7. //新增/更新一个学生
  8. void saveStudent(Student student);
  9. //获取指定ID的学生
  10. Student getStudentById(long id);
  11. //删除指定ID的学生
  12. void deleteStudentById(long id);
  13. //分页
  14. Page<Student> findPaginated(int pageNo, int pageSize, String sortField, String sortDirection);
  15. }
3.2、实现

StudentServiceImpl

  1. import java.util.List;
  2. import java.util.Optional;
  3. import en.edu.lzzy.s05mvcemployee.model.Student;
  4. import en.edu.lzzy.s05mvcemployee.repository.StudentRepository;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.data.domain.Sort;
  10. import org.springframework.stereotype.Service;
  11. @Service
  12. public class StudentServiceImpl implements StudentService {
  13. @Autowired
  14. private StudentRepository studentRepository;
  15. @Override
  16. public List <Student> getAllStudents() {
  17. return studentRepository.findAll();
  18. }
  19. @Override
  20. public void saveStudent(Student student) {
  21. this.studentRepository.save(student);
  22. }
  23. @Override
  24. public Student getStudentById(long id) {
  25. //调用数据访问层查找指定ID的学生,返回Optional对象
  26. Optional <Student> optional = studentRepository.findById(id);
  27. Student student = null;
  28. //如果存在指定id的学生
  29. if (optional.isPresent()) {
  30. //从Optional对象中获取学生对象
  31. student = optional.get();
  32. } else {
  33. //否则抛出运行时异常
  34. throw new RuntimeException(" 找不到学生ID :: " + id);
  35. }
  36. return student;
  37. }
  38. @Override
  39. public void deleteStudentById(long id) {
  40. this.studentRepository.deleteById(id);
  41. }
  42. @Override
  43. public Page<Student> findPaginated(int pageNo, int pageSize, String sortField, String sortDirection) {
  44. //设置排序参数,升序ASC/降序DESC?
  45. Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name())
  46. ? Sort.by(sortField).ascending()
  47. : Sort.by(sortField).descending();
  48. //根据页号/每页记录数/排序依据返回某指定页面数据。
  49. Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
  50. return this.studentRepository.findAll(pageable);
  51. }
  52. }

4、Controller层

代码

  1. import en.edu.lzzy.s05mvcemployee.model.Student;
  2. import en.edu.lzzy.s05mvcemployee.service.StudentService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.domain.Page;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.util.List;
  9. @Controller
  10. public class StudentController {
  11. @Autowired
  12. private StudentService studentService;
  13. // display list of employees
  14. // @GetMapping("/")
  15. // public String viewHomePage(Model model) {
  16. // model.addAttribute("listEmployees", employeeService.getAllEmployees());
  17. // return "index";
  18. // }
  19. @GetMapping("/")
  20. public String viewHomePage(Model model) {
  21. return findPaginated(1, "studentName", "asc", model);
  22. }
  23. @GetMapping("/showNewStudentForm")
  24. public String showNewStudentForm(Model model) {
  25. // create model attribute to bind form data
  26. Student student = new Student();
  27. model.addAttribute("student", student);
  28. return "new_student";
  29. }
  30. @PostMapping("/saveStudent")
  31. public String saveStudent(@ModelAttribute("student") Student student) {
  32. // save employee to database
  33. studentService.saveStudent(student);
  34. return "redirect:/";
  35. }
  36. @GetMapping("/showFormForUpdate/{id}")
  37. public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
  38. // get employee from the service
  39. Student student = studentService.getStudentById(id);
  40. // set employee as a model attribute to pre-populate the form
  41. model.addAttribute("student", student);
  42. return "update_student";
  43. }
  44. @GetMapping("/deleteStudent/{id}")
  45. public String deleteStudent(@PathVariable(value = "id") long id) {
  46. // call delete employee method
  47. this.studentService.deleteStudentById(id);
  48. return "redirect:/";
  49. }
  50. //获取分页数据
  51. @GetMapping("/page/{pageNo}")
  52. public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
  53. @RequestParam("sortField") String sortField,
  54. @RequestParam("sortDir") String sortDir,
  55. Model model) {
  56. int pageSize = 5;
  57. Page<Student> page = studentService.findPaginated(pageNo, pageSize, sortField, sortDir);
  58. List<Student> listStudents = page.getContent();
  59. model.addAttribute("currentPage", pageNo);
  60. model.addAttribute("totalPages", page.getTotalPages());
  61. model.addAttribute("totalItems", page.getTotalElements());
  62. model.addAttribute("sortField", sortField);
  63. model.addAttribute("sortDir", sortDir);
  64. model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
  65. model.addAttribute("listStudents", listStudents);
  66. return "index";
  67. }
  68. }

5、视图层

5.1、index.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Student Management System</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
  7. </head>
  8. <body>
  9. <div class="container my-2">
  10. <h1>Students List</h1>
  11. <a th:href = "@{/showNewStudentForm}" class="btn btn-primary btn-sm mb-3"> Add Student </a>
  12. <table border="1" class = "table table-striped table-responsive-md">
  13. <thead>
  14. <tr>
  15. <th>
  16. <a th:href="@{'/page/' + ${currentPage} + '?sortField=studentName&sortDir=' + ${reverseSortDir}}">
  17. Student Name</a>
  18. </th>
  19. <th>
  20. <a th:href="@{'/page/' + ${currentPage} + '?sortField=studentAge&sortDir=' + ${reverseSortDir}}">
  21. Student Age</a>
  22. </th>
  23. <th>
  24. <a th:href="@{'/page/' + ${currentPage} + '?sortField=counsellor&sortDir=' + ${reverseSortDir}}">
  25. Student Counsellor</a>
  26. </th>
  27. <th> Actions </th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <tr th:each="student : ${listStudents}">
  32. <td th:text="${student.studentName}"></td>
  33. <td th:text="${student.studentAge}"></td>
  34. <td th:text="${student.counsellor}"></td>
  35. <td> <a th:href="@{/showFormForUpdate/{id}(id=${student.id})}" class="btn btn-primary">Update</a>
  36. <a th:href="@{/deleteStudent/{id}(id=${student.id})}" class="btn btn-danger">Delete</a>
  37. </td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div th:if = "${totalPages > 1}">
  42. <div class = "row col-sm-10">
  43. <div class = "col-sm-3">
  44. Total Rows: [[${totalItems}]]
  45. </div>
  46. <div class = "col-sm-5">
  47. <span th:each="i: ${#numbers.sequence(1, totalPages)}">
  48. <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">[[${i}]]</a>
  49. <span th:unless="${currentPage != i}">[[${i}]]</span> &nbsp; &nbsp;
  50. </span>
  51. </div>
  52. <div class = "col-sm-1">
  53. <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">Next</a>
  54. <span th:unless="${currentPage < totalPages}">Next</span>
  55. </div>
  56. <div class="col-sm-1">
  57. <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">Last</a>
  58. <span th:unless="${currentPage < totalPages}">Last</span>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </body>
  64. </html>
5.2、new_student.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Student Management System</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1>Student Management System</h1>
  11. <hr>
  12. <h2>Save Student</h2>
  13. <form action="#" th:action="@{/saveStudent}" th:object="${student}" method="POST">
  14. <input type="text" th:field="*{studentName}" placeholder="Student Name" class="form-control mb-4 col-4">
  15. <input type="text" th:field="*{studentAge}" placeholder="Student Age" class="form-control mb-4 col-4">
  16. <input type="text" th:field="*{counsellor}" placeholder="Student Counsellor" class="form-control mb-4 col-4">
  17. <button type="submit" class="btn btn-info col-2"> Save Student</button>
  18. </form>
  19. <hr>
  20. <a th:href="@{/}"> Back to Student List</a>
  21. </div>
  22. </body>
  23. </html>
5.3、test.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <div class="row">
  11. <div class="col">
  12. 1 of 2
  13. </div>
  14. <div class="col">
  15. 2 of 2
  16. </div>
  17. </div>
  18. <div class="row">
  19. <div class="col">
  20. 1 of 3
  21. </div>
  22. <div class="col">
  23. 2 of 3
  24. </div>
  25. <div class="col">
  26. 3 of 3
  27. </div>
  28. <div class="col">
  29. 1 of 3
  30. </div>
  31. <div class="col">
  32. 2 of 3
  33. </div>
  34. <div class="col">
  35. 3 of 3
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>
5.4、update_student.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Student Management System</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1>Student Management System</h1>
  11. <hr>
  12. <h2>Update Student</h2>
  13. <form action="#" th:action="@{/saveStudent}" th:object="${student}" method="POST">
  14. <!-- Add hidden form field to handle update -->
  15. <input type="hidden" th:field="*{id}" />
  16. <input type="text" th:field="*{studentName}" class="form-control mb-4 col-4">
  17. <input type="text" th:field="*{studentAge}" class="form-control mb-4 col-4">
  18. <input type="text" th:field="*{counsellor}" class="form-control mb-4 col-4">
  19. <button type="submit" class="btn btn-info col-2"> Update Student</button>
  20. </form>
  21. <hr>
  22. <a th:href="@{/}"> Back to Student List</a>
  23. </div>
  24. </body>
  25. </html>

四、运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/790173
推荐阅读
相关标签
  

闽ICP备14008679号