为了实现一个全面的OA系统考勤管理模块,我们将采用Spring Boot框架和MySQL数据库。我们将分步骤实现以下功能:
1. 考勤数据统计
2. 考勤签到
3. 考勤补签
4. 请假管理
5. 调休管理
## 1. 项目结构
```
attendance-management/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── attendance/
│ │ │ ├── AttendanceManagementApplication.java
│ │ │ ├── controller/
│ │ │ ├── model/
│ │ │ ├── repository/
│ │ │ └── service/
│ │ └── resources/
│ │ ├── application.properties
│ │ └── templates/
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── attendance/
├── pom.xml
└── README.md
```
## 2. 配置 `pom.xml`
```xml
<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>com.example</groupId>
<artifactId>attendance-management</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok for reducing boilerplate code -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring Boot Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```
## 3. 数据库配置 (`application.properties`)
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/attendance_db
spring.datasource.username=root
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
```
## 4. 模型类 (`model` 包)
### 4.1 考勤记录 (`AttendanceRecord.java`)
```java
package com.example.attendance.model;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Data
public class AttendanceRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long employeeId;
private LocalDateTime checkInTime;
private LocalDateTime checkOutTime;
private boolean isLate;
private boolean isEarlyLeave;
private boolean isOvertime;
private String remarks;
}
```
### 4.2 请假记录 (`LeaveRecord.java`)
```java
package com.example.attendance.model;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Data
public class LeaveRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long employeeId;
private LocalDate startDate;
private LocalDate endDate;
private String leaveType; // e.g., Sick Leave, Annual Leave, etc.
private String status; // e.g., Pending, Approved, Rejected
private String remarks;
}
```
### 4.3 调休记录 (`AdjustRestRecord.java`)
```java
package com.example.attendance.model;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Data
public class AdjustRestRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long employeeId;
private LocalDate adjustDate;
private String status; // e.g., Pending, Approved, Rejected
private String remarks;
}
```
## 5. 仓库接口 (`repository` 包)
### 5.1 考勤记录仓库接口 (`AttendanceRecordRepository.java`)
```java
package com.example.attendance.repository;
import com.example.attendance.model.AttendanceRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface AttendanceRecordRepository extends JpaRepository<AttendanceRecord, Long> {
List<AttendanceRecord> findByEmployeeId(Long employeeId);
}
```
### 5.2 请假记录仓库接口 (`LeaveRecordRepository.java`)
```java
package com.example.attendance.repository;
import com.example.attendance.model.LeaveRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface LeaveRecordRepository extends JpaRepository<LeaveRecord, Long> {
List<LeaveRecord> findByEmployeeId(Long employeeId);
}
```
### 5.3 调休记录仓库接口 (`AdjustRestRecordRepository.java`)
```java
package com.example.attendance.repository;
import com.example.attendance.model.AdjustRestRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface AdjustRestRecordRepository extends JpaRepository<AdjustRestRecord, Long> {
List<AdjustRestRecord> findByEmployeeId(Long employeeId);
}
```
## 6. 服务类 (`service` 包)
### 6.1 考勤记录服务类 (`AttendanceRecordService.java`)
```java
package com.example.attendance.service;
import com.example.attendance.model.AttendanceRecord;
import com.example.attendance.repository.AttendanceRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AttendanceRecordService {
@Autowired
private AttendanceRecordRepository repository;
public List<AttendanceRecord> getAllRecords() {
return repository.findAll();
}
public List<AttendanceRecord> getRecordsByEmployeeId(Long employeeId) {
return repository.findByEmployeeId(employeeId);
}
public AttendanceRecord saveRecord(AttendanceRecord record) {
return repository.save(record);
}
public void deleteRecord(Long id) {
repository.deleteById(id);
}
}
```
### 6.2 请假记录服务类 (`LeaveRecordService.java`)
```java
package com.example.attendance.service;
import com.example.attendance.model.LeaveRecord;
import com.example.attendance.repository.LeaveRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LeaveRecordService {
@Autowired
private LeaveRecordRepository repository;
public List<LeaveRecord> getAllRecords() {
return repository.findAll();
}
public List<LeaveRecord> getRecordsByEmployeeId(Long employeeId) {
return repository.findByEmployeeId(employeeId);
}
public LeaveRecord saveRecord(LeaveRecord record) {
return repository.save(record);
}
public void deleteRecord(Long id) {
repository.deleteById(id);
}
}
```
### 6.3 调休记录服务类 (`AdjustRestRecordService.java`)
```java
package com.example.attendance.service;
import com.example.attendance.model.AdjustRestRecord;
import com.example.attendance.repository.AdjustRestRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdjustRestRecordService {
@Autowired
private AdjustRestRecordRepository repository;
public List<AdjustRestRecord> getAllRecords() {
return repository.findAll();
}
public List<AdjustRestRecord> getRecordsByEmployeeId(Long employeeId) {
return repository.findByEmployeeId(employeeId);
}
public AdjustRestRecord saveRecord(AdjustRestRecord record) {
return repository.save(record);
}
public void deleteRecord(Long id) {
repository.deleteById(id);
}
}
```
## 7. 控制器类 (`controller` 包)
### 7.1 考勤记录控制器 (`AttendanceRecordController.java`)
```java
package com.example.attendance.controller;
import com.example.attendance.model.AttendanceRecord;
import com.example.attendance.service.AttendanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/attendance")
public class AttendanceRecordController {
@Autowired
private AttendanceRecordService service;
@GetMapping("/records")
public List<AttendanceRecord> getAllRecords() {
return service.getAllRecords();
}
@GetMapping("/records/{employeeId}")
public List<AttendanceRecord> getRecordsByEmployeeId(@PathVariable Long employeeId) {
return service.getRecordsByEmployeeId(employeeId);
}
@PostMapping("/record")
public AttendanceRecord saveRecord(@RequestBody AttendanceRecord record) {
return service.saveRecord(record);
}
@DeleteMapping("/record/{id}")
public void deleteRecord(@PathVariable Long id) {
service.deleteRecord(id);
}
}
```
### 7.2 请假记录控制器 (`LeaveRecordController.java`)
```java
package com.example.attendance.controller;
import com.example.attendance.model.LeaveRecord;
import com.example.attendance.service.LeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/leave")
public class LeaveRecordController {
@Autowired
private LeaveRecordService service;
@GetMapping("/records")
public List<LeaveRecord> getAllRecords() {
return service.getAllRecords();
}
@GetMapping("/records/{employeeId}")
public List<LeaveRecord> getRecordsByEmployeeId(@PathVariable Long employeeId) {
return service.getRecordsByEmployeeId(employeeId);
}
@PostMapping("/record")
public LeaveRecord saveRecord(@RequestBody LeaveRecord record) {
return service.saveRecord(record);
}
@DeleteMapping("/record/{id}")
public void deleteRecord(@PathVariable Long id) {
service.deleteRecord(id);
}
}
```
### 7.3 调休记录控制器 (`AdjustRestRecordController.java`)
```java
package com.example.attendance.controller;
import com.example.attendance.model.AdjustRestRecord;
import com.example.attendance.service.AdjustRestRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/adjustrest")
public class AdjustRestRecordController {
@Autowired
private AdjustRestRecordService service;
@GetMapping("/records")
public List<AdjustRestRecord> getAllRecords() {
return service.getAllRecords();
}
@GetMapping("/records/{employeeId}")
public List<AdjustRestRecord> getRecordsByEmployeeId(@PathVariable Long employeeId) {
return service.getRecordsByEmployeeId(employeeId);
}
@PostMapping("/record")
public AdjustRestRecord saveRecord(@RequestBody AdjustRestRecord record) {
return service.saveRecord(record);
}
@DeleteMapping("/record/{id}")
public void deleteRecord(@PathVariable Long id) {
service.deleteRecord(id);
}
}
```
### 主应用程序类 (`AttendanceManagementApplication.java`)
```java
package com.example.attendance;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AttendanceManagementApplication {
public static void main(String[] args) {
SpringApplication.run(AttendanceManagementApplication.class, args);
}
}
```
以上代码实现了一个简单的考勤管理模块,包含考勤数据统计、考勤签到、考勤补签、请假管理和调休管理的基本功能。