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

问题描述

我一直在关注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);
}

StudentService ...

@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));  
    }

StudentDataAccessService ...

@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);

解决方法

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