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

17,185

Solution 1

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

Solution 2

You can just change the order of the array Object[ ] and the mapper. This syntax is supported in the current version. So your code would be:

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

and

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

You can see the doc of this method here.

Share:
17,185
Thirumal
Author by

Thirumal

Github: M-THIRUMAL

Updated on June 15, 2022

Comments

  • Thirumal
    Thirumal almost 2 years

    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