当前位置:   article > 正文

用JAVA创做学生管理系统_用java编写一个学生管理系统

用java编写一个学生管理系统

目录

前言:本项目不仅可以用来做学生管理系统,还可以用来做图书管理系统,商品管理系统,食堂菜系系统等。。。。

一、项目内容

1.1 、idea创建一个新的项目

1.1.1、选择依赖

1.1.2、项目的结构

1.2、连接自己的数据库

1.2.2、新建一个数据库并添加表和各列

1.3.、实现过程

1.3.1、model层

1.3.2Repository层

1.3.3service层

1.3.4、controller控制层

1.3.5templates层

二、最终效果

1.主页面

2.新增学生页面

3.修改学生页面

4.细节展示

总结:java是一中非常强大的语言,我们在学习的过程中不仅发现java不经可以完成复杂的逻辑计算,还可将数据进行在网页上的展示,把数据更直观的体现在我们面前,在探索java的道路上我们会发现越来越多的乐趣,有时候甚至忘记了时间的流逝,所以java值得我们深入的去学习,要用自己的耐心去发现更深奥更有趣的功能。


前言:本项目不仅可以用来做学生管理系统,还可以用来做图书管理系统,商品管理系统,食堂菜系系统等。。。。

一、项目内容

1.1 、idea创建一个新的项目

 

1.1.1、选择依赖

Lombok

Spring Web

Spring Data

 JPAThymeleaf

MySQL Driver

 

1.1.2、项目的结构

 

1.2、连接自己的数据库

以下是在application.properties文件中进行的配置

然后在idea的左侧找到数据库

数据源 ----> mysql

测试连接成功后就可以啦

注意:如果连接的是本机的数据库,需要打开本机的mysql才能连接

1.2.2、新建一个数据库并添加表和各列

  1. CREATE TABLE IF NOT EXISTS 'Student'(
  2. 'id' INT UNSIGNED AUTO_INCREMENT,
  3. 'name' VARCHAR(5) NOT NULL,
  4. 'gender' VARCHAR(1) NOT NULL,
  5. 'grade' INT NOT NULL,
  6. 'gan' VARCHAR(20) NOT NULL,
  7. 'Class' VARCHAR(20) NOT NULL,
  8. PRIMARY KEY ('id')
  9. )ENGINE=Student DEFAULT CHARSET =utf8;

 

1.3.、实现过程

1.3.1、model层

创建各种属性

id学号

name姓名

gender性别

grade年级

gan 专业

class1班级

注解:

@Date注解包含了get、set方法
@Entity注解进行实体类的持久化操作
@GeneratedValue注解指定主键生成,实现自增长

  1. package com.example.java1.model;
  2. import jakarta.persistence.*;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. @Data
  7. @AllArgsConstructor
  8. @NoArgsConstructor
  9. @Entity
  10. @Table(name="students")
  11. public class Student {
  12. @Id
  13. @GeneratedValue(strategy = GenerationType.IDENTITY)
  14. private long id; //学号
  15. @Column(name = "namee")//姓名
  16. private String name;
  17. @Column(name = "gender")//性别
  18. private String gender;
  19. @Column(name = "grade")//年级
  20. private int grade;
  21. @Column(name = "gan")//专业
  22. private String gan;
  23. @Column(name = "class1")//班级
  24. private String class1;
  25. }

1.3.2Repository层

Repository层创建一个StudentManage接口并继承JpaRepository接口

  1. package com.example.java1.repository;
  2. import com.example.java1.model.Student;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.data.jpa.repository.Query;
  5. import org.springframework.data.repository.query.Param;
  6. import org.springframework.stereotype.Repository;
  7. import java.util.List;
  8. @Repository
  9. public interface StudentManage extends JpaRepository <Student, Long>{
  10. @Query("select h from Student h where h.name like %:name%")
  11. List<Student> findByName(@Param("name") String name);
  12. }

1.3.3service层

