创建一个基于Java的员工工资管理系统

创建一个基于Java的员工工资管理系统是一个涉及多个步骤的过程。以下是一个详细的开发指南,包括项目结构、数据库设计、配置文件、DAO层、Service层、Controller层和前端页面的示例。

1. 需求分析

明确系统的主要功能需求,例如:

  • 员工信息管理(增删改查)
  • 工资计算与发放
  • 考勤管理
  • 奖惩管理
  • 报表统计
  • 用户权限管理

2. 技术选型

确定使用的技术栈:

  • 后端:Spring, Spring MVC, MyBatis
  • 前端:HTML, CSS, JavaScript (可选框架如Vue.js或React.js)
  • 数据库:MySQL
  • 服务器:Tomcat

3. 数据库设计

设计数据库模型,比如员工表、工资表、考勤表等。这里以员工表、工资表和考勤表为例:

employee
CREATE TABLE `employee` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(100) NOT NULL,
  `gender` ENUM('male', 'female'),
  `date_of_birth` DATE,
  `position` VARCHAR(100),
  `department` VARCHAR(100),
  `entry_date` DATE,
  `salary` DECIMAL(10, 2),
  `phone` VARCHAR(20),
  `email` VARCHAR(100),
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);
salary
CREATE TABLE `salary` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `employee_id` INT NOT NULL,
  `month` VARCHAR(20),
  `year` VARCHAR(20),
  `basic_salary` DECIMAL(10, 2),
  `bonus` DECIMAL(10, 2),
  `deduction` DECIMAL(10, 2),
  `net_salary` DECIMAL(10, 2),
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`employee_id`) REFERENCES `employee`(`id`)
);
attendance
CREATE TABLE `attendance` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `employee_id` INT NOT NULL,
  `date` DATE,
  `status` ENUM('present', 'absent', 'late', 'leave'),
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`employee_id`) REFERENCES `employee`(`id`)
);

4. 创建项目结构

使用IDE(如IntelliJ IDEA或Eclipse)创建一个新的Maven项目,并添加必要的依赖项到pom.xml文件中。

5. 配置Spring和MyBatis

src/main/resources目录下创建配置文件,如applicationContext.xmlmybatis-config.xml,用于配置Spring和MyBatis。

applicationContext.xml 示例
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/payroll?useSSL=false&serverTimezone=UTC"/>
    <property name="username" value="root"/>
    <property name="password" value="password"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.payroll.mapper"/>
</bean>

6. 编写Mapper接口

定义MyBatis的Mapper接口来操作数据库。例如,为员工表和工资表创建EmployeeMapper.javaSalaryMapper.java

EmployeeMapper.java
package com.payroll.mapper;

import com.payroll.entity.Employee;
import org.apache.ibatis.annotations.*;

@Mapper
public interface EmployeeMapper {
    @Select("SELECT * FROM employee")
    List<Employee> findAll();

    @Select("SELECT * FROM employee WHERE id = #{id}")
    Employee findById(@Param("id") int id);

    @Insert("INSERT INTO employee(name, gender, date_of_birth, position, department, entry_date, salary, phone, email) VALUES(#{name}, #{gender}, #{date_of_birth}, #{position}, #{department}, #{entry_date}, #{salary}, #{phone}, #{email})")
    int insert(Employee employee);

    @Update("UPDATE employee SET name=#{name}, gender=#{gender}, date_of_birth=#{date_of_birth}, position=#{position}, department=#{department}, entry_date=#{entry_date}, salary=#{salary}, phone=#{phone}, email=#{email} WHERE id=#{id}")
    int update(Employee employee);

    @Delete("DELETE FROM employee WHERE id=#{id}")
    int delete(@Param("id") int id);
}
SalaryMapper.java
package com.payroll.mapper;

import com.payroll.entity.Salary;
import org.apache.ibatis.annotations.*;

@Mapper
public interface SalaryMapper {
    @Select("SELECT * FROM salary")
    List<Salary> findAll();

    @Select("SELECT * FROM salary WHERE id = #{id}")
    Salary findById(@Param("id") int id);

    @Insert("INSERT INTO salary(employee_id, month, year, basic_salary, bonus, deduction, net_salary) VALUES(#{employee_id}, #{month}, #{year}, #{basic_salary}, #{bonus}, #{deduction}, #{net_salary})")
    int insert(Salary salary);

    @Update("UPDATE salary SET employee_id=#{employee_id}, month=#{month}, year=#{year}, basic_salary=#{basic_salary}, bonus=#{bonus}, deduction=#{deduction}, net_salary=#{net_salary} WHERE id=#{id}")
    int update(Salary salary);

    @Delete("DELETE FROM salary WHERE id=#{id}")
    int delete(@Param("id") int id);
}

7. 实现Service层

编写服务层来处理业务逻辑。例如,创建一个EmployeeService.javaSalaryService.java

EmployeeService.java
package com.payroll.service;

import com.payroll.entity.Employee;
import com.payroll.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public List<Employee> getAllEmployees() {
        return employeeMapper.findAll();
    }

    public Employee getEmployeeById(int id) {
        return employeeMapper.findById(id);
    }

    public void addEmployee(Employee employee) {
        employeeMapper.insert(employee);
    }

    public void updateEmployee(Employee employee) {
        employeeMapper.update(employee);
    }

    public void deleteEmployee(int id) {
        employeeMapper.delete(id);
    }
}
SalaryService.java
package com.payroll.service;

