Search code examples
javasqlspringspring-bootjdbctemplate

JdbcTemplate "queryForObject" and "query" is deprecated in Spring. What should it be replaced by?


Query for object,

Student student = return jdbcTemplate.queryForObject("select * from student_id = ?", new Object[] { studentId }, studentRowMapper);

For query,

List<Student> students = return jdbcTemplate.query("select * from class_room_id = ?", new Object[] { classRoomId }, studentRowMapper);

Both jdbcTemplate.queryForObject and jdbcTemplate.query are deprecated in spring boot 2.4.X above


Solution

  • As explained in the javadoc of the class as well as in the upgrade/migration guide it explains that you should use the varargs method.

    jdbcTemplate.queryForObject("select * from student_id = ?", studentRowMapper, studentId);
    

    and

    List<Student> students = return jdbcTemplate.query("select * from class_room_id = ?", studentRowMapper, classRoomId);