如何在SpringMVC中调用api并从更新方法中选择正确的可选方法

如何在SpringMVC中调用api并从更新方法中选择正确的可选方法,api,spring-mvc,controller,updates,optional,Api,Spring Mvc,Controller,Updates,Optional,我一直在关注MVC的在线教程,但在更新时遇到了障碍 在控制器中使用了updateStudent()方法并传入id,但在updateStudent()中我有很多选项 试图找出如何调用api并从我希望它使用的方法中选择可选的方法 谢谢你的帮助 谢谢 控制器 public class StudentController { public final StudentService studentService; @Autowired public StudentController(Stud

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

@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;
}
学生道

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

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

这是否回答了您的问题?