项目目录
1、在Result定义返回结果
package com.edu.result;
import lombok.Data;
import java.io.Serializable;
/**
* 后端统一返回结果
* @param <T>
*/
@Data
public class Result<T> implements Serializable {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
public static <T> Result<T> success() {
Result<T> result = new Result<T>();
result.code = 1;
return result;
}
public static <T> Result<T> success(T object) {
Result<T> result = new Result<T>();
result.data = object;
result.code = 1;
return result;
}
public static <T> Result<T> error(String msg) {
Result result = new Result();
result.msg = msg;
result.code = 0;
return result;
}
}
2、写实体类(entity,和表中的属性一一对应)
package com.edu.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {
private String classId;
private int classSize;
private String headTeacher;
private int attendanceTimes;
private int focus;
}
3、在(vo)中定义类,写自己想要返回的属性
package com.edu.vo;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AbsentStuVo {
private String personName;
private String studentId;
private String classId;
private Integer attendanceStatus;
private String headTeacher;
private String attendRate;
}
3、书写mapper层
package com.edu.mapper;
import com.edu.vo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
/**
* 根据学生id显示请假列表
* @param studentId
* @return
*/
@Select("select start_time, end_time, reason, leave_status, submit_time from leave_applications where student_id=#{studentId}")
List<LeaveApplicationsVo> listLeaveApplications(String studentId);
/**
* 根据学生id显示迟到列表
* @param studentId
* @return
*/
@Select("select l.late_date,c.course_session,c.course_name , l.teacher,l.entry_time,l.late_duration from late_attendance l left join course c on c.course_id=l.course_id where l.student_id=#{studentId}")
List<LateAttendanceVo> listLateAttendance(String studentId);
/**
*根据学生id显示旷课列表
* @param studentId
* @return
*/
@Select("select a.absence_date,a.class_period,u.course_name,a.teacher from absence_record a left join course u on u.course_id=a.course_id where a.student_id=#{studentId}")
List<AbsenceRecordVo> listAbsenceRecord(String studentId);
/**
* 请假列表详情
* @param studentId
* @return
*/
@Select("select l.student_id, s.student_name, l.start_time,l.end_time,l.reason,l.leave_status from leave_applications l left join student s on l.student_id=s.student_id where l.student_id=#{studentId}")
List<LeaveApplicationsdetailVo> LeaveApplicationsdetail(String studentId);
/**
* 根据学生id查询该学生各科的缺勤次数
* @param studentId
* @return
*/
@Select("SELECT c.course_name, IFNULL(COUNT( DISTINCT a.absent_id), 0) AS absence_count FROM course c LEFT JOIN absent_students a ON c.course_id = a.course_id AND a.student_id = #{studentId} GROUP BY c.course_name")
List<CourseAbsenceCountVo> viewAbsenceCountsForSubjects(String studentId);
/**
* 根据学生id查询学生信息
* @param studentId
* @return
*/
@Select("SELECT s.student_id, s.student_name, c.class_name, c.head_teacher," +
" (pa.attendance_count /(pa.attendance_count + pa.leave_count+pa.absent_count)) * 100 AS attendance_rate " +
"FROM student s JOIN class c ON s.class_id = c.class_id JOIN personal_attendance pa ON s.student_id = pa.student_id " +
"WHERE s.student_id = #{studentId}")
List<StudentDetailVO> viewStudentDetail(String studentId);
/**
* 申请请假
* @param studentId 学生id
* @param startTime 开始时间
* @param endTime 结束时间
* @param reason 原因
* @return
*/
@Insert("INSERT INTO leave_applications (student_id, start_time, end_time, reason, submit_time)" +
"VALUES (#{studentId}, #{startTime}, #{endTime}, #{reason}, NOW())")
Integer insertLeaveRequest(String studentId, String startTime, String endTime, String reason);
}
4、编写service层
package com.edu.service;
import com.edu.result.Result;
import com.edu.vo.*;
import java.util.List;
public interface StudentService {
/**
* 根据学生id显示请假列表
* @param studentId
* @return
*/
List<LeaveApplicationsVo> listLeaveApplications(String studentId);
/**
* 根据学生id显示迟到列表
* @param studentId
* @return
*/
List<LateAttendanceVo> listLateAttendance(String studentId);
/**
* 根据学生id显示旷课列表
* @param studentId
* @return
*/
List<AbsenceRecordVo> listAbsenceRecord(String studentId);
/**
* 请假列表详情
* @param studentId
* @return
*/
List<LeaveApplicationsdetailVo> LeaveApplicationsdetail(String studentId);
/**
* 根据学生id查询该学生各科的缺勤次数
* @param studentId
* @return
*/
List<CourseAbsenceCountVo> viewAbsenceCountsForSubjects(String studentId);
/**
* 根据学生id查询学生信息
* @param studentId
* @return
*/
List<StudentDetailVO> viewStudentDetail(String studentId);
/**
* 申请请假
* @param studentId 学生id
* @param startTime 开始时间
* @param endTime 结束时间
* @param reason 原因
* @return
*/
Result insertLeaveRequest(String studentId, String startTime, String endTime, String reason);
}
5、编写service层逻辑
package com.edu.service.Impl;
import com.edu.mapper.StudentMapper;
import com.edu.result.Result;
import com.edu.service.StudentService;
import com.edu.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
/**
* 根据学生id显示请假列表
* @param studentId
* @return
*/
@Override
public List<LeaveApplicationsVo> listLeaveApplications(String studentId){
return studentMapper.listLeaveApplications(studentId);
}
/**
* 根据学生id显示迟到列表
* @param studentId
* @return
*/
@Override
public List<LateAttendanceVo> listLateAttendance(String studentId){
return studentMapper.listLateAttendance(studentId);
}
/**
* 根据学生id显示旷课列表
* @param studentId
* @return
*/
@Override
public List<AbsenceRecordVo> listAbsenceRecord(String studentId){
return studentMapper.listAbsenceRecord(studentId);
}
/**
* 请假列表详情
* @param studentId
* @return
*/
@Override
public List<LeaveApplicationsdetailVo> LeaveApplicationsdetail(String studentId){
return studentMapper.LeaveApplicationsdetail(studentId);
}
/**
* 根据学生id查询该学生各科的缺勤次数
* @param studentId
* @return
*/
@Override
public List<CourseAbsenceCountVo> viewAbsenceCountsForSubjects(String studentId){
return studentMapper.viewAbsenceCountsForSubjects(studentId);
}
/**
* 根据学生id查询学生信息
* @param studentId
* @return
*/
@Override
public List<StudentDetailVO> viewStudentDetail(String studentId){
return studentMapper.viewStudentDetail(studentId);
}
/**
* 申请请假
* @param studentId 学生id
* @param startTime 开始时间
* @param endTime 结束时间
* @param reason 原因
* @return
*/
@Override
public Result insertLeaveRequest(String studentId, String startTime, String endTime, String reason){
try {
int deletedCount = studentMapper.insertLeaveRequest(studentId,startTime,endTime,reason);
if (deletedCount == 0) {
return Result.error("插入失败");
} else {
return Result.success("插入成功");
}
} catch (Exception e) {
// 记录异常或进行其他处理
return Result.error("插入时发生错误:" + e.getMessage());
}
}
}
6、编写控制层(controller)
package com.edu.controller;
import com.edu.result.Result;
import com.edu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/attendance/student")
@Slf4j
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 根据学生id显示请假列表
* @param studentId
* @return
*/
@GetMapping("/listLeaveApplications")
public Result listLeaveApplications(String studentId) {
return Result.success(studentService.listLeaveApplications(studentId));
}
/**
* 根据学生id显示迟到列表
* @param studentId
* @return
*/
@GetMapping("/listLateAttendance")
public Result listLateAttendance(String studentId) {
return Result.success(studentService.listLateAttendance(studentId));
}
/**
* 根据学生id显示旷课列表
* @param studentId
* @return
*/
@GetMapping("/listAbsenceRecord")
public Result listAbsenceRecord(String studentId) {
return Result.success(studentService.listAbsenceRecord(studentId));
}
/**
* 请假列表详情
* @param studentId
* @return
*/
@GetMapping("/viewLeaveApplications")
public Result LeaveApplicationsdetail(String studentId) {
return Result.success(studentService.LeaveApplicationsdetail(studentId));
}
/**
* 根据学生id查询该学生各科的缺勤次数
* @param studentId
* @return
*/
@GetMapping("/viewAbsenceCountsForSubjects")
public Result viewAbsenceCountsForSubjects(String studentId) {
return Result.success(studentService.viewAbsenceCountsForSubjects(studentId));
}
/**
* 根据学生id查询学生信息
* @param studentId
* @return
*/
@GetMapping("/viewStudentDetail")
public Result viewStudentDetail(String studentId) {
return Result.success(studentService.viewStudentDetail(studentId));
}
/**
* 申请请假
* @param studentId 学生id
* @param startTime 开始时间
* @param endTime 结束时间
* @param reason 原因
* @return
*/
@PostMapping("/insertLeaveRequest")
public Result insertLeaveRequest(String studentId, String startTime, String endTime, String reason) {
return Result.success(studentService.insertLeaveRequest(studentId,startTime,endTime,reason));
}
}