帮我写基于springboot+vue考勤管理功能代码
时间: 2023-06-08 09:05:57 浏览: 110
基于springboot+vue的综合考勤管理系统源码+数据库(高分毕业设计).zip
5星 · 资源好评率100%
好的,我会尝试帮您编写这部分代码。以下是一个简单的代码示例,供您参考:
后端代码(基于 Spring Boot):
```java
@RestController
public class AttendanceController {
@Autowired
private AttendanceService attendanceService;
@PostMapping("/attendance")
public void addAttendance(@RequestBody Attendance attendance) {
attendanceService.addAttendance(attendance);
}
@GetMapping("/attendance")
public List<Attendance> getAllAttendances() {
return attendanceService.getAllAttendances();
}
@GetMapping("/attendance/{id}")
public Attendance getAttendanceById(@PathVariable("id") Long id) {
return attendanceService.getAttendanceById(id);
}
@PutMapping("/attendance/{id}")
public void updateAttendance(@PathVariable("id") Long id, @RequestBody Attendance attendance) {
attendanceService.updateAttendance(id, attendance);
}
@DeleteMapping("/attendance/{id}")
public void deleteAttendance(@PathVariable("id") Long id) {
attendanceService.deleteAttendance(id);
}
}
```
```java
@Service
public class AttendanceServiceImpl implements AttendanceService {
@Autowired
private AttendanceRepository attendanceRepository;
@Override
public void addAttendance(Attendance attendance) {
attendanceRepository.save(attendance);
}
@Override
public List<Attendance> getAllAttendances() {
return attendanceRepository.findAll();
}
@Override
public Attendance getAttendanceById(Long id) {
return attendanceRepository.findById(id).orElse(null);
}
@Override
public void updateAttendance(Long id, Attendance attendance) {
Attendance existingAttendance = attendanceRepository.findById(id).orElse(null);
if(existingAttendance != null) {
existingAttendance.setEmployeeId(attendance.getEmployeeId());
existingAttendance.setDate(attendance.getDate());
existingAttendance.setStatus(attendance.getStatus());
attendanceRepository.save(existingAttendance);
}
}
@Override
public void deleteAttendance(Long id) {
attendanceRepository.deleteById(id);
}
}
```
```java
@Entity
@Table(name = "attendance")
public class Attendance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long employeeId;
private LocalDate date;
private AttendanceStatus status;
// getters and setters
}
```
```java
public enum AttendanceStatus {
PRESENT, ABSENT, LEAVE
}
```
```java
public interface AttendanceRepository extends JpaRepository<Attendance, Long> {
}
```
前端代码(基于 Vue.js):
```html
<template>
<div>
<h1>考勤管理</h1>
<router-link to="/add-attendance">新增考勤</router-link>
<table>
<thead>
<tr>
<th>ID</th>
<th>员工ID</th>
<th>日期</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(attendance, index) in attendances" :key="attendance.id">
<td>{{ attendance.id }}</td>
<td>{{ attendance.employeeId }}</td>
<td>{{ attendance.date }}</td>
<td>{{ attendance.status }}</td>
<td>
<router-link :to="{ name: 'edit-attendance', params: { id: attendance.id } }">编辑</router-link>
<button @click="deleteAttendance(index, attendance.id)">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'AttendanceList',
data() {
return {
attendances: [],
}
},
mounted() {
this.loadAttendances()
},
methods: {
loadAttendances() {
axios.get('/attendance')
.then(response => {
this.attendances = response.data
})
},
deleteAttendance(index, id) {
axios.delete(`/attendance/${id}`)
.then(() => {
this.attendances.splice(index, 1)
})
},
},
}
</script>
```
希望这份代码能帮到您,如果有问题可以随时询问我。
阅读全文