JdbcTemplate queryForInt / Long은 Spring 3.2.2에서 더 이상 사용되지 않습니다. 무엇으로 교체해야합니까?
JdbcTemplate의 queryforInt / queryforLong 메소드는 Spring 3.2에서 더 이상 사용되지 않습니다. 이러한 방법을 사용하여 기존 코드를 대체하는 가장 좋은 방법이 무엇인지 이유를 알 수 없습니다.
일반적인 방법 :
int rowCount = jscoreJdbcTemplate.queryForInt(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
);
위의 방법을 다음과 같이 다시 작성해야합니다.
Object[] params = new Object[] {
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
};
int rowCount = jscoreJdbcTemplate.queryForObject(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
params, Integer.class);
분명히이 폐기는 JdbcTemplate 클래스를 더 단순하게 만듭니다 (또는 그렇게합니까?). QueryForInt는 항상 편리한 방법이었고 (내 생각에) 오랫동안 사용되었습니다. 제거 된 이유. 결과적으로 코드가 더 복잡해집니다.
내가 생각하는 것은 누군가 queryForInt / Long 메소드가 혼란스러운 의미를 가지고 있다는 것을 깨달았다는 것입니다. 즉, JdbcTemplate 소스 코드에서 현재 구현을 볼 수 있습니다.
@Deprecated
public int queryForInt(String sql, Object... args) throws DataAccessException {
Number number = queryForObject(sql, args, Integer.class);
return (number != null ? number.intValue() : 0);
}
결과 집합이 비어 있으면 0을 반환하지만 예외가 발생한다고 생각할 수 있습니다.
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
따라서 다음 구현은 본질적으로 현재 구현과 동일합니다.
@Deprecated
public int queryForInt(String sql, Object... args) throws DataAccessException {
return queryForObject(sql, args, Integer.class);
}
그리고 이제 더 이상 사용되지 않는 코드를 추악한 코드로 바꿔야합니다.
queryForObject(sql, new Object { arg1, arg2, ...}, Integer.class);
또는이 (더 좋은) :
queryForObject(sql, Integer.class, arg1, arg2, ...);
편의 메소드 queryForLong (sql)을 사용하지 않는 것이 불편하다는 원래 포스터에 동의합니다.
저는 Spring 3.1을 사용하여 앱을 개발했고 방금 최신 Spring 버전 (3.2.3)으로 업데이트했으며 더 이상 사용되지 않는 것으로 나타났습니다.
다행히도 한 줄 변경되었습니다.
return jdbcTemplate.queryForLong(sql); // deprecated in Spring 3.2.x
변경되었습니다
return jdbcTemplate.queryForObject(sql, Long.class);
그리고 두 개의 단위 테스트는 위의 변경 사항이 작동 함을 나타내는 것 같습니다.
대신 사용되지 않습니다 queryForObject(String, Class)
.
이러한 코드 교체 :
long num = jdbcTemplate.queryForLong(sql);
이 코드로 :
long num = jdbcTemplate.queryForObject(sql, Long.class);
is very dangerous because if column have null value queryForObject return null and as we know primitive types can't be null and You will have NullPointerException. The compiler didn't warn You about this. You will know about this error at runtime. The same error You will have if You have method that return primitive type:
public long getValue(String sql) {
return = jdbcTemplate.queryForObject(sql, Long.class);
}
The deprecated method queryForLong in JdbcTemplate in Spring 3.2.2 have the following body:
@Deprecated
public long queryForLong(String sql) throws DataAccessException {
Number number = queryForObject(sql, Long.class);
return (number != null ? number.longValue() : 0);
}
You see before they return primitive value there is check that this is not null and if it is null they return 0. By the way - Should be 0L.
JdbcTemplate#queryForInt
returns 0 if the column value is SQL NULL or 0. There is no way to distinguish one case from the other. I think this is the main reason why the method is deprecated. BTW, ResultSet#getInt
behaves similarly. Though, we can distinguish between these two cases by ResultSet#wasNull
.
public int getCircleCount() {
Object param = "1";
String sql = "select count(*) from circle where id = ? ";
jdbcTemplate.setDataSource(getDataSource());
int result = getJdbcTemplate().queryForObject(sql, new Object[] { param }, Integer.class);
return result;
}
'IT박스' 카테고리의 다른 글
다른 git repo 안에 git repo 유지 (0) | 2020.08.18 |
---|---|
구조체를 0으로 초기화 (0) | 2020.08.18 |
SQL Server에서 Active Directory 사용자 그룹을 로그인으로 추가하는 방법 (0) | 2020.08.18 |
Neo4j에서 데이터베이스를 삭제 / 생성하는 방법은 무엇입니까? (0) | 2020.08.17 |
Swift에서 x 분마다 작업 수행 (0) | 2020.08.17 |