added static newInstance method to BeanPropertyRowMapper (SPR-6433); aligned ParameterizedBeanPropertyRowMapper factory methods

This commit is contained in:
Juergen Hoeller 2009-11-25 00:08:57 +00:00
parent fce0361052
commit 9a2f9ccde4
2 changed files with 29 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@ -100,6 +100,8 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
/**
* Create a new 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
*/
public BeanPropertyRowMapper(Class<T> mappedClass) {
@ -204,21 +206,22 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
}
/**
* Set whether we're defaulting Java primitives in the case of mapping a null value from corresponding
* database fields.
* Set whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
* <p>Default is <code>false</code>, throwing an exception when nulls are mapped to Java primitives.
*/
public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) {
this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue;
}
/**
* Return whether we're defaulting Java primitives in the case of mapping a null value
* from corresponding database fields.
*/
public boolean isPrimitivesDefaultedForNullValue() {
return primitivesDefaultedForNullValue;
}
/**
* Return whether we're defaulting Java primitives in the case of mapping a null value from corresponding
* database fields.
*/
public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) {
this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue;
}
/**
* Extract the values for all columns in the current row.
@ -305,4 +308,16 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
}
/**
* Static factory method to create a new 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;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@ -35,9 +35,9 @@ import org.springframework.jdbc.core.BeanPropertyRowMapper;
* String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long,
* float, Float, double, Double, BigDecimal, <code>java.util.Date</code>, etc.
*
* <p>The mapper can be configured to use the primitives default value when mapping null values by
* passing in 'true' for the 'primitivesDefaultedForNullValue' using the {@link #newInstance(Class, boolean)} method.
* Also see {@link BeanPropertyRowMapper#setPrimitivesDefaultedForNullValue(boolean)}
* <p>The mapper can be configured to use the primitives default value when mapping null values
* by setting the {@link #setPrimitivesDefaultedForNullValue 'primitivesDefaultedForNullValue'}
* flag to 'true'.
*
* <p>To facilitate mapping between columns and fields that don't have matching names,
* try using column aliases in the SQL statement like "select fname as first_name from customer".
@ -59,19 +59,8 @@ public class ParameterizedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper
* @param mappedClass the class that each row should be mapped to
*/
public static <T> ParameterizedBeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
return newInstance(mappedClass, false);
}
/**
* Static factory method to create a new ParameterizedBeanPropertyRowMapper
* (with the mapped class specified only once).
* @param mappedClass the class that each row should be mapped to
* @param primitivesDefaultedForNullValue whether we're defaulting primitives when mapping a null value
*/
public static <T> ParameterizedBeanPropertyRowMapper<T> newInstance(Class<T> mappedClass, boolean primitivesDefaultedForNullValue) {
ParameterizedBeanPropertyRowMapper<T> newInstance = new ParameterizedBeanPropertyRowMapper<T>();
newInstance.setMappedClass(mappedClass);
newInstance.setPrimitivesDefaultedForNullValue(primitivesDefaultedForNullValue);
return newInstance;
}