基于SpringBoot+Vue球队训练信息管理系统设计与实现
由于篇幅所限,我将提供一个简化的SpringBoot后端服务的代码示例,用于创建一个训练信息的RESTful API。
// 导入SpringBoot相关依赖
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
// 定义一个TrainingInfo实体类
@Entity
public class TrainingInfo {
@Id
private Long id;
private String name;
private String location;
// 省略其他属性和getter/setter方法
}
// 定义一个用于数据传输的对象
public class TrainingInfoDTO {
private Long id;
private String name;
private String location;
// 省略构造函数、getter和setter方法
}
// 创建一个TrainingInfoController类
@RestController
@RequestMapping("/api/training-info")
public class TrainingInfoController {
// 使用Mock数据,实际项目中应连接数据库
private List<TrainingInfo> trainingInfoList = new ArrayList<>();
// 获取所有训练信息
@GetMapping
public ResponseEntity<List<TrainingInfoDTO>> getAllTrainingInfo() {
return ResponseEntity.ok(trainingInfoList.stream()
.map(this::convertToDTO)
.collect(Collectors.toList()));
}
// 根据ID获取特定训练信息
@GetMapping("/{id}")
public ResponseEntity<TrainingInfoDTO> getTrainingInfoById(@PathVariable Long id) {
return trainingInfoList.stream()
.filter(ti -> ti.getId().equals(id))
.map(this::convertToDTO)
.findFirst()
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
// 创建新的训练信息
@PostMapping
public ResponseEntity<TrainingInfoDTO> createTrainingInfo(@RequestBody TrainingInfoDTO trainingInfoDTO) {
TrainingInfo trainingInfo = convertToEntity(trainingInfoDTO);
trainingInfoList.add(trainingInfo);
return ResponseEntity.ok(convertToDTO(trainingInfo));
}
// 更新训练信息
@PutMapping("/{id}")
public ResponseEntity<TrainingInfoDTO> updateTrainingInfo(@PathVariable Long id, @RequestBody TrainingInfoDTO trainingInfoDTO) {
return trainingInfoList.stream()
.filter(ti -> ti.getId().equals(id))
.map(ti -> {
ti.setName(trainingInfoDTO.getName());
ti.setLocation(trainingInfoDTO.getLocation());
return ti;
})
.map(this::convertToDTO)
.map(ResponseEntity::ok)
评论已关闭