一、项目简介
员工信息管理系统是一个用来管理员工信息的系统,可以实现管理员登录、注册、修改密码等功能,还能实现员工的增删改查、员工信息的展示和查询、将员工信息导出为exce、上传图片l等功能。这个系统使用了Spring Boot和MyBatis来实现快速开发和数据库操作的便捷性。使用Spring Boot可以省去繁琐的配置,快速搭建项目框架;使用MyBatis可以通过编写简单的SQL语句实现对数据库的操作,提高开发效率。通过将Spring Boot和MyBatis结合使用,可以更加方便地开发员工信息管理系统。
二、技术栈
1、后端:springboot+mybatis+mysql
2、前端:html+css+thymeleaf
三、创建相关表
在MySQL数据库中创建两个表,分别是管理员信息表、员工信息表。根据Java实体类里面的属性创建对应的字段,将id设置为主键,采用自增长方式。
四、运行截图
登陆界面
主界面
添加员工界面
员工信息更新界面
查询结果
信息导出成excel
5、相关代码
pom.xml文件添加依赖和插件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>staff</artifactId>
<version>1.0-SNAPSHOT</version>
<name>staff</name>
<description>staff</description>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
管理员登录控制器
package com.springboot.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.mapper.StaffMapper;
import com.springboot.pojo.Admin;
import com.springboot.pojo.Staff;
import com.springboot.service.Impl.CrudImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.List;
@Controller
public class LoginController {
Admin admin;
/**
* Controller控制器依赖于业务逻辑层,将CrudImpl自动装配到控制器(LoginController对象)
*/
@Autowired
CrudImpl crud;
/**
* 该方法用来处理前端浏览器的登录请求,登录成功跳转到"员工信息管理系统"主页面,登录失败返回login.html页面
* 步骤:
* 1 调用业务逻辑对象的login方法判断登录是否成功
* 2 登录成功跳转到"员工信息管理系统"主页面
* 3 登录失败返回login.html页面,并给出提示"用户名或者密码错误"
* @Requestparam name 浏览器表单采集的用户名
* @Requestparam password 浏览器表单采集的密码
* @Requestparam model 用来在视图层和控制层之间传递数据的对象
* @return 登录成功跳转到"员工信息管理系统"主页面,登录失败返回index.html页面
*/
@GetMapping("/login")
public String login(){
return "login";
}
@PostMapping("/login")
public String login(@RequestParam("name") String name, @RequestParam("password") String password,
HttpSession session, Model m){
admin = crud.getByName(name,password);
//条件成立。跳转到主页面。否则返回登录页页面
if(admin != null){
session.setAttribute("admin",admin);
return "redirect:table";
}else {
m.addAttribute("msg","用户名或密码错误");
return "login";
}
}
@Autowired
StaffMapper staffMapper;
//跳转到主页面
@GetMapping ("/table")
public String table(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size){
PageHelper.startPage(start,size,"id ASC");
List<Staff> s = staffMapper.listStaff();
PageInfo<Staff> page = new PageInfo<>(s);
m.addAttribute("page",page);
m.addAttribute("name",admin.getName());
return "table";
}
}
管理员注册控制器
package com.springboot.controller;
import com.springboot.pojo.Admin;
import com.springboot.service.Impl.CrudImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class RegisterController {
@Autowired
CrudImpl crud;
//跳转到注册页面
@GetMapping("/register")
public String register()throws Exception{
return "register";
}
@PostMapping("/register")
public String register(@RequestParam("name")String name,@RequestParam("password")String password, Model m)throws Exception{
Admin admin = new Admin();
admin.setName(name);
admin.setPassword(password);
crud.insert(admin);
return "login";
}
}
员工信息管理控制器
package com.springboot.controller;
import com.springboot.pojo.Staff;
import com.springboot.service.Impl.CrudStaffImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller
public class StaffController {
@Autowired
CrudStaffImpl crudStaff;
//跳转到编辑页面
@GetMapping("/edit")
public String edit(@RequestParam("id") int id, Model m){
Staff staff = crudStaff.get(id);
m.addAttribute("staff",staff);
return "edit";
}
@PostMapping("/update")
public String update(Staff staff){
crudStaff.update(staff);
return "redirect:table";
}
//处理删除请求
@GetMapping("/delete")
public String delete(@RequestParam("id")int id){
crudStaff.delete(id);
return "redirect:table";
}
//退出系统,跳转到登陆页面
@GetMapping("/quit")
public String quit(){
return "redirect:login";
}
//处理查询请求,跳转到查询页面
@PostMapping("/search")
public String search(@RequestParam("phone") String phone, Model m, HttpSession session){
Staff s = crudStaff.getByPhone(phone);
m.addAttribute("staff",s);
m.addAttribute("name",session.getAttribute("admin").toString());
return "search";
}
//将员工信息导出为excel
@GetMapping("/excel")
public String excel(){
List<Staff> staffList = crudStaff.findAll();
ExcelExportUtil.export(staffList,"D:\\staff.xls",Staff.class);
return "excel";
}
}