Java OA系统人事管理模块

### 基于Spring Boot和MySQL实现OA系统人事管理模块

本文将介绍如何使用Spring Boot和MySQL实现一个简单的OA系统人事管理模块,涵盖员工信息管理、入职办理、转正管理和离职管理等功能,并扩展至社保、公积金管理和转正审核流程。

#### 项目结构
我们将采用以下项目结构:
```
oa-system
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── oasystem
│   │   │               ├── controller
│   │   │               │   ├── EmployeeController.java
│   │   │               │   ├── BenefitsController.java
│   │   │               │   └── PromotionController.java
│   │   │               ├── entity
│   │   │               │   ├── Employee.java
│   │   │               │   ├── SocialSecurity.java
│   │   │               │   ├── HousingFund.java
│   │   │               │   └── Promotion.java
│   │   │               ├── repository
│   │   │               │   ├── EmployeeRepository.java
│   │   │               │   ├── SocialSecurityRepository.java
│   │   │               │   ├── HousingFundRepository.java
│   │   │               │   └── PromotionRepository.java
│   │   │               ├── service
│   │   │               │   ├── EmployeeService.java
│   │   │               │   ├── BenefitsService.java
│   │   │               │   └── PromotionService.java
│   │   │               └── OaSystemApplication.java
│   │   └── resources
│   │       ├── application.properties
│   │       └── templates
└── pom.xml
```

#### 实现步骤

##### 1. 创建实体类

```java
package com.example.oasystem.entity;

import javax.persistence.*;
import java.util.Date;

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String gender;
    private int age;
    private String contactInfo;

    @Temporal(TemporalType.DATE)
    private Date hireDate;
    private String position;
    private String salaryGrade;
    private String status; // 在职、试用期、离职等

    // Getters and Setters
}

@Entity
public class SocialSecurity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long employeeId;
    private String socialSecurityNumber;
    private String status; // 已办理、未办理

    // Getters and Setters
}

@Entity
public class HousingFund {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long employeeId;
    private String housingFundNumber;
    private String status; // 已办理、未办理

    // Getters and Setters
}

@Entity
public class Promotion {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long employeeId;
    private Date promotionDate;
    private String newPosition;
    private String newSalaryGrade;
    private String status; // 待审核、已转正、拒绝

    // Getters and Setters
}
```

##### 2. 创建Repository接口

```java
package com.example.oasystem.repository;

import com.example.oasystem.entity.Employee;
import com.example.oasystem.entity.SocialSecurity;
import com.example.oasystem.entity.HousingFund;
import com.example.oasystem.entity.Promotion;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {}

public interface SocialSecurityRepository extends JpaRepository<SocialSecurity, Long> {}

public interface HousingFundRepository extends JpaRepository<HousingFund, Long> {}

public interface PromotionRepository extends JpaRepository<Promotion, Long> {}
```

##### 3. 创建Service类

```java
package com.example.oasystem.service;

import com.example.oasystem.entity.Employee;
import com.example.oasystem.entity.SocialSecurity;
import com.example.oasystem.entity.HousingFund;
import com.example.oasystem.entity.Promotion;
import com.example.oasystem.repository.EmployeeRepository;
import com.example.oasystem.repository.SocialSecurityRepository;
import com.example.oasystem.repository.HousingFundRepository;
import com.example.oasystem.repository.PromotionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

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

    public Optional<Employee> findById(Long id) {
        return employeeRepository.findById(id);
    }

    public Employee save(Employee employee) {
        return employeeRepository.save(employee);
    }

    public void deleteById(Long id) {
        employeeRepository.deleteById(id);
    }
}

@Service
public class BenefitsService {

    @Autowired
    private SocialSecurityRepository socialSecurityRepository;

    @Autowired
    private HousingFundRepository housingFundRepository;

    public List<SocialSecurity> findAllSocialSecurities() {
        return socialSecurityRepository.findAll();
    }

    public Optional<SocialSecurity> findSocialSecurityById(Long id) {
        return socialSecurityRepository.findById(id);
    }

    public SocialSecurity saveSocialSecurity(SocialSecurity socialSecurity) {
        return socialSecurityRepository.save(socialSecurity);
    }

    public void deleteSocialSecurityById(Long id) {
        socialSecurityRepository.deleteById(id);
    }

    public List<HousingFund> findAllHousingFunds() {
        return housingFundRepository.findAll();
    }

    public Optional<HousingFund> findHousingFundById(Long id) {
        return housingFundRepository.findById(id);
    }

    public HousingFund saveHousingFund(HousingFund housingFund) {
        return housingFundRepository.save(housingFund);
    }

    public void deleteHousingFundById(Long id) {
        housingFundRepository.deleteById(id);
    }
}

@Service
public class PromotionService {

    @Autowired
    private PromotionRepository promotionRepository;

    public List<Promotion> findAll() {
        return promotionRepository.findAll();
    }

    public Optional<Promotion> findById(Long id) {
        return promotionRepository.findById(id);
    }

    public Promotion save(Promotion promotion) {
        return promotionRepository.save(promotion);
    }

    public void deleteById(Long id) {
        promotionRepository.deleteById(id);
    }
}
```

##### 4. 创建Controller类

