这样用反射从结果集中提取JavaBean会影响效率吗?
/**
* 从结果集中取出游标当前所指的记录,封装为JavaBean
* @param beanClass
* @param rs
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws SQLException
*/
public static <B> B extractOneBean(Class<B> beanClass, ResultSet rs)
throws SQLException,
IllegalAccessException,
InstantiationException,
InvocationTargetException {
B bean = beanClass.newInstance();
Method[] methods = beanClass.getMethods();
String methodName;
int clmIndex;
Class parType;
Object parValue;
for (Method method : methods) {
// filter method
methodName = method.getName();
if (!methodName.startsWith("set")) {
continue;
}
try {
clmIndex = rs.findColumn(methodName.substring(3));
} catch (SQLException e) {
continue;
}
// invoke setter
parType = method.getParameterTypes()[0];
parValue = null;
if (parType == String.class) {
parValue = rs.getString(clmIndex);
} else if (parType == int.class) {
parValue = rs.getInt(clmIndex);
} else if (parType == Date.class) {
parValue = rs.getDate(clmIndex);
}
if (parValue != null) {
method.invoke(bean, parValue);
}
}
return bean;
}
/**
* 取出结果集中的所有记录,封装为JavaBean List
* @param beanClass
* @param rs
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws SQLException
*/
public static <B> List<B> extractAllBeans(Class<B> beanClass, ResultSet rs)
throws SQLException,
IllegalAccessException,
InstantiationException,
InvocationTargetException {
// create bean list
ArrayList<B> beanList = new ArrayList<B>();
rs.last();
int rowCount = rs.getRow();
for (int i = 0; i < rowCount; i++) {
beanList.add(beanClass.newInstance());
}
Method[] methods = beanClass.getMethods();
String methodName;
int clmIndex;
Class parType;
Object parValue;
for (Method method : methods) {
// filter method
methodName = method.getName();
if (!methodName.startsWith("set")) {
continue;
}
try {
clmIndex = rs.findColumn(methodName.substring(3));
} catch (SQLException e) {
continue;
}
// invoke setter on each bean
rs.first();
parType = method.getParameterTypes()[0];
for(int i = 0; i < rowCount; i++) {
parValue = null;
if (parType == String.class) {
parValue = rs.getString(clmIndex);
} else if (parType == int.class) {
parValue = rs.getInt(clmIndex);
} else if (parType == Date.class) {
parValue = rs.getDate(clmIndex);
}
if (parValue != null) {
method.invoke(beanList.get(i), parValue);
}
rs.next();
}
}
return beanList;
}
/**
* 从结果集中取出游标当前所指的记录,封装为JavaBean
* @param beanClass
* @param rs
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws SQLException
*/
public static <B> B extractOneBean(Class<B> beanClass, ResultSet rs)
throws SQLException,
IllegalAccessException,
InstantiationException,
InvocationTargetException {
B bean = beanClass.newInstance();
Method[] methods = beanClass.getMethods();
String methodName;
int clmIndex;
Class parType;
Object parValue;
for (Method method : methods) {
// filter method
methodName = method.getName();
if (!methodName.startsWith("set")) {
continue;
}
try {
clmIndex = rs.findColumn(methodName.substring(3));
} catch (SQLException e) {
continue;
}
// invoke setter
parType = method.getParameterTypes()[0];
parValue = null;
if (parType == String.class) {
parValue = rs.getString(clmIndex);
} else if (parType == int.class) {
parValue = rs.getInt(clmIndex);
} else if (parType == Date.class) {
parValue = rs.getDate(clmIndex);
}
if (parValue != null) {
method.invoke(bean, parValue);
}
}
return bean;
}
/**
* 取出结果集中的所有记录,封装为JavaBean List
* @param beanClass
* @param rs
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws SQLException
*/
public static <B> List<B> extractAllBeans(Class<B> beanClass, ResultSet rs)
throws SQLException,
IllegalAccessException,
InstantiationException,
InvocationTargetException {
// create bean list
ArrayList<B> beanList = new ArrayList<B>();
rs.last();
int rowCount = rs.getRow();
for (int i = 0; i < rowCount; i++) {
beanList.add(beanClass.newInstance());
}
Method[] methods = beanClass.getMethods();
String methodName;
int clmIndex;
Class parType;
Object parValue;
for (Method method : methods) {
// filter method
methodName = method.getName();
if (!methodName.startsWith("set")) {
continue;
}
try {
clmIndex = rs.findColumn(methodName.substring(3));
} catch (SQLException e) {
continue;
}
// invoke setter on each bean
rs.first();
parType = method.getParameterTypes()[0];
for(int i = 0; i < rowCount; i++) {
parValue = null;
if (parType == String.class) {
parValue = rs.getString(clmIndex);
} else if (parType == int.class) {
parValue = rs.getInt(clmIndex);
} else if (parType == Date.class) {
parValue = rs.getDate(clmIndex);
}
if (parValue != null) {
method.invoke(beanList.get(i), parValue);
}
rs.next();
}
}
return beanList;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货