目录
前言:本项目不仅可以用来做学生管理系统,还可以用来做图书管理系统,商品管理系统,食堂菜系系统等。。。。
一、项目内容
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值得我们深入的去学习,要用自己的耐心去发现更深奥更有趣的功能。
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 {
@Query("select h from Student h where h.name like %:name%")
List 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 getTobtainStuden();//获取学生信息
void newStudents(Student name);//新增
Student get(long id);//获取指定ID
void delStudent(long id);//删除
List findByRoomContaining(String name);
Student seekStudent(Long id);//查找
// List bb(String name);
void deleteAByid(Long id);
Page 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 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 findByRoomContaining(String name) {
return null;
}
@Override
public Student seekStudent(Long id) {
Optional 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 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 page = DepositStudent.paging(pageNo, pageSize, sortField, sortDir);
List 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 students = DepositStudent.findAll();
// model.addAttribute("student",students);
// return "search";
// }
//指定查询
// @PostMapping("/student/inpname")
// public String searchBook(@RequestParam("inpname") String inpname, Model model) {
// List students = DepositStudent.paging(inpname);
// model.addAttribute("books",students);
//
//
// return "search";
// }
@GetMapping("/query{inpname}")
public String query(@PathVariable(value = "inpname") String inpame,Model model){
List listA = DepositStudent.findByRoomContaining(inpame);
model.addAttribute("listA",listA);
return "index";
}
}
用来完成各种页面的框架和数据的呈现(可视化)
主页面
Employee Management System
新增学生
添加学生
修改学生信息
Employee Management System