```java
package com.example.oasystem.controller;

import com.example.oasystem.entity.Employee;
import com.example.oasystem.entity.SocialSecurity;
import com.example.oasystem.entity.HousingFund;
import com.example.oasystem.entity.Promotion;
import com.example.oasystem.service.EmployeeService;
import com.example.oasystem.service.BenefitsService;
import com.example.oasystem.service.PromotionService;
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.findAll();
    }

    @GetMapping("/{id}")
    public Employee getEmployeeById(@PathVariable Long id) {
        return employeeService.findById(id).orElse(null);
    }

    @PostMapping
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeService.save(employee);
    }

    @PutMapping("/{id}")
    public Employee updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
        Employee existingEmployee = employeeService.findById(id).orElse(null);
        if (existingEmployee != null) {
            existingEmployee.setName(employee.getName());
            existingEmployee.setGender(employee.getGender());
            existingEmployee.setAge(employee.getAge());
            existingEmployee.setContactInfo(employee.getContactInfo());
            existingEmployee.setHireDate(employee.getHireDate());
            existingEmployee.setPosition(employee.getPosition());
            existingEmployee.setSalaryGrade(employee.getSalaryGrade());
            existingEmployee.setStatus(employee.getStatus());
            return employeeService.save(existingEmployee);
        }
        return null;
    }

    @DeleteMapping("/{id}")
    public void deleteEmployee(@PathVariable Long id) {
        employeeService.deleteById(id);
    }
}

@RestController
@RequestMapping("/benefits")
public class BenefitsController {

    @Autowired
    private BenefitsService benefitsService;

    @GetMapping("/social-security")
    public List<SocialSecurity> getAllSocialSecurities() {
        return benefitsService.findAllSocialSecurities();
    }

    @GetMapping("/social-security/{id}")
    public SocialSecurity getSocialSecurityById(@PathVariable Long id) {
        return benefitsService.findSocialSecurityById(id).orElse(null);
    }

    @PostMapping("/social-security")
    public SocialSecurity createSocialSecurity(@RequestBody SocialSecurity socialSecurity) {
        return benefitsService.saveSocialSecurity(socialSecurity);
    }

    @PutMapping("/social-security/{id}")
    public SocialSecurity updateSocialSecurity(@PathVariable Long id, @RequestBody SocialSecurity socialSecurity) {
        SocialSecurity existingSocialSecurity = benefitsService.findSocialSecurityById(id).orElse(null);
        if (existingSocialSecurity != null) {
            existingSocialSecurity.setSocialSecurityNumber(socialSecurity.getSocialSecurityNumber());
            existingSocialSecurity.setStatus(socialSecurity.getStatus());
            return benefitsService.saveSocialSecurity(existingSocialSecurity);
        }
        return null;
    }

    @DeleteMapping("/social-security/{id}")
    public void deleteSocialSecurity(@PathVariable Long id) {
        benefitsService.deleteSocialSecurityById(id);
    }

    @GetMapping("/housing-fund")
    public List<HousingFund> getAllHousingFunds() {
        return benefitsService.findAllHousingFunds();
    }

    @GetMapping("/housing-fund/{id}")
    public HousingFund getHousingFundById(@PathVariable Long id) {
        return benefitsService.findHousingFundById(id).orElse(null);
    }

    @PostMapping("/housing-fund")
    public HousingFund createHousingFund(@RequestBody HousingFund housingFund) {
        return benefitsService.saveHousingFund(housingFund);
    }

    @PutMapping("/housing-fund/{id}")
    public HousingFund updateHousingFund(@PathVariable Long id, @RequestBody HousingFund housingFund) {
        HousingFund existingHousingFund = benefitsService.findHousingFundById(id).orElse(null);
        if (existingHousingFund != null) {
            existingHousingFund.setHousingFundNumber(housingFund.getHousingFundNumber());
            existingHousingFund.setStatus(housingFund.getStatus());
            return benefitsService.saveHousingFund(existingHousingFund);
        }
        return null;
    }

    @DeleteMapping("/housing-fund/{id}")
    public void deleteHousingFund(@PathVariable Long id) {
        benefitsService.deleteHousingFundById(id);
    }
}

@RestController
@RequestMapping("/promotions")
public class PromotionController {

    @Autowired
    private PromotionService promotionService;

    @GetMapping
    public List<Promotion> getAllPromotions() {
        return promotionService.findAll();
    }

    @GetMapping("/{id}")
    public Promotion getPromotionById(@PathVariable Long id) {
        return promotionService.findById(id).orElse(null);
    }

    @PostMapping
    public Promotion createPromotion(@RequestBody Promotion promotion) {
        return promotionService.save(promotion);
    }

    @PutMapping("/{id}")
    public Promotion updatePromotion(@PathVariable Long id, @RequestBody Promotion promotion) {
        Promotion existingPromotion = promotionService.findById(id).orElse(null);
        if (existingPromotion != null) {
            existingPromotion.setPromotionDate(promotion.getPromotionDate());
            existingPromotion.setNewPosition(promotion.getNewPosition());
            existingPromotion.setNewSalaryGrade(promotion.getNewSalaryGrade());
            existingPromotion.setStatus(promotion.getStatus());
            return promotionService.save(existingPromotion);
        }
        return null;
    }

    @DeleteMapping("/{id}")
    public void deletePromotion(@PathVariable Long id) {
        promotionService.deleteById(id);
    }
}
```

##### 5. 配置MySQL数据库

确保 `src/main/resources/application.properties` 文件中包含以下配置:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/oasystem
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
```

##### 6. 启动应用

在 `OaSystemApplication` 类中运行应用:
```java
package com.example.oasystem;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OaSystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(OaSystemApplication.class, args);
    }
}
```

通过以上步骤,我们成功地为OA系统人事管理模块添加了员工的社保、公积金管理以及转正审核流程的功能。

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值