service层中StudentBc是业务类,是封装我们要实现增删改查的方法

  1. package com.example.java1.service;
  2. import com.example.java1.model.Student;
  3. import org.springframework.data.domain.Page;
  4. import java.util.List;
  5. public interface StudentBc {
  6. List <Student> getTobtainStuden();//获取学生信息
  7. void newStudents(Student name);//新增
  8. Student get(long id);//获取指定ID
  9. void delStudent(long id);//删除
  10. List<Student> findByRoomContaining(String name);
  11. Student seekStudent(Long id);//查找
  12. // List<Student> bb(String name);
  13. void deleteAByid(Long id);
  14. Page<Student> paging (int pageNo, int pageSize, String sortField, String sortDirection);//分页
  15. }

StudentBcachieve是实现类,用于实现方法的增删改查

  1. package com.example.java1.service;
  2. import com.example.java1.model.Student;
  3. import com.example.java1.repository.StudentManage;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.data.domain.PageRequest;
  7. import org.springframework.data.domain.Pageable;
  8. import org.springframework.data.domain.Sort;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. import java.util.Optional;
  12. @Service
  13. public class StudentBcachieve implements StudentBc{
  14. @Autowired
  15. private StudentManage DepositStudent;
  16. @Override
  17. public List<Student> getTobtainStuden() {return DepositStudent.findAll();}
  18. @Override
  19. public void newStudents(Student name) {
  20. this.DepositStudent.save(name);
  21. }
  22. @Override
  23. public Student get(long id) {
  24. //调用数据访问层查找指定ID的学生,返回Optional对象
  25. Optional< Student > optional = DepositStudent.findById(id);
  26. Student name = null;
  27. //如果存在指定id的学生
  28. if (optional.isPresent()) {
  29. //从Optional对象中获取学生对象
  30. name = optional.get();
  31. } else {
  32. //否则抛出运行时异常
  33. throw new RuntimeException(" 找不到学生ID :: " + id);
  34. }
  35. return name;
  36. }
  37. @Override
  38. public void delStudent(long id) {
  39. this.DepositStudent.deleteById(id);
  40. }
  41. @Override
  42. public List<Student> findByRoomContaining(String name) {
  43. return null;
  44. }
  45. @Override
  46. public Student seekStudent(Long id) {
  47. Optional<Student> optional = DepositStudent.findById(id);
  48. Student name = null;
  49. if( optional.isPresent()){
  50. name = optional.get();
  51. }else{
  52. throw new RuntimeException(" 查无此人,请重新输入, id : "+ id);
  53. }
  54. return name;
  55. }
  56. @Override
  57. public void deleteAByid(Long id) {
  58. this.DepositStudent.deleteById(id);
  59. }
  60. @Override
  61. public Page<Student> paging(int pageNo, int pageSize, String sortField, String sortDirection) {
  62. //设置排序参数,升序ASC/降序DESC?
  63. Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name())
  64. ? Sort.by(sortField).ascending()
  65. : Sort.by(sortField).descending();
  66. //根据页号/每页记录数/排序依据返回某指定页面数据。
  67. Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
  68. return this.DepositStudent.findAll(pageable);
  69. }
  70. }

1.3.4、controller控制层

用来控制各种数据在客户端的相应和跳转

  1. package com.example.java1.controller;
  2. import com.example.java1.model.Student;
  3. import com.example.java1.service.StudentBc;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. @Controller
  11. public class StudenControl {
  12. @Autowired
  13. private StudentBc DepositStudent;
  14. @GetMapping("/")
  15. public String viewHomePage(Model model) {
  16. return paging(1, "name", "asc", model);
  17. }
  18. //新增
  19. @GetMapping("/xz")
  20. public String showNewEmployeeForm(Model model) {
  21. // create model attribute to bind form data
  22. Student student = new Student();
  23. model.addAttribute("student", student);
  24. return "Increase";
  25. }
  26. @PostMapping("/xr")
  27. public String saveEmployee(@ModelAttribute("student") Student student) {
  28. // save employee to database
  29. DepositStudent.newStudents(student);
  30. return "redirect:/";
  31. }
  32. //删除
  33. @GetMapping("/sc/{id}")
  34. public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
  35. // get employee from the service
  36. Student student = DepositStudent.get(id);
  37. // set employee as a model attribute to pre-populate the form
  38. model.addAttribute("student", student);
  39. return "Modify";
  40. }
  41. @GetMapping("/ee/{id}")
  42. public String deleteEmployee(@PathVariable(value = "id") long id) {
  43. // call delete employee method
  44. this.DepositStudent.delStudent(id);
  45. return "redirect:/";
  46. }
  47. //获取分页数据
  48. @GetMapping("/page/{pageNo}")
  49. public String paging(@PathVariable (value = "pageNo") int pageNo,
  50. @RequestParam("sortField") String sortField,
  51. @RequestParam("sortDir") String sortDir,
  52. Model model) {
  53. int pageSize = 5;
  54. Page<Student> page = DepositStudent.paging(pageNo, pageSize, sortField, sortDir);
  55. List<Student> listEmployees = page.getContent();
  56. model.addAttribute("currentPage", pageNo);
  57. model.addAttribute("totalPages", page.getTotalPages());
  58. model.addAttribute("totalItems", page.getTotalElements());
  59. model.addAttribute("sortField", sortField);
  60. model.addAttribute("sortDir", sortDir);
  61. model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
  62. model.addAttribute("listEmployees", listEmployees);
  63. return "MainPage";
  64. }
  65. // @GetMapping("/student")
  66. // public String getBooks(Model model){
  67. // List<Student> students = DepositStudent.findAll();
  68. // model.addAttribute("student",students);
  69. // return "search";
  70. // }
  71. //指定查询
  72. // @PostMapping("/student/inpname")
  73. // public String searchBook(@RequestParam("inpname") String inpname, Model model) {
  74. // List<Student> students = DepositStudent.paging(inpname);
  75. // model.addAttribute("books",students);
  76. //
  77. //
  78. // return "search";
  79. // }
  80. @GetMapping("/query{inpname}")
  81. public String query(@PathVariable(value = "inpname") String inpame,Model model){
  82. List<Student> listA = DepositStudent.findByRoomContaining(inpame);
  83. model.addAttribute("listA",listA);
  84. return "index";
  85. }
  86. }

1.3.5templates层

用来完成各种页面的框架和数据的呈现(可视化)

主页面

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Employee 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. <meta charset="UTF-8">
  11. <h1>学生管理系统</h1>
  12. <a th:href = "@{/xz}" class="btn btn-primary btn-sm mb-3">添加学生</a>
  13. <h3>查找</h3>
  14. <form id="query-form" action="/query{inpname}" method="get">
  15. <input type="text" id="name-input" name="inpname" placeholder="Name">
  16. <button type="button" onclick="queryStudent()">搜索</button>
  17. </form>
  18. <!-- 将输入框输入的内容传到url上面去-->
  19. <script>
  20. function queryStudent() {
  21. var name = document.getElementById("name-input").value;
  22. var url = "/query" + encodeURIComponent(name);
  23. window.location.href = url;
  24. }
  25. </script>
  26. <table border="1" class = "table table-striped table-responsive-md">
  27. <thead>
  28. <tr>
  29. <th>
  30. <a th:href="@{'/page/' + ${currentPage} + '?sortField=id&sortDir=' + ${reverseSortDir}}">
  31. 学号</a>
  32. </th>
  33. <th>
  34. <a th:href="@{'/page/' + ${currentPage} + '?sortField=name&sortDir=' + ${reverseSortDir}}">
  35. 姓名</a>
  36. </th>
  37. <th>
  38. <a th:href="@{'/page/' + ${currentPage} + '?sortField=gender&sortDir=' + ${reverseSortDir}}">
  39. 性别</a>
  40. </th>
  41. <th>
  42. <a th:href="@{'/page/' + ${currentPage} + '?sortField=grade&sortDir=' + ${reverseSortDir}}">
  43. 年级</a>
  44. </th>
  45. <th>
  46. <a th:href="@{'/page/' + ${currentPage} + '?sortField=gan&sortDir=' + ${reverseSortDir}}">
  47. 专业</a>
  48. </th>
  49. <th>
  50. <a th:href="@{'/page/' + ${currentPage} + '?sortField=class1&sortDir=' + ${reverseSortDir}}">
  51. 班级</a>
  52. </th>
  53. <th> 操作 </th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <tr th:each="name : ${listEmployees}">
  58. <td th:text="${name.id}"></td>
  59. <td th:text="${name.name}"></td>
  60. <td th:text="${name.gender}"></td>
  61. <td th:text="${name.grade}"></td>
  62. <td th:text="${name.gan}"></td>
  63. <td th:text="${name.class1}"></td>
  64. <td> <a th:href="@{/sc/{id}(id=${name.id})}" class="btn btn-primary">修改</a>
  65. <a th:href="@{/ee/{id}(id=${name.id})}" class="btn btn-danger">删除</a>
  66. </td>
  67. </tr>
  68. </tbody>
  69. </table>
  70. <div th:if = "${totalPages > 1}">
  71. <div class = "row col-sm-10">
  72. <div class = "col-sm-3">
  73. 学生人数: [[${totalItems}]]
  74. </div>
  75. <div class = "col-sm-5">
  76. <span th:each="i: ${#numbers.sequence(1, totalPages)}">
  77. <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">[[${i}]]</a>
  78. <span th:unless="${currentPage != i}">[[${i}]]</span> &nbsp; &nbsp;
  79. </span>
  80. </div>
  81. <div class = "col-sm-1">
  82. <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">下一页</a>
  83. <span th:unless="${currentPage < totalPages}"></span>
  84. </div>
  85. <div class="col-sm-1">
  86. <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">最后一页</a>
  87. <span th:unless="${currentPage < totalPages}"></span>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </body>
  93. </html>

新增学生

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <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. <meta charset="UTF-8">
  11. <h1>添加学生</h1>
  12. <hr>
  13. <meta charset="UTF-8">
  14. <h2>请输入学生信息</h2>
  15. <form action="#" th:action="@{/xr}" th:object="${student}" method="POST">
  16. <!-- Add hidden form field to handle update -->
  17. <input type="hidden" th:field="*{id}" placeholder="学号" class="form-control mb-4 col-4">
  18. <input type="text" th:field="*{name}" placeholder="姓名" class="form-control mb-4 col-4">
  19. <input type="text" th:field="*{gender}" placeholder="性别" class="form-control mb-4 col-4">
  20. <input type="text" th:field="*{grade}" placeholder="年级" class="form-control mb-4 col-4">
  21. <input type="text" th:field="*{gan}" placeholder="专业" class="form-control mb-4 col-4">
  22. <input type="text" th:field="*{class1}" placeholder="班级" class="form-control mb-4 col-4">
  23. <button type="submit" class="btn btn-info col-2"> 添加 </button>
  24. </form>
  25. <hr>
  26. <a th:href="@{/}"> 返回首页</a>
  27. </div>
  28. </body>
  29. </html>

修改学生信息

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Employee 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. <meta charset="UTF-8">
  11. <h1>修改系统</h1>
  12. <hr>
  13. <h2>输入您的修改</h2>
  14. <form action="#" th:action="@{/xr}" th:object="${student}" method="POST">
  15. <input type="hidden" th:field="*{id}" placeholder="学号" class="form-control mb-4 col-4">
  16. <input type="text" th:field="*{name}" placeholder="姓名" class="form-control mb-4 col-4">
  17. <input type="text" th:field="*{gender}" placeholder="性别" class="form-control mb-4 col-4">
  18. <input type="text" th:field="*{grade}" placeholder="年级" class="form-control mb-4 col-4">
  19. <input type="text" th:field="*{gan}" placeholder="专业" class="form-control mb-4 col-4">
  20. <input type="text" th:field="*{class1}" placeholder="班级" class="form-control mb-4 col-4">
  21. <button type="submit" class="btn btn-info col-2"> 确认修改</button>
  22. </form>
  23. <hr>
  24. <a th:href="@{/}"> Back to Employee List</a>
  25. </div>
  26. </body>
  27. </html>

二、最终效果

1.主页面

 

2.新增学生页面

3.修改学生页面

 

4.细节展示

 

 

总结:java是一中非常强大的语言,我们在学习的过程中不仅发现java不经可以完成复杂的逻辑计算,还可将数据进行在网页上的展示,把数据更直观的体现在我们面前,在探索java的道路上我们会发现越来越多的乐趣,有时候甚至忘记了时间的流逝,所以java值得我们深入的去学习,要用自己的耐心去发现更深奥更有趣的功能。

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

闽ICP备14008679号