package com.loc.pojo;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public abstract class BaseBean {
@SuppressWarnings("unchecked")
public static RowMapper mapRow(final Class clazz) throws Exception{
return new RowMapper(){
public Object mapRow(ResultSet resultset, int rowNum)
throws SQLException {
Field[] fields = clazz.getDeclaredFields();
Object o = null;
try {
o = clazz.newInstance();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
for(Field field:fields){
Method m;
try {
m = clazz.getMethod("set"+toUpp(field.getName()), new Class[]{field.getType()});
m.invoke(o, new Object[]{resultset.getObject(field.getName())});
} catch (Exception e) {
e.printStackTrace();
}
}
return o;
}
private String toUpp(String str) {
if (str != null && str != "") {
str = str.substring(0, 1).toUpperCase() + str.substring(1);
}
return str;
}
};
}
}