如何在 Spring MVC 中调用 API 并从更新方法中选择正确的选项

问题描述 投票:0回答:1

我一直在关注 MVC 的在线教程,但在更新时遇到了障碍。

在控制器中使用了

updateStudent()
方法并传入 id,但在
updateStudent()
内部我有很多选项。

尝试弄清楚如何调用 API 并从我希望它使用的方法中选择选项。

如有任何帮助,我们将不胜感激。

控制器....

public class StudentController {
    
public final StudentService studentService;

@Autowired
public StudentController(StudentService studentService) {
    this.studentService=studentService;
}

@PutMapping(path="/updateme/{id}")
public void updateStudent(@PathVariable ("id") UUID id,@RequestBody Student student) {
    studentService.updateStudent(id, student);
}

学生服务...

@Service
public class StudentService {

private final StudentDao studentDao;

//constructor
    public StudentService(@Qualifier("postgres3")StudentDao studentDao) {
        this.studentDao=studentDao;
    }
    
    @PutMapping
    public void updateStudent(UUID id, Student student) {
        
        Optional.ofNullable(student.getChapterProgress())
        .filter(cp -> !StringUtils.isEmpty(cp))
        .ifPresent(cp -> studentDao.updateChapterProgress(id, cp));
        
        Optional.ofNullable(student.getAvgTestScore())
        .filter(avg -> !StringUtils.isEmpty(avg))
        .ifPresent(avg -> studentDao.updateAvgTestScore(id, avg));

        Optional.ofNullable(student.getChap1Score())
        .filter(c1 -> !StringUtils.isEmpty(c1))
        .ifPresent(c1 -> studentDao.updateChap1Score(id, c1));
        
        Optional.ofNullable(student.getChap2Score())
        .filter(c2 -> !StringUtils.isEmpty(c2))
        .ifPresent(c2 -> studentDao.updateChap2Score(id, c2));
        
        Optional.ofNullable(student.getChap3Score())
        .filter(c3 -> !StringUtils.isEmpty(c3))
        .ifPresent(c3 -> studentDao.updateChap3Score(id, c3));
        
        Optional.ofNullable(student.getChap4Score())
        .filter(c4 -> !StringUtils.isEmpty(c4))
        .ifPresent(c4 -> studentDao.updateChap4Score(id, c4));
        
        Optional.ofNullable(student.getChap5Score())
        .filter(c5 -> !StringUtils.isEmpty(c5))
        .ifPresent(c5 -> studentDao.updateChap5Score(id, c5));
        
        Optional.ofNullable(student.getChap6Score())
        .filter(c6 -> !StringUtils.isEmpty(c6))
        .ifPresent(c6 -> studentDao.updateChap6Score(id, c6));  
    }

学生数据访问服务...

@Repository("postgres3")
public class StudentDataAccessService implements StudentDao  {

private JdbcTemplate jdbcTemplate;

@Autowired
public StudentDataAccessService(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate= jdbcTemplate;
}

@Override
public int updateChapterProgress(UUID id, Integer chapterprogress) {
    String sql = "UPDATE student SET chapterprogress = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chapterprogress, id);
}

@Override
public int updateAvgTestScore(UUID id, Double avg) {
    String sql = "UPDATE student SET avgtestscore = ? WHERE id = ?";
    return jdbcTemplate.update(sql, avg, id);

}

@Override
public int updateChap1Score(UUID id, Double chap1Score) {
    String sql = "UPDATE student SET chap1score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap1Score, id);
}

@Override
public int updateChap2Score(UUID id, Double chap2Score) {
    String sql = "UPDATE student SET chap2score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap2Score, id);
}

@Override
public int updateChap3Score(UUID id, Double chap3Score) {
    String sql = "UPDATE student SET chap3score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap3Score, id);
}

@Override
public int updateChap4Score(UUID id, Double chap4Score) {
    String sql = "UPDATE student SET chap4score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap4Score, id);
}

@Override
public int updateChap5Score(UUID id, Double chap5Score) {
    String sql = "UPDATE student SET chap5score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap5Score, id);
}

@Override
public int updateChap6Score(UUID id, Double chap6Score) {
    String sql = "UPDATE student SET chap6score = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chap6Score, id);
}

@Override
public int updateStudentById(UUID id, Student student) {
    return 0;
}

StudentDao...

public interface StudentDao {
    
int updateStudentById(UUID id,Student student);

int updateChapterProgress(UUID id, Integer chapterprogress);

int updateAvgTestScore(UUID id, Double avg);

int updateChap1Score(UUID id, Double chap1Score);

int updateChap2Score(UUID id, Double chap2Score);

int updateChap3Score(UUID id, Double chap3Score);

int updateChap4Score(UUID id, Double chap4Score);

int updateChap5Score(UUID id, Double chap5Score);

int updateChap6Score(UUID id, Double chap6Score);
spring-mvc controller option-type
1个回答
0
投票

最终将每个更新分配给控制器中自己的方法调用,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.