JdbcTemplate's queryForObject allows for specifying primitive types as well

Issue: SPR-13220
This commit is contained in:
Juergen Hoeller
2015-07-13 21:30:57 +02:00
parent 8fdbf4285b
commit e0329306df
3 changed files with 51 additions and 20 deletions

View File

@@ -92,7 +92,7 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
/**
* Create a new BeanPropertyRowMapper for bean-style configuration.
* Create a new {@code BeanPropertyRowMapper} for bean-style configuration.
* @see #setMappedClass
* @see #setCheckFullyPopulated
*/
@@ -100,8 +100,8 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
}
/**
* Create a new BeanPropertyRowMapper, accepting unpopulated properties
* in the target bean.
* Create a new {@code BeanPropertyRowMapper}, accepting unpopulated
* properties in the target bean.
* <p>Consider using the {@link #newInstance} factory method instead,
* which allows for specifying the mapped type once only.
* @param mappedClass the class that each row should be mapped to
@@ -111,7 +111,7 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
}
/**
* Create a new BeanPropertyRowMapper.
* Create a new {@code BeanPropertyRowMapper}.
* @param mappedClass the class that each row should be mapped to
* @param checkFullyPopulated whether we're strictly validating that
* all bean properties have been mapped from corresponding database fields
@@ -326,14 +326,12 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
/**
* Static factory method to create a new BeanPropertyRowMapper
* Static factory method to create a new {@code BeanPropertyRowMapper}
* (with the mapped class specified only once).
* @param mappedClass the class that each row should be mapped to
*/
public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
BeanPropertyRowMapper<T> newInstance = new BeanPropertyRowMapper<T>();
newInstance.setMappedClass(mappedClass);
return newInstance;
return new BeanPropertyRowMapper<T>(mappedClass);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.sql.SQLException;
import org.springframework.dao.TypeMismatchDataAccessException;
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.NumberUtils;
/**
@@ -41,31 +42,34 @@ import org.springframework.util.NumberUtils;
*/
public class SingleColumnRowMapper<T> implements RowMapper<T> {
private Class<T> requiredType;
private Class<?> requiredType;
/**
* Create a new SingleColumnRowMapper.
* Create a new {@code SingleColumnRowMapper} for bean-style configuration.
* @see #setRequiredType
*/
public SingleColumnRowMapper() {
}
/**
* Create a new SingleColumnRowMapper.
* Create a new {@code SingleColumnRowMapper}.
* <p>Consider using the {@link #newInstance} factory method instead,
* which allows for specifying the required type once only.
* @param requiredType the type that each result object is expected to match
*/
public SingleColumnRowMapper(Class<T> requiredType) {
this.requiredType = requiredType;
setRequiredType(requiredType);
}
/**
* Set the type that each result object is expected to match.
* <p>If not specified, the column value will be exposed as
* returned by the JDBC driver.
*/
public void setRequiredType(Class<T> requiredType) {
this.requiredType = requiredType;
this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
}
@@ -187,15 +191,13 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
/**
* Static factory method to create a new ParameterizedSingleColumnRowMapper
* Static factory method to create a new {@code SingleColumnRowMapper}
* (with the required type specified only once).
* @param requiredType the type that each result object is expected to match
* @since 4.1
*/
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType) {
SingleColumnRowMapper<T> newInstance = new SingleColumnRowMapper<T>();
newInstance.setRequiredType(requiredType);
return newInstance;
return new SingleColumnRowMapper<T>(requiredType);
}
}