### 1. 数据库配置
```properties
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/hrm_db
spring.datasource.username=root
spring.datasource.password=root_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
```
### 2. 实体类定义
```java
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private double baseSalary;
private double allowances;
private double deductions;
private Date joinDate;
// other fields, getters and setters
}
@Entity
@Table(name = "job_positions")
public class JobPosition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
// other fields, getters and setters
}
@Entity
@Table(name = "job_applications")
public class JobApplication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "job_position_id")
private JobPosition appliedPosition;
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee applicant;
private Date applicationDate;
// other fields, getters and setters
}
@Entity
@Table(name = "training_programs")
public class TrainingProgram {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
// other fields, getters and setters
}
```
### 3. 数据访问层 (Repository)
```java
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
// Custom queries if needed
}
public interface JobPositionRepository extends CrudRepository<JobPosition, Long> {
}
public interface JobApplicationRepository extends CrudRepository<JobApplication, Long> {
}
public interface TrainingProgramRepository extends CrudRepository<TrainingProgram, Long> {
}
```
### 4. 服务层 (Service)
```java
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public void addEmployee(Employee employee) {
employeeRepository.save(employee);
}
public Employee getEmployee(Long id) {
Optional<Employee> optionalEmployee = employeeRepository.findById(id);
return optionalEmployee.orElse(null);
}
public void updateEmployee(Employee employee) {
employeeRepository.save(employee);
}
public void deleteEmployee(Long id) {
employeeRepository.deleteById(id);
}
// Additional methods as needed
}
@Service
public class PayrollService {
public double calculateSalary(Employee employee) {
return employee.getBaseSalary() + employee.getAllowances() - employee.getDeductions();
}
// Additional payroll-related methods
}
@Service
public class AttendanceService {
// Methods for marking attendance, handling leave, overtime, etc.
}
@Service
public class RecruitmentService {
@Autowired
private JobPositionRepository jobPositionRepository;
@Autowired
private JobApplicationRepository jobApplicationRepository;
public void publishJobPosition(JobPosition position) {
jobPositionRepository.save(position);
}
public List<JobPosition> getAvailablePositions() {
return (List<JobPosition>) jobPositionRepository.findAll();
}
public void applyForJob(JobApplication application) {
jobApplicationRepository.save(application);
}
// Additional recruitment-related methods
}
@Service
public class TrainingService {
@Autowired
private TrainingProgramRepository trainingProgramRepository;
public void createTrainingProgram(TrainingProgram program) {
trainingProgramRepository.save(program);
}
public List<TrainingProgram> getAvailablePrograms() {
return (List<TrainingProgram>) trainingProgramRepository.findAll();
}
// Additional training-related methods
}
```
### 5. 控制层 (Controller)
```java
@RestController
@RequestMapping("/api")
public class HRMController {
@Autowired
private EmployeeService employeeService;
@Autowired
private PayrollService payrollService;
@Autowired
private RecruitmentService recruitmentService;
@Autowired
private TrainingService trainingService;
// Define API endpoints for HRM functionalities
}
```
### 6. 安全控制、定时任务、异常处理和数据验证、性能优化等功能
#### 安全控制
```java
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**").authenticated() // 需要认证才能访问API
.anyRequest().permitAll() // 其他请求允许访问
.and().formLogin().permitAll() // 允许表单登录
.and().logout().permitAll(); // 允许注销
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
#### 定时任务
```java
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class ScheduledTasks {
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
public void generateDailyReport() {
// 实现报表生成逻辑
}
}
```
#### 异常处理和数据验证
```java
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse("Internal Server Error", ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping
public ResponseEntity<Employee> addEmployee(@Valid @RequestBody Employee employee) {
employeeService.addEmployee(employee);
return ResponseEntity.ok(employee);
}
}
```
#### 性能优化
```java
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Cacheable("employees")
public Employee getEmployee(Long id) {
Optional<Employee> optionalEmployee = employeeRepository.findById(id);
return optionalEmployee.orElse(null);
}
}
```
06-30
1849