import com.payroll.entity.Salary;
import com.payroll.mapper.SalaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;

    public List<Salary> getAllSalaries() {
        return salaryMapper.findAll();
    }

    public Salary getSalaryById(int id) {
        return salaryMapper.findById(id);
    }

    public void addSalary(Salary salary) {
        salaryMapper.insert(salary);
    }

    public void updateSalary(Salary salary) {
        salaryMapper.update(salary);
    }

    public void deleteSalary(int id) {
        salaryMapper.delete(id);
    }
}

8. 控制器层

使用Spring MVC编写控制器来处理HTTP请求。例如,创建一个EmployeeController.javaSalaryController.java

EmployeeController.java
package com.payroll.controller;

import com.payroll.entity.Employee;
import com.payroll.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/list")
    public String listEmployees(Model model) {
        List<Employee> employees = employeeService.getAllEmployees();
        model.addAttribute("employees", employees);
        return "employeeList";
    }

    @GetMapping("/view/{id}")
    public String viewEmployee(@PathVariable("id") int id, Model model) {
        Employee employee = employeeService.getEmployeeById(id);
        model.addAttribute("employee", employee);
        return "employeeView";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("employee", new Employee());
        return "employeeAdd";
    }

    @PostMapping("/add")
    public String addEmployee(@ModelAttribute Employee employee) {
        employeeService.addEmployee(employee);
        return "redirect:/employee/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") int id, Model model) {
        Employee employee = employeeService.getEmployeeById(id);
        model.addAttribute("employee", employee);
        return "employeeEdit";
    }

    @PostMapping("/edit/{id}")
    public String editEmployee(@PathVariable("id") int id, @ModelAttribute Employee employee) {
        employee.setId(id);
        employeeService.updateEmployee(employee);
        return "redirect:/employee/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteEmployee(@PathVariable("id") int id) {
        employeeService.deleteEmployee(id);
        return "redirect:/employee/list";
    }
}
SalaryController.java
package com.payroll.controller;

import com.payroll.entity.Salary;
import com.payroll.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;

    @GetMapping("/list")
    public String listSalaries(Model model) {
        List<Salary> salaries = salaryService.getAllSalaries();
        model.addAttribute("salaries", salaries);
        return "salaryList";
    }

    @GetMapping("/view/{id}")
    public String viewSalary(@PathVariable("id") int id, Model model) {
        Salary salary = salaryService.getSalaryById(id);
        model.addAttribute("salary", salary);
        return "salaryView";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("salary", new Salary());
        return "salaryAdd";
    }

    @PostMapping("/add")
    public String addSalary(@ModelAttribute Salary salary) {
        salaryService.addSalary(salary);
        return "redirect:/salary/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") int id, Model model) {
        Salary salary = salaryService.getSalaryById(id);
        model.addAttribute("salary", salary);
        return "salaryEdit";
    }

    @PostMapping("/edit/{id}")
    public String editSalary(@PathVariable("id") int id, @ModelAttribute Salary salary) {
        salary.setId(id);
        salaryService.updateSalary(salary);
        return "redirect:/salary/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteSalary(@PathVariable("id") int id) {
        salaryService.deleteSalary(id);
        return "redirect:/salary/list";
    }
}

9. 前端页面

根据需要设计前端页面,可以使用Thymeleaf作为模板引擎。例如,创建一个简单的员工列表页面employeeList.html和工资列表页面salaryList.html

employeeList.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Gender</th>
        <th>Date of Birth</th>
        <th>Position</th>
        <th>Department</th>
        <th>Entry Date</th>
        <th>Salary</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Actions</th>
    </tr>
    <tr th:each="employee : ${employees}">
        <td th:text="${employee.id}"></td>
        <td th:text="${employee.name}"></td>
        <td th:text="${employee.gender}"></td>
        <td th:text="${employee.date_of_birth}"></td>
        <td th:text="${employee.position}"></td>
        <td th:text="${employee.department}"></td>
        <td th:text="${employee.entry_date}"></td>
        <td th:text="${employee.salary}"></td>
        <td th:text="${employee.phone}"></td>
        <td th:text="${employee.email}"></td>
        <td>
            <a th:href="@{/employee/view/{id}(id=${employee.id})}">View</a>
            <a th:href="@{/employee/edit/{id}(id=${employee.id})}">Edit</a>
            <a th:href="@{/employee/delete/{id}(id=${employee.id})}">Delete</a>
        </td>
    </tr>
</table>
<a href="/employee/add">Add New Employee</a>
</body>
</html>
salaryList.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Salary List</title>
</head>
<body>
<h1>Salary List</h1>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Employee ID</th>
        <th>Month</th>
        <th>Year</th>
        <th>Basic Salary</th>
        <th>Bonus</th>
        <th>Deduction</th>
        <th>Net Salary</th>
        <th>Actions</th>
    </tr>
    <tr th:each="salary : ${salaries}">
        <td th:text="${salary.id}"></td>
        <td th:text="${salary.employee_id}"></td>
        <td th:text="${salary.month}"></td>
        <td th:text="${salary.year}"></td>
        <td th:text="${salary.basic_salary}"></td>
        <td th:text="${salary.bonus}"></td>
        <td th:text="${salary.deduction}"></td>
        <td th:text="${salary.net_salary}"></td>
        <td>
            <a th:href="@{/salary/view/{id}(id=${salary.id})}">View</a>
            <a th:href="@{/salary/edit/{id}(id=${salary.id})}">Edit</a>
            <a th:href="@{/salary/delete/{id}(id=${salary.id})}">Delete</a>
        </td>
    </tr>
</table>
<a href="/salary/add">Add New Salary</a>
</body>
</html>

10. 测试与部署

完成所有编码后,进行单元测试确保各部分工作正常。之后,可以将应用部署到Tomcat服务器上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值