JdbcTemplate queryForInt () устарел

JdbcTemplate queryForInt () не рекомендуется

Вы обновляете версию Spring и заметили, чтоqueryForInt() устарел, что следует заменить?

  private boolean isUserExists(String username) {

        String sql = "SELECT count(*) FROM USERS WHERE username = ?";
        boolean result = false;

        //The method queryForInt(String, Object...) from the type JdbcTemplate is deprecated
        int count = getJdbcTemplate().queryForInt(sql, new Object[] { username });

    if (count > 0) {
        result = true;
    }

    return result;
  }

Решение

ИqueryForInt(), иqueryForLong() устарели с версии 3.2.2 (исправьте меня, если ошибка). Чтобы исправить это, замените код наqueryForObject(String, Class).

  private boolean isUserExists(String username) {

        String sql = "SELECT count(*) FROM USERS WHERE username = ?";
    boolean result = false;

    int count = getJdbcTemplate().queryForObject(
                        sql, new Object[] { username }, Integer.class);

    if (count > 0) {
        result = true;
    }

    return result;
  }

Просмотрите исходный код Spring.

JdbcTemplate.java

package org.springframework.jdbc.core;

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {

  //...
  @Deprecated
  public long queryForLong(String sql, Object... args) throws DataAccessException {
    Number number = queryForObject(sql, args, Long.class);
    return (number != null ? number.longValue() : 0);
  }

  @Deprecated
  public int queryForInt(String sql, Object... args) throws DataAccessException {
    Number number = queryForObject(sql, args, Integer.class);
    return (number != null ? number.intValue() : 0);
  }

Рекомендации