重要接口代码:
package org.apache.commons.dbutils;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 将数据集转换其他类型的接口
*/
public interface ResultSetHandler {
/**
* 将数据集转换其他类型
*/
public Object handle(ResultSet rs) throws SQLException;
}
package org.apache.commons.dbutils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
* RowProcessor行结果处理其接口
*/
public interface RowProcessor {
/**
*返回的结果为一条记录的信息(使用于查询单条记录的信息)的对象数组
*/
public Object[] toArray(ResultSet rs) throws SQLException;
/**
*返回的结果为一条记录的信息(使用于查询单条记录的信息)对应的对象
*/
public Object toBean(ResultSet rs, Class type) throws SQLException;
/*
*返回所有记录对应的对象的集合信息
*/
public List toBeanList(ResultSet rs, Class type) throws SQLException;
/*
*返回所有记录对应的对象的Map集合信息
*/
public Map toMap(ResultSet rs) throws SQLException;
}
实现此接口的类:BasicRowProcessor:
/**
*bean处理器的类用于处理数据和bean之间的转换
*/
public class BeanProcessor {
protected static final int PROPERTY_NOT_FOUND = -1;
/**
*存储bean的原始数据类型
*/
private static final Map primitiveDefaults = new HashMap();
static {
primitiveDefaults.put(Integer.TYPE, new Integer(0));
primitiveDefaults.put(Short.TYPE, new Short((short) 0));
primitiveDefaults.put(Byte.TYPE, new Byte((byte) 0));
primitiveDefaults.put(Float.TYPE, new Float(0));
primitiveDefaults.put(Double.TYPE, new Double(0));
primitiveDefaults.put(Long.TYPE, new Long(0));
primitiveDefaults.put(Boolean.TYPE, Boolean.FALSE);
primitiveDefaults.put(Character.TYPE, new Character('\u0000'));
}
public BeanProcessor() {
super();
}
/**
* 将数据集中一条记录转换为一个对象
*/
public Object toBean(ResultSet rs, Class type) throws SQLException {
PropertyDescriptor[] props = this.propertyDescriptors(type);
ResultSetMetaData rsmd = rs.getMetaData();
int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
return this.createBean(rs, type, props, columnToProperty);
}