How to Replace Deprecated jdbcTemplate.queryForObject and jdbcTemplate.query in Spring Boot 2.4.X and above – 如何在 Spring Boot 2.4.X 及以上版本中替换已废弃的 jdbcTemplate.queryForObject 和 jdbcTemplate.query

最后修改: 2023年 9月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll discuss the deprecated queryForObject() and query() methods of JdbcTemplate and their preferred counterparts.

在本教程中,我们将讨论 JdbcTemplate 中已废弃的 queryForObject()query() 方法及其首选对应方法。

The deprecated methods accept parameters passed within an array of objects, whereas the new methods utilize Varargs for passing parameters. This approach is more intuitive because Varargs are designed specifically for efficiently passing a variable number of arguments to a method.

已废弃的方法接受在对象数组中传递的参数,而新方法则使用 Varargs 来传递参数。这种方法更为直观,因为 Varargs 是专为向方法有效传递可变数量的参数而设计的。

Throughout our tutorial, we’ll explain all the methods by running queries on a student table in the in-memory H2 database.

在整个教程中,我们将通过在内存H2 数据库中的学生表上运行查询来讲解所有方法。

2. Database Setup

2.数据库设置

Before we look at the methods in question, let’s first examine the student table, which we’ll use in all our examples:

在了解相关方法之前,让我们先看看 student 表,我们将在所有示例中使用该表:

CREATE TABLE student (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    student_name VARCHAR(255) NOT NULL,
    age INT,
    grade INT NOT NULL,
    gender VARCHAR(10) NOT NULL,
    state VARCHAR(100) NOT NULL
);
-- Student 1
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('John Smith', 18, 3, 'Male', 'California');

-- Student 2
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emily Johnson', 17, 2, 'Female', 'New York');

--More insert statements...

3. Deprecated Method query()

3.已废弃的方法 query()

In JdbcTemplate, there are three query() method variations, each using a different functional interface to collect or process the rows from the query result. These interfaces are ResultSetExtractor, RowCallbackHandler, and RowMapper.

JdbcTemplate 中, 有三个 query() 方法变体,每个变体使用不同的功能接口来收集或处理查询结果中的记录。这些接口是 ResultSetExtractor, RowCallbackHandler, 和 RowMapper.

Let’s see each of them and their suggested replacements.

让我们逐一看看它们和建议的替代品。

3.1. query() Method with RowMapper

3.1.使用 RowMapperquery() 方法

The query() method in the JdbcTemplate class returns a list representing rows returned by the DB query. Let’s begin with an example where we’ll query the student table to get students of a certain age and gender:

类中的query()方法返回一个列表,该列表代表通过 DB 查询返回的记录。让我们从查询 student 表以获取具有特定年龄和性别的学生的示例开始:</span

public List<Student> getStudentsOfAgeAndGender(Integer age, String gender) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ?";
    Object[] args = {age, gender};
    return jdbcTemplate.query(sql, args, new StudentRowMapper());
}

In the above method, the variable args of type Object[] stores the query parameters. Even if it is a single query parameter, it needs to be added to an array, which is inconvenient.

在上述方法中,Object[] 类型的变量 args 存储了查询参数。即使是单个查询参数,也需要添加到数组中,这很不方便。

Suppose we need additional filter criteria to get students of a certain grade. Then, we have to write a new method for it:

假设我们需要额外的筛选条件来获取某个年级的学生。那么,我们必须为此编写一个新方法:

public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ? and grade = ?";
    Object[] args = {age, gender, grade};
    return jdbcTemplate.query(sql, args, new StudentRowMapper());
}

So, to implement this, we modified sql and args variables. But, can we eliminate the boiler-plate code involving the args variable? Let’s explore this further in the following method:

因此,为了实现这一点,我们修改了 sqlargs 变量。但是,我们能否消除涉及 args 变量的模板代码呢?让我们在下面的方法中进一步探讨:

public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, String grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ? and grade = ?";
    return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender, grade);
}

The varargs variation of the jdbcTemplate.query did exactly what we wanted.

