继续上一篇的功能改造,小弟我发现每次都要根据自己定义的实体类去直接实现 PageTemplate类中private List ResultSetForList(ResultSet rs, RowMapper rm)方法中的RowMapper方法,实在是很麻烦,不但要定义实体类,每次对应不同的实体类,总是需要实现RowMapper,麻烦死了。自己根据反射机制的一部分写了一个从记录集映射实体类的小东西。下面就和大家分享一下。
1.DefaultMapper类
实现RowMapper 接口的基类
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.free.source.plusin.ResultSet4Bean;
public class DefaultMapper implements RowMapper {
private ResultSet4Bean rsb;
public DefaultMapper(String className)
{
rsb=new ResultSet4Bean(className);
}
public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
Object obj=new Object();
try {
obj=rsb.getBeanObject(arg0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
}
红色的部分就是关键了。自己写的封装注入对象的类。
2.ResultSet4Bean类
用于封装记录集映射实体类。
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import com.free.source.plusin.resultset.IResultSetWrapper;
import com.free.source.plusin.resultset.ResultSetWrapperImpl;
/**
* @author TonyDev
*
*/
public class ResultSet4Bean {
private IResultSetWrapper rsp;
private BeanWrapper bw;
private String className;
public ResultSet4Bean(String className) {
this.className = className;
init();
}
private void init() {
rsp = new ResultSetWrapperImpl();
}
public Object getBeanObject(ResultSet rs) throws InstantiationException,
IllegalAccessException, ClassNotFoundException,
IntrospectionException, SQLException, IllegalArgumentException,
InvocationTargetException {
bw = new BeanWrapper(this.className);
Map
if (hm.size() > 0) {
bw.setProperty(hm);
}
return bw.getObj();
}
}
红色的部分是自己封装的类,请继续往下看。
3.IResultSetWrapper接口
用于获取记录集中实体字段属性的接口
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
/**
* @author TonyDev
*
*/
public interface IResultSetWrapper {
public Map setWrapper(ResultSet rs) throws SQLException;
}
4.ResultSetWrapperImpl 类
用于实现IResultSetWrapper 接口
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* @author TonyDev
*
*/
public class ResultSetWrapperImpl implements IResultSetWrapper {
/*
* (non-Javadoc)
*
*/
public Map setWrapper(ResultSet rs) throws SQLException {
Map
if ( rs.getRow()!=0) {
ResultSetMetaData rsMetaData;
try {
rsMetaData = rs.getMetaData();
int columnCount = rsMetaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
String columnName = rsMetaData.getColumnName(i + 1);
setMap.put(columnName, rs.getObject(i + 1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return setMap;
}
}
5.BeanWrapper 类
用于封装注入实体属性的类。
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
* @author TonyDev
*
*/
public class BeanWrapper {
private BeanInfo bf;
private Object obj;
/**
* 默认构造子,用于生成新的对象
* @param className
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws IntrospectionException
*/
public BeanWrapper(String className) throws InstantiationException,
IllegalAccessException, ClassNotFoundException,
IntrospectionException {
obj = Class.forName(className).newInstance();
bf = Introspector.getBeanInfo(obj.getClass());
}
/**
* 将参数注入对象
* @param resultsetMap
* @throws IntrospectionException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public void setProperty(Map resultsetMap) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor[] propDescs = bf.getPropertyDescriptors();
for (int i = 0; i < propDescs.length; i++) {
if (!propDescs[i].getName().equals("class")) {
String propName = propDescs[i].getName();
Object mapObj = resultsetMap.get(propName);
this.methodInvoke(propDescs[i], mapObj);
}
}
}
/**
* 参数注入到对象的方法
* @param selfObj
* @param valueObj
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private void methodInvoke(PropertyDescriptor selfObj, Object valueObj)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Method method = selfObj.getWriteMethod();
Object arglist[] = new Object[1];
arglist[0] = valueObj;
method.invoke(obj, arglist);
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
红色的部分是自己定义的实体Bean的基本属性。
6.BeanInfos类
用于封装实体基本属性的类
/**
* @author TonyDev
*
*/
public class BeanInfos {
private String fieldName; //属性名
private Class fieldType; //属性类型
private Object fieldValue; //属性值对象
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public Class getFieldType() {
return fieldType;
}
public void setFieldType(Class fieldType) {
this.fieldType = fieldType;
}
public Object getFieldValue() {
return fieldValue;
}
public void setFieldValue(Object fieldValue) {
this.fieldValue = fieldValue;
}
}
以上的错误处理部分没做得太详细,还往大家自己开动脑筋。
由于自己发现了许多BUG,暂时不提供JAR包,我会尽快修正。之后再开放下载