JdbcTemplate“queryForObject”和“query”在 Spring 中已弃用。应该用什么代替呢?

问题描述 投票:0回答:2

查询对象,

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

jdbcTemplate.queryForObject
jdbcTemplate.query
在上述Spring Boot 2.4.X中已弃用

java sql spring spring-boot jdbctemplate
2个回答
51
投票

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

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

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

20
投票

您应该更改输入变量和映射器的顺序。您还应该使用 varargs 而不是 Object[ ]。所以你的代码是:

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

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

您可以在[此处]查看此方法的文档(https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcOperations.html#queryForObject( java.lang.String,java.lang.Class,java.lang.Object...).

© www.soinside.com 2019 - 2024. All rights reserved.