jdbcTemplate.queryvarargs变化正是我们想要的。

Let’s see the deprecated and the replacement methods in action:

让我们看看废弃方法和替换方法的实际应用:

@Test
public void givenDeprecatedMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
    List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
    }
    assertEquals(5, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
    List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);

    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
    }
    assertEquals(5, students.size());
}

As we can see above, the two test methods invoke getStudentsOfAgeAndGender() of the Dao classes, StudentDaoWithDeprecatedJdbcTemplateMethods and StudentDaoWithPreferredJdbcTemplateMethods. Following is the output of both methods:

如上所示,这两个测试方法调用了 Dao 类 StudentDaoWithDeprecatedJdbcTemplateMethodsStudentDaoWithPreferredJdbcTemplateMethods 中的 getStudentsOfAgeAndGender() 方法。以下是这两种方法的输出结果:

Student Name: Olivia Garcia Student gender: Female Student grade: 2
Student Name: Ava Davis Student gender: Female Student grade: 2
Student Name: Olivia Johnson Student gender: Female Student grade: 2
Student Name: Isabella Davis Student gender: Female Student grade: 2
Student Name: Sophia Hernandez Student gender: Female Student grade: 2

No doubt, the varargs version serves its intended purpose.

毫无疑问,varargs 版本达到了预期目的。

In the upcoming sections, we’ll mostly discuss the usage of the deprecated and preferred methods. The benefits of the varargs version would be the same as discussed in this section. Hence, we won’t be repeating that.

在接下来的章节中,我们将主要讨论弃用方法和首选方法的用法vargs 版本的优点与本节讨论的相同。因此,我们不再赘述。

3.2. query() Method with ResultSetExtractor

3.2.使用 ResultSetExtractorquery() 方法

Now, let’s move on to the next deprecated version that uses the ResultSetExtractor interface. The method extractData() in the interface gets invoked once so that all the rows can be returned in a data structure after processing. For this example, we have created a StudentResultExtractor:

现在,让我们继续下一个 废弃版本,它使用 ResultSetExtractor 接口接口中的方法 extractData() 会被调用一次,以便在处理后以数据结构形式返回所有行。在本例中,我们创建了一个 StudentResultExtractor

public class StudentResultExtractor implements ResultSetExtractor<List<Student>> {
    @Override
    public List<Student> extractData(ResultSet rs) throws SQLException {
        List<Student> students = new ArrayList<Student>();
        while(rs.next()) {
            Student student = new Student();
            student.setStudentId(rs.getInt("student_id"));
            student.setStudentName(rs.getString("student_name"));
            student.setAge(rs.getInt("age"));
            student.setStudentGender(rs.getString("gender"));
            student.setGrade(rs.getInt("grade"));
            student.setState(rs.getString("state"));
            students.add(student);
        }
        return students;
    }
}

It returns a List of Student.

它返回一个 StudentList

Let’s take a look at the query method using the array of objects to pass query parameters, and StudentResultExtractor mentioned above:

让我们看看使用对象数组传递查询参数的查询方法,以及上文提到的StudentResultExtractor:

public List<Student> getStudentsOfGradeAndState(Integer grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    Object[] args = {grade, state};
    return jdbcTemplate.query(sql, args, new StudentResultExtractor());
}

The above method queries the student table to retrieve students studying in a specific grade and belonging to a particular state.

上述方法查询 student 表,以检索就读于特定年级并属于特定州的学生。

Similar to the earlier section, we’ll implement the same with the varargs version of the query method:

与前一节类似,我们将使用 varargs 版本的查询方法来实现相同的功能:

public List<Student> getStudentsOfGradeAndState(Integer grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    return jdbcTemplate.query(sql, new StudentResultExtractor(), grade, state);
}

Finally, we invoke the above two methods in separate test functions:

最后,我们在不同的测试函数中调用上述两种方法:

@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
    List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName()
          + " Student grade: " + student.getStudentGender()
          + " Student State: " + student.getState());
    }
    assertEquals(6, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
    List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName()
          + " Student grade: " + student.getStudentGender()
          + " Student State: " + student.getState());
    }
    assertEquals(6, students.size());
}

As expected, both the tests give the same output:

不出所料,这两个测试都给出了相同的结果:

Student Name: Ethan Rodriguez Student grade: Male Student State: New York
Student Name: Benjamin Brown Student grade: Male Student State: New York
Student Name: Matthew Martinez Student grade: Male Student State: New York
Student Name: Christopher Lee Student grade: Male Student State: New York
Student Name: Liam Johnson Student grade: Male Student State: New York
Student Name: Mason Smith Student grade: Male Student State: New York

3.3. query() Method with RowCallbackHandler

3.3.带有 RowCallbackHandlerquery() 方法

Lastly, in this section, we’ll discuss the version using the interface RowCallbackHandler. For this example, we would use RowCountCallbackHandler, which is a subclass of RowCallbackHandler.

最后,在本节中,我们将讨论使用接口 RowCallbackHandler 的版本。在这个示例中,我们将使用 RowCountCallbackHandler ,它是 RowCallbackHandler 的子类。

Let’s start by taking a look at the deprecated version:

让我们先来看看 过时版本

public Integer getCountOfStudentsInAGradeFromAState(String grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    Object[] args = {grade, state};
    RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler();
    jdbcTemplate.query(sql, args, countCallbackHandler);
    return countCallbackHandler.getRowCount();
}

The method, true to its name, gets the total number of students studying in a given grade and belonging to a particular state.

该方法顾名思义,就是获得某个年级和某个州的在校学生总数。

Similarly, here is the method using the preferred version of the query method:

同样,以下是使用 首选版本的 query 方法的方法:

public Integer getCountOfStudentsInAGradeFromAState(String grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";

    RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler();
    jdbcTemplate.query(sql, countCallbackHandler, grade, state);
    return countCallbackHandler.getRowCount();
}

Let’s see how these methods can be invoked:

让我们看看如何调用这些方法:

@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnCount() {
    Integer count = studentDaoWithDeprecatedJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
    logger.info("Total students of grade 1 from New York:" + count);
    assertEquals(6, count);
}

@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
    logger.info("Total students of grade 1 from New York:" + count);
    assertEquals(6, count);
}

As shown below, unsurprisingly, they yield identical results:

如下图所示,不出所料,它们的结果完全相同:

Total students of grade 1 from New York: 6

4. Deprecated Method queryForObject()

4.已废弃的方法 queryForObject()

The method queryForObject() also has two deprecated variants that use the Object[] type for passing the query parameters. These are queryForObject(String sql, Object[] args, Class<T> requiredType) and queryForObject(String sql, Object[] args, RowMapper<T> rowMapper).

方法 queryForObject() 还有两个已废弃的变体,它们使用 Object[] 类型来传递查询参数。它们是 queryForObject(String sql, Object[] args, Class<T> requiredType)queryForObject(String sql, Object[] args, RowMapper<T> rowMapper)

Like before, we’ll explore their suggested replacements using Varargs in the upcoming sections.

与之前一样,我们将在接下来的章节中探讨使用 Varargs 替换它们的建议。

4.1. queryForObject() Method with RowMapper

4.1.使用 RowMapperqueryForObject() 方法

Unlike the query() method, queryForObject() is meant for handling a DB query result with a single row. Along the same lines, let’s first check out the following method that uses the deprecated version of queryForObject():

query()方法不同,queryForObject()用于处理单行的数据库查询结果。按照同样的思路,让我们先看看以下使用 queryForObject() 过时版本的方法:

public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where student_id = ? and grade = ?";
    Object[] args = {studentID, grade};

    return jdbcTemplate.queryForObject(sql, args, new StudentRowMapper());
}

The above method returns the student from a grade having a specific student ID. But just like the deprecated version of the query() method, it also needs to declare a variable of type Object[] for the query parameters.

上述方法从具有特定学生 ID 的成绩中返回学生。但就像 过时版本的 query() 方法一样,它也需要为查询参数声明一个 Object[] 类型的变量。

Since the above promotes boiler-plate code, let’s see a cleaner approach:

既然上述代码都是模板式的,那我们就来看看更简洁的方法:

public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where student_id = ? and grade = ?";

    return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), studentID, grade);
}

However, here, there is no need to declare a variable of type Object[]. The varargs variant of queryForObject() directly takes the studentID and grade arguments as the last parameters.

但是,这里不需要声明 Object[] 类型的变量。queryForObject()varargs 变体直接将 studentIDgrade 参数作为最后一个参数。

Now, let’s look at how the methods are used:https://www.baeldung.com/wp-admin/post.php?post=165771&action=edit

现在,让我们看看这些方法是如何使用的:https://www.baeldung.com/wp-admin/post.php?post=165771&action=edit

@Test
public void givenDeprecatedMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
    Student student = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
    assertEquals(1, student.getGrade());
    assertEquals(4, student.getStudentId());
    logger.info("Student ID: " + student.getStudentId()
      + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}

@Test
public void givenPreferredMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
    Student student = studentDaoWithPreferredJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
    assertEquals(1, student.getGrade());
    assertEquals(4, student.getStudentId());
    logger.info("Student ID: " + student.getStudentId()
      + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}

As expected, both methods give the same output:

不出所料,两种方法的输出结果相同:

Student ID: 4 Student Name: Sophia Martinez Student grade: 1

4.2. queryForObject() With Class<T>

4.2.queryForObject() 使用 Class<T>

Let’s begin by taking a look at the example using the deprecated method queryForObject(String sql, Object[] args, Class<T> requiredType):

让我们先看看使用已废弃方法 queryForObject(String sql, Object[] args, Class<T> requiredType) 的示例:

public Integer getCountOfGenderInAGrade(String gender, Integer grade) {
    String sql = "select count(1) as total from student where gender = ? and grade = ?";
    Object[] args = {gender, grade};

    return jdbcTemplate.queryForObject(sql, args, Integer.class);
}

The above method returns the total number of students of a certain gender studying in a given grade.

上述方法返回在某年级就读的某性别学生总数。

Let’s see the approach with the preferred method queryForObject(String sql, Class<T> requiredType, Object… args):

让我们看看首选方法 queryForObject(String sql, Class<T> requiredType, Object… args)

public Integer getCountOfGenderInAGrade(String gender, Integer grade) {
    String sql = "select count(1) as total from student where gender = ? and grade = ?";

    return jdbcTemplate.queryForObject(sql, Integer.class, gender, grade);
}

As usual, in the above method, we’re able to get rid of the variable args of type Object[].

与往常一样,在上述方法中,我们可以删除 Object[] 类型的变量 args

Now, let’s go through the following methods showing getCountOfGenderInAGrade() in action:

现在,让我们通过以下方法来展示 getCountOfGenderInAGrade() 的操作:

@Test
public void givenPreferredMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
    assertEquals(6, count);
    logger.info("Total number of Female Students: " + count);
}

@Test
public void givenDeprecatedMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
    assertEquals(6, count);
    logger.info("Total number of Female Students: " + count);
}

Finally, as shown below, the replacement method successfully manages to get a similar result:

最后,如下图所示,替换法成功地得到了类似的结果:

Total number of Female Students: 6

5. Conclusion

5.结论

In this tutorial, we explored the preferred replacements of the deprecated variants of query() and queryForObject() methods of the JdbcTemplate class. With examples, we explained the new methods that use Varargs to take the query parameters as arguments. We also saw how it helps eliminate the boilerplate code for getting the parameters in an array of objects.

在本教程中,我们探讨了替代 JdbcTemplate 类中 query()queryForObject() 方法的首选方法。通过示例,我们解释了使用 Varargs 将查询参数作为参数的新方法。我们还看到了它如何帮助消除在对象数组中获取参数的模板代码。

As usual, the examples can be found over on GitHub.

和往常一样,您可以在 GitHub 上找到这些示例