parent
9b253e1524
commit
af9c2d7580
@ -0,0 +1,13 @@
|
||||
package com.example.attendance;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AttendanceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AttendanceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.example.attendance.controller;
|
||||
|
||||
import com.example.attendance.entity.Student;
|
||||
import com.example.attendance.service.StudentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/students")
|
||||
public class StudentController {
|
||||
|
||||
@Autowired
|
||||
private StudentService studentService;
|
||||
|
||||
@GetMapping("/")
|
||||
public ResponseEntity<List<Student>> getAllStudents() {
|
||||
List<Student> students = studentService.getAllStudents();
|
||||
return ResponseEntity.ok(students);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Student> getStudent(@PathVariable Long id) {
|
||||
Student student = studentService.getStudentById(id);
|
||||
if (student != null) {
|
||||
return ResponseEntity.ok(student);
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public ResponseEntity<Student> addStudent(@RequestBody Student student) {
|
||||
studentService.addStudent(student);
|
||||
return ResponseEntity.ok(student);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Student> updateStudentPoints(@PathVariable Long id, @RequestParam int points) {
|
||||
studentService.updateStudentPoints(id, points);
|
||||
Student updatedStudent = studentService.getStudentById(id);
|
||||
return ResponseEntity.ok(updatedStudent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传Excel文件并导入学生数据
|
||||
* @param file Excel文件
|
||||
* @return 导入结果
|
||||
*/
|
||||
@PostMapping("/import")
|
||||
public String importStudents(@RequestParam("file") MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
return "请上传有效的Excel文件";
|
||||
}
|
||||
|
||||
try {
|
||||
studentService.importStudents(file);
|
||||
return "学生数据导入成功";
|
||||
} catch (Exception e) {
|
||||
return "学生数据导入失败:" + e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.example.attendance.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StudentDTO {
|
||||
private Long id;
|
||||
private String name;
|
||||
private int currentPoints;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.attendance.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StudentExcelDTO {
|
||||
private String name;
|
||||
private String studentId;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.example.attendance.mapper;
|
||||
|
||||
import com.example.attendance.entity.Student;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface StudentMapper {
|
||||
|
||||
@Select("SELECT * FROM students WHERE id = #{id}")
|
||||
Student getStudentById(Long id);
|
||||
|
||||
@Select("SELECT * FROM students")
|
||||
List<Student> getAllStudents();
|
||||
|
||||
@Insert("INSERT INTO students(name, initialPoints, currentPoints) VALUES(#{name}, #{initialPoints}, #{currentPoints})")
|
||||
void insertStudent(Student student);
|
||||
|
||||
@Update("UPDATE students SET currentPoints = #{currentPoints} WHERE id = #{id}")
|
||||
void updateStudentPoints(@Param("id") Long id, @Param("currentPoints") int currentPoints);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.attendance.service;
|
||||
|
||||
import com.example.attendance.dto.StudentExcelDTO;
|
||||
import com.example.attendance.entity.Student;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StudentService {
|
||||
void importStudents(MultipartFile file);
|
||||
List<Student> getAllStudents();
|
||||
Student getStudentById(Long id);
|
||||
void addStudent(Student student);
|
||||
void updateStudentPoints(Long id, int points);
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.example.attendance.service.impl;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.example.attendance.dto.StudentExcelDTO;
|
||||
import com.example.attendance.entity.Student;
|
||||
import com.example.attendance.listener.StudentExcelListener;
|
||||
import com.example.attendance.mapper.StudentMapper;
|
||||
import com.example.attendance.service.StudentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class StudentServiceImpl implements StudentService {
|
||||
|
||||
@Autowired
|
||||
private StudentMapper studentMapper;
|
||||
|
||||
@Override
|
||||
public void importStudents(MultipartFile file) {
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
EasyExcel.read(inputStream, StudentExcelDTO.class, new StudentExcelListener(studentMapper))
|
||||
.sheet()
|
||||
.doRead();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("从Excel导入学生数据失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Student> getAllStudents() {
|
||||
return studentMapper.getAllStudents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Student getStudentById(Long id) {
|
||||
return studentMapper.getStudentById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStudent(Student student) {
|
||||
studentMapper.insertStudent(student);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStudentPoints(Long id, int points) {
|
||||
studentMapper.updateStudentPoints(id, points);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
spring.application.name=attendance
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/roll_call_system
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456789jk
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
mybatis.mapper-locations=classpath:mappers/*.xml
|
Loading…
Reference in new issue