您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在现代企业中,员工管理系统是一个非常重要的工具,它可以帮助企业高效地管理员工信息、考勤记录、薪资发放等。本文将详细介绍如何使用Java语言实现一个简单的员工管理系统。通过本文的学习,读者将掌握Java编程的基本技能,并能够独立开发一个小型的管理系统。
在开始开发之前,我们需要明确系统的功能需求。一个简单的员工管理系统通常包括以下功能:
本系统采用MVC(Model-View-Controller)架构,将系统分为模型层、视图层和控制层。模型层负责数据的存储和业务逻辑处理,视图层负责用户界面的展示,控制层负责处理用户请求并调用相应的业务逻辑。
系统使用MySQL数据库存储数据,主要包含以下表:
根据系统需求,我们设计以下类:
在pom.xml
中添加以下依赖:
<dependencies>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Thymeleaf模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
package com.example.employeemanagement.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "gender")
private String gender;
@Column(name = "birth_date")
private Date birthDate;
@Column(name = "department_id")
private Long departmentId;
@Column(name = "position")
private String position;
@Column(name = "salary")
private Double salary;
}
package com.example.employeemanagement.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
}
package com.example.employeemanagement.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "attendance")
public class Attendance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_id")
private Long employeeId;
@Column(name = "date")
private Date date;
@Column(name = "status")
private String status;
}
package com.example.employeemanagement.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "salary")
public class Salary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_id")
private Long employeeId;
@Column(name = "amount")
private Double amount;
@Column(name = "date")
private Date date;
}
package com.example.employeemanagement.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "role")
private String role;
}
package com.example.employeemanagement.dao;
import com.example.employeemanagement.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeDAO extends JpaRepository<Employee, Long> {
}
package com.example.employeemanagement.dao;
import com.example.employeemanagement.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DepartmentDAO extends JpaRepository<Department, Long> {
}
package com.example.employeemanagement.dao;
import com.example.employeemanagement.entity.Attendance;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AttendanceDAO extends JpaRepository<Attendance, Long> {
}
package com.example.employeemanagement.dao;
import com.example.employeemanagement.entity.Salary;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SalaryDAO extends JpaRepository<Salary, Long> {
}
package com.example.employeemanagement.dao;
import com.example.employeemanagement.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDAO extends JpaRepository<User, Long> {
User findByUsername(String username);
}
package com.example.employeemanagement.service;
import com.example.employeemanagement.entity.Employee;
import com.example.employeemanagement.dao.EmployeeDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeService {
@Autowired
private EmployeeDAO employeeDAO;
public List<Employee> getAllEmployees() {
return employeeDAO.findAll();
}
public Employee getEmployeeById(Long id) {
return employeeDAO.findById(id).orElse(null);
}
public Employee addEmployee(Employee employee) {
return employeeDAO.save(employee);
}
public Employee updateEmployee(Employee employee) {
return employeeDAO.save(employee);
}
public void deleteEmployee(Long id) {
employeeDAO.deleteById(id);
}
}
package com.example.employeemanagement.service;
import com.example.employeemanagement.entity.Department;
import com.example.employeemanagement.dao.DepartmentDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DepartmentService {
@Autowired
private DepartmentDAO departmentDAO;
public List<Department> getAllDepartments() {
return departmentDAO.findAll();
}
public Department getDepartmentById(Long id) {
return departmentDAO.findById(id).orElse(null);
}
public Department addDepartment(Department department) {
return departmentDAO.save(department);
}
public Department updateDepartment(Department department) {
return departmentDAO.save(department);
}
public void deleteDepartment(Long id) {
departmentDAO.deleteById(id);
}
}
package com.example.employeemanagement.service;
import com.example.employeemanagement.entity.Attendance;
import com.example.employeemanagement.dao.AttendanceDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AttendanceService {
@Autowired
private AttendanceDAO attendanceDAO;
public List<Attendance> getAllAttendances() {
return attendanceDAO.findAll();
}
public Attendance getAttendanceById(Long id) {
return attendanceDAO.findById(id).orElse(null);
}
public Attendance addAttendance(Attendance attendance) {
return attendanceDAO.save(attendance);
}
public Attendance updateAttendance(Attendance attendance) {
return attendanceDAO.save(attendance);
}
public void deleteAttendance(Long id) {
attendanceDAO.deleteById(id);
}
}
package com.example.employeemanagement.service;
import com.example.employeemanagement.entity.Salary;
import com.example.employeemanagement.dao.SalaryDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SalaryService {
@Autowired
private SalaryDAO salaryDAO;
public List<Salary> getAllSalaries() {
return salaryDAO.findAll();
}
public Salary getSalaryById(Long id) {
return salaryDAO.findById(id).orElse(null);
}
public Salary addSalary(Salary salary) {
return salaryDAO.save(salary);
}
public Salary updateSalary(Salary salary) {
return salaryDAO.save(salary);
}
public void deleteSalary(Long id) {
salaryDAO.deleteById(id);
}
}
package com.example.employeemanagement.service;
import com.example.employeemanagement.entity.User;
import com.example.employeemanagement.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDAO userDAO;
public User findByUsername(String username) {
return userDAO.findByUsername(username);
}
public User addUser(User user) {
return userDAO.save(user);
}
}
package com.example.employeemanagement.controller;
import com.example.employeemanagement.entity.Employee;
import com.example.employeemanagement.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/{id}")
public Employee getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@PostMapping
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
@PutMapping
public Employee updateEmployee(@RequestBody Employee employee) {
return employeeService.updateEmployee(employee);
}
@DeleteMapping("/{id}")
public void deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
}
}
package com.example.employeemanagement.controller;
import com.example.employeemanagement.entity.Department;
import com.example.employeemanagement.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/departments")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@GetMapping
public List<Department> getAllDepartments() {
return departmentService.getAllDepartments();
}
@GetMapping("/{id}")
public Department getDepartmentById(@PathVariable Long id) {
return departmentService.getDepartmentById(id);
}
@PostMapping
public Department addDepartment(@RequestBody Department department) {
return departmentService.addDepartment(department);
}
@PutMapping
public Department updateDepartment(@RequestBody Department department) {
return departmentService.updateDepartment(department);
}
@DeleteMapping("/{id}")
public void deleteDepartment(@PathVariable Long id) {
departmentService.deleteDepartment(id);
}
}
package com.example.employeemanagement.controller;
import com.example.employeemanagement.entity.Attendance;
import com.example.employeemanagement.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/attendances")
public class AttendanceController {
@Autowired
private AttendanceService attendanceService;
@GetMapping
public List<Attendance> getAllAttendances() {
return attendanceService.getAllAttendances();
}
@GetMapping("/{id}")
public Attendance getAttendanceById(@PathVariable Long id) {
return attendanceService.getAttendanceById(id);
}
@PostMapping
public Attendance addAttendance(@RequestBody Attendance attendance) {
return attendanceService.addAttendance(attendance);
}
@PutMapping
public Attendance updateAttendance(@RequestBody Attendance attendance) {
return attendanceService.updateAttendance(attendance);
}
@DeleteMapping("/{id}")
public void deleteAttendance(@PathVariable Long id) {
attendanceService.deleteAttendance(id);
}
}
package com.example.employeemanagement.controller;
import com.example.employeemanagement.entity.Salary;
import com.example.employeemanagement.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/salaries")
public class SalaryController {
@Autowired
private SalaryService salaryService;
@GetMapping
public List<Salary> getAllSalaries() {
return salaryService.getAllSalaries();
}
@GetMapping("/{id}")
public Salary getSalaryById(@PathVariable Long id) {
return salaryService.getSalaryById(id);
}
@PostMapping
public Salary addSalary(@RequestBody Salary salary) {
return salaryService.addSalary(salary);
}
@PutMapping
public Salary updateSalary(@RequestBody Salary salary) {
return salaryService.updateSalary(salary);
}
@DeleteMapping("/{id}")
public void deleteSalary(@PathVariable Long id) {
salaryService.deleteSalary(id);
}
}
package com.example.employeemanagement.controller;
import com.example.employeemanagement.entity.User;
import com.example.employeemanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public User registerUser(@RequestBody User user) {
return userService.addUser(user);
}
@PostMapping("/login")
public User loginUser(@RequestBody User user) {
return userService.findByUsername(user.getUsername());
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Employee Management System</title>
</head>
<body>
<h1>Employee Management System</h1>
<ul>
<li><a href="/employees">Employee List</a></li>
<li><a href="/departments">Department List</a></li>
<li><a href="/attendances">Attendance List</a></li>
<li><a href="/salaries">Salary List</a></li>
</ul>
</body>
</html>
”`html <!DOCTYPE html>
ID | <
---|