Supports ConversionService on SingleColumnRowMapper
Issue: SPR-16483
This commit is contained in:
committed by
Juergen Hoeller
parent
711b0f50f2
commit
8c623c8a42
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -20,6 +20,8 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.dao.TypeMismatchDataAccessException;
|
||||
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
|
||||
import org.springframework.jdbc.support.JdbcUtils;
|
||||
@@ -37,6 +39,7 @@ import org.springframework.util.NumberUtils;
|
||||
* and converted into the specified target type.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.2
|
||||
* @see JdbcTemplate#queryForList(String, Class)
|
||||
* @see JdbcTemplate#queryForObject(String, Class)
|
||||
@@ -46,6 +49,8 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
@Nullable
|
||||
private Class<?> requiredType;
|
||||
|
||||
@Nullable
|
||||
private ConversionService conversionService = DefaultConversionService.getSharedInstance();
|
||||
|
||||
/**
|
||||
* Create a new {@code SingleColumnRowMapper} for bean-style configuration.
|
||||
@@ -74,6 +79,15 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link ConversionService} for converting a fetched value.
|
||||
* <p>Default is the {@link DefaultConversionService}.
|
||||
* @since 5.0.4
|
||||
* @see DefaultConversionService#getSharedInstance
|
||||
*/
|
||||
public void setConversionService(@Nullable ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a value for the single column in the current row.
|
||||
@@ -164,7 +178,8 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
* <p>If the required type is String, the value will simply get stringified
|
||||
* via {@code toString()}. In case of a Number, the value will be
|
||||
* converted into a Number, either through number conversion or through
|
||||
* String parsing (depending on the value type).
|
||||
* String parsing (depending on the value type). Otherwise, the value will
|
||||
* be converted to a required type using the {@link ConversionService}.
|
||||
* @param value the column value as extracted from {@code getColumnValue()}
|
||||
* (never {@code null})
|
||||
* @param requiredType the type that each result object is expected to match
|
||||
@@ -173,6 +188,7 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
* @see #getColumnValue(java.sql.ResultSet, int, Class)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
protected Object convertValueToRequiredType(Object value, Class<?> requiredType) {
|
||||
if (String.class == requiredType) {
|
||||
return value.toString();
|
||||
@@ -187,6 +203,9 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
return NumberUtils.parseNumber(value.toString(),(Class<Number>) requiredType);
|
||||
}
|
||||
}
|
||||
else if (this.conversionService != null && this.conversionService.canConvert(value.getClass(), requiredType)) {
|
||||
return this.conversionService.convert(value, requiredType);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"Value [" + value + "] is of type [" + value.getClass().getName() +
|
||||
@@ -205,4 +224,17 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
|
||||
return new SingleColumnRowMapper<>(requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param conversionService the {@link ConversionService} for converting a fetched value
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType, @Nullable ConversionService conversionService) {
|
||||
SingleColumnRowMapper<T> rowMapper = newInstance(requiredType);
|
||||
rowMapper.setConversionService(conversionService);
|
||||
return rowMapper;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user