org.springframework.jdbc.core.BeanPropertyRowMapperCustom Maven / Gradle / Ivy
package org.springframework.jdbc.core;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.jdbc.support.JdbcUtils;
public class BeanPropertyRowMapperCustom extends BeanPropertyRowMapper {
public BeanPropertyRowMapperCustom() {
super();
}
public BeanPropertyRowMapperCustom(Class mappedClass) {
super(mappedClass);
}
public BeanPropertyRowMapperCustom(Class mappedClass, boolean checkFullyPopulated) {
super(mappedClass, checkFullyPopulated);
}
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T mappedObject = BeanUtils.instantiate(getMappedClass());
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
beanWrapper.setAutoGrowNestedPaths(true);
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int index = 1; index <= columnCount; index++) {
try {
String column = JdbcUtils.lookupColumnName(metaData, index);
Object value = JdbcUtils.getResultSetValue(rs, index, Class.forName(metaData.getColumnClassName(index)));
beanWrapper.setPropertyValue(column, value);
}
catch (TypeMismatchException | NotWritablePropertyException | ClassNotFoundException e) {
// Ignore
}
}
return mappedObject;
}
}