
org.smallframework.spring.SormBeanPropertyRowMapper Maven / Gradle / Ivy
Show all versions of sorm Show documentation
package org.smallframework.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import sf.database.jdbc.rowmapper.BeanRowMapper;
import sf.database.jdbc.rowmapper.RowMapperHelp;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* support jpa annotations.
* @param
*/
public class SormBeanPropertyRowMapper implements RowMapper {
/**
* Logger available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
/**
* The class we are mapping to.
*/
@Nullable
private Class mappedClass;
/**
* Create a new {@code SormBeanPropertyRowMapper} for bean-style configuration.
* @see #setMappedClass
*/
public SormBeanPropertyRowMapper() {
}
/**
* Create a new {@code SormBeanPropertyRowMapper}, accepting unpopulated
* properties in the target bean.
* 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 SormBeanPropertyRowMapper(Class mappedClass) {
initialize(mappedClass);
}
/**
* Set the class that each row should be mapped to.
*/
public void setMappedClass(Class mappedClass) {
if (this.mappedClass == null) {
initialize(mappedClass);
} else {
if (this.mappedClass != mappedClass) {
throw new InvalidDataAccessApiUsageException("The mapped class can not be reassigned to map to " +
mappedClass + " since it is already providing mapping for " + this.mappedClass);
}
}
}
/**
* Get the class that we are mapping to.
*/
@Nullable
public final Class getMappedClass() {
return this.mappedClass;
}
/**
* Initialize the mapping meta-data for the given class.
* @param mappedClass the mapped class
*/
protected void initialize(Class mappedClass) {
this.mappedClass = mappedClass;
}
/**
* Extract the values for all columns in the current row.
* Utilizes public setters and result set meta-data.
* @see java.sql.ResultSetMetaData
*/
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
Assert.state(this.mappedClass != null, "Mapped class was not specified");
BeanRowMapper beanRowMapper = RowMapperHelp.getBeanRowMapper(this.mappedClass, null);
return beanRowMapper.handle(rs, rs.getMetaData(), rowNumber);
}
/**
* Static factory method to create a new {@code SormBeanPropertyRowMapper}
* (with the mapped class specified only once).
* @param mappedClass the class that each row should be mapped to
* @param
* @return
*/
public static SormBeanPropertyRowMapper newInstance(Class mappedClass) {
return new SormBeanPropertyRowMapper<>(mappedClass);
}
}