赞
踩
目录
前言:本项目不仅可以用来做学生管理系统,还可以用来做图书管理系统,商品管理系统,食堂菜系系统等。。。。
Lombok
Spring Web
Spring Data
JPAThymeleaf
MySQL Driver
以下是在application.properties文件中进行的配置
然后在idea的左侧找到数据库
数据源 ----> mysql
测试连接成功后就可以啦
注意:如果连接的是本机的数据库,需要打开本机的mysql才能连接
- CREATE TABLE IF NOT EXISTS 'Student'(
- 'id' INT UNSIGNED AUTO_INCREMENT,
- 'name' VARCHAR(5) NOT NULL,
- 'gender' VARCHAR(1) NOT NULL,
- 'grade' INT NOT NULL,
- 'gan' VARCHAR(20) NOT NULL,
- 'Class' VARCHAR(20) NOT NULL,
- PRIMARY KEY ('id')
- )ENGINE=Student DEFAULT CHARSET =utf8;
创建各种属性
id学号
name姓名
gender性别
grade年级
gan 专业
class1班级
注解:
@Date注解包含了get、set方法
@Entity注解进行实体类的持久化操作
@GeneratedValue注解指定主键生成,实现自增长
- package com.example.java1.model;
-
- import jakarta.persistence.*;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Entity
- @Table(name="students")
- public class Student {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private long id; //学号
-
- @Column(name = "namee")//姓名
- private String name;
-
- @Column(name = "gender")//性别
- private String gender;
-
- @Column(name = "grade")//年级
- private int grade;
-
- @Column(name = "gan")//专业
- private String gan;
-
- @Column(name = "class1")//班级
- private String class1;
- }
Repository层创建一个StudentManage接口并继承JpaRepository接口
- package com.example.java1.repository;
-
- import com.example.java1.model.Student;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.query.Param;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
-
- @Repository
- public interface StudentManage extends JpaRepository <Student, Long>{
- @Query("select h from Student h where h.name like %:name%")
- List<Student> findByName(@Param("name") String name);
- }
service层中StudentBc是业务类,是封装我们要实现增删改查的方法
- package com.example.java1.service;
-
- import com.example.java1.model.Student;
- import org.springframework.data.domain.Page;
-
- import java.util.List;
-
- public interface StudentBc {
- List <Student> getTobtainStuden();//获取学生信息
-
- void newStudents(Student name);//新增
-
- Student get(long id);//获取指定ID
-
- void delStudent(long id);//删除
-
- List<Student> findByRoomContaining(String name);
- Student seekStudent(Long id);//查找
- // List<Student> bb(String name);
- void deleteAByid(Long id);
- Page<Student> paging (int pageNo, int pageSize, String sortField, String sortDirection);//分页
-
- }
StudentBcachieve是实现类,用于实现方法的增删改查
- package com.example.java1.service;
-
- import com.example.java1.model.Student;
- import com.example.java1.repository.StudentManage;
- 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;
-
- import java.util.List;
- import java.util.Optional;
-
- @Service
- public class StudentBcachieve implements StudentBc{
- @Autowired
- private StudentManage DepositStudent;
- @Override
- public List<Student> getTobtainStuden() {return DepositStudent.findAll();}
- @Override
- public void newStudents(Student name) {
- this.DepositStudent.save(name);
- }
- @Override
- public Student get(long id) {
- //调用数据访问层查找指定ID的学生,返回Optional对象
- Optional< Student > optional = DepositStudent.findById(id);
-
- Student name = null;
- //如果存在指定id的学生
- if (optional.isPresent()) {
- //从Optional对象中获取学生对象
- name = optional.get();
- } else {
- //否则抛出运行时异常
- throw new RuntimeException(" 找不到学生ID :: " + id);
- }
- return name;
- }
- @Override
- public void delStudent(long id) {
- this.DepositStudent.deleteById(id);
- }
-
- @Override
- public List<Student> findByRoomContaining(String name) {
- return null;
- }
-
-
- @Override
- public Student seekStudent(Long id) {
- Optional<Student> optional = DepositStudent.findById(id);
- Student name = null;
- if( optional.isPresent()){
- name = optional.get();
- }else{
- throw new RuntimeException(" 查无此人,请重新输入, id : "+ id);
- }
- return name;
- }
- @Override
- public void deleteAByid(Long id) {
- this.DepositStudent.deleteById(id);
-
- }
- @Override
- public Page<Student> paging(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.DepositStudent.findAll(pageable);
- }
- }
-
用来控制各种数据在客户端的相应和跳转
- package com.example.java1.controller;
-
- import com.example.java1.model.Student;
- import com.example.java1.service.StudentBc;
- 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 StudenControl {
- @Autowired
- private StudentBc DepositStudent;
- @GetMapping("/")
- public String viewHomePage(Model model) {
- return paging(1, "name", "asc", model);
- }
- //新增
- @GetMapping("/xz")
- public String showNewEmployeeForm(Model model) {
- // create model attribute to bind form data
- Student student = new Student();
- model.addAttribute("student", student);
- return "Increase";
- }
-
- @PostMapping("/xr")
- public String saveEmployee(@ModelAttribute("student") Student student) {
- // save employee to database
- DepositStudent.newStudents(student);
- return "redirect:/";
- }
- //删除
- @GetMapping("/sc/{id}")
- public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
-
- // get employee from the service
- Student student = DepositStudent.get(id);
-
- // set employee as a model attribute to pre-populate the form
- model.addAttribute("student", student);
- return "Modify";
- }
-
- @GetMapping("/ee/{id}")
- public String deleteEmployee(@PathVariable(value = "id") long id) {
-
- // call delete employee method
- this.DepositStudent.delStudent(id);
- return "redirect:/";
- }
-
- //获取分页数据
- @GetMapping("/page/{pageNo}")
- public String paging(@PathVariable (value = "pageNo") int pageNo,
- @RequestParam("sortField") String sortField,
- @RequestParam("sortDir") String sortDir,
- Model model) {
- int pageSize = 5;
-
- Page<Student> page = DepositStudent.paging(pageNo, pageSize, sortField, sortDir);
- List<Student> listEmployees = 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("listEmployees", listEmployees);
- return "MainPage";
- }
- // @GetMapping("/student")
- // public String getBooks(Model model){
- // List<Student> students = DepositStudent.findAll();
- // model.addAttribute("student",students);
- // return "search";
- // }
-
- //指定查询
- // @PostMapping("/student/inpname")
- // public String searchBook(@RequestParam("inpname") String inpname, Model model) {
- // List<Student> students = DepositStudent.paging(inpname);
- // model.addAttribute("books",students);
- //
- //
- // return "search";
- // }
- @GetMapping("/query{inpname}")
- public String query(@PathVariable(value = "inpname") String inpame,Model model){
- List<Student> listA = DepositStudent.findByRoomContaining(inpame);
- model.addAttribute("listA",listA);
- return "index";
- }
-
- }
用来完成各种页面的框架和数据的呈现(可视化)
主页面
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Employee Management System</title>
-
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
-
- </head>
- <body>
-
- <div class="container my-2">
- <meta charset="UTF-8">
- <h1>学生管理系统</h1>
-
- <a th:href = "@{/xz}" class="btn btn-primary btn-sm mb-3">添加学生</a>
-
- <h3>查找</h3>
- <form id="query-form" action="/query{inpname}" method="get">
- <input type="text" id="name-input" name="inpname" placeholder="Name">
- <button type="button" onclick="queryStudent()">搜索</button>
- </form>
- <!-- 将输入框输入的内容传到url上面去-->
- <script>
- function queryStudent() {
- var name = document.getElementById("name-input").value;
- var url = "/query" + encodeURIComponent(name);
- window.location.href = url;
- }
- </script>
-
- <table border="1" class = "table table-striped table-responsive-md">
- <thead>
- <tr>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=id&sortDir=' + ${reverseSortDir}}">
- 学号</a>
- </th>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=name&sortDir=' + ${reverseSortDir}}">
- 姓名</a>
- </th>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=gender&sortDir=' + ${reverseSortDir}}">
- 性别</a>
- </th>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=grade&sortDir=' + ${reverseSortDir}}">
- 年级</a>
- </th>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=gan&sortDir=' + ${reverseSortDir}}">
- 专业</a>
- </th>
- <th>
- <a th:href="@{'/page/' + ${currentPage} + '?sortField=class1&sortDir=' + ${reverseSortDir}}">
- 班级</a>
- </th>
- <th> 操作 </th>
- </tr>
- </thead>
- <tbody>
- <tr th:each="name : ${listEmployees}">
- <td th:text="${name.id}"></td>
- <td th:text="${name.name}"></td>
- <td th:text="${name.gender}"></td>
- <td th:text="${name.grade}"></td>
- <td th:text="${name.gan}"></td>
- <td th:text="${name.class1}"></td>
- <td> <a th:href="@{/sc/{id}(id=${name.id})}" class="btn btn-primary">修改</a>
- <a th:href="@{/ee/{id}(id=${name.id})}" class="btn btn-danger">删除</a>
- </td>
- </tr>
- </tbody>
- </table>
-
- <div th:if = "${totalPages > 1}">
- <div class = "row col-sm-10">
- <div class = "col-sm-3">
- 学生人数: [[${totalItems}]]
- </div>
- <div class = "col-sm-5">
- <span th:each="i: ${#numbers.sequence(1, totalPages)}">
- <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">[[${i}]]</a>
- <span th:unless="${currentPage != i}">[[${i}]]</span>
- </span>
- </div>
- <div class = "col-sm-1">
- <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">下一页</a>
- <span th:unless="${currentPage < totalPages}"></span>
- </div>
-
- <div class="col-sm-1">
- <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}+ '?sortField=' + ${sortField} + '&sortDir=' + ${sortDir}}">最后一页</a>
- <span th:unless="${currentPage < totalPages}"></span>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
新增学生
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>添加学生</title>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
-
- </head>
-
- <body>
- <div class="container">
- <meta charset="UTF-8">
- <h1>添加学生</h1>
- <hr>
- <meta charset="UTF-8">
- <h2>请输入学生信息</h2>
-
- <form action="#" th:action="@{/xr}" th:object="${student}" method="POST">
-
- <!-- Add hidden form field to handle update -->
- <input type="hidden" th:field="*{id}" placeholder="学号" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{name}" placeholder="姓名" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{gender}" placeholder="性别" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{grade}" placeholder="年级" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{gan}" placeholder="专业" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{class1}" placeholder="班级" class="form-control mb-4 col-4">
-
- <button type="submit" class="btn btn-info col-2"> 添加 </button>
- </form>
-
- <hr>
-
- <a th:href="@{/}"> 返回首页</a>
- </div>
- </body>
-
- </html>
修改学生信息
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Employee Management System</title>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
- </head>
-
- <body>
- <div class="container">
- <meta charset="UTF-8">
- <h1>修改系统</h1>
- <hr>
- <h2>输入您的修改</h2>
-
- <form action="#" th:action="@{/xr}" th:object="${student}" method="POST">
- <input type="hidden" th:field="*{id}" placeholder="学号" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{name}" placeholder="姓名" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{gender}" placeholder="性别" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{grade}" placeholder="年级" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{gan}" placeholder="专业" class="form-control mb-4 col-4">
-
- <input type="text" th:field="*{class1}" placeholder="班级" class="form-control mb-4 col-4">
- <button type="submit" class="btn btn-info col-2"> 确认修改</button>
- </form>
-
- <hr>
-
- <a th:href="@{/}"> Back to Employee List</a>
- </div>
- </body>
-
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。