JdbcTemplate “queryForObject” 和 “query” 在春季被弃用。它应该被什么取代?

2022-09-03 04:12:22

查询对象,

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

对于查询,

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

两者都已在上述的 Spring boot 2.4.X 中弃用jdbcTemplate.queryForObjectjdbcTemplate.query


答案 1

正如该类的javadoc以及升级/迁移指南中所解释的那样,它解释了您应该使用varargs方法

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

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

答案 2

您只需更改数组 Object[ ] 和映射器的顺序即可。当前版本支持此语法。所以你的代码是:

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

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

您可以在此处查看此方法的文档。


推荐