|
|
@@ -6,7 +6,9 @@ import java.sql.ResultSet;
|
|
|
import java.sql.ResultSetMetaData;
|
|
|
import java.sql.SQLException;
|
|
|
import java.sql.Types;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanDesc.PropDesc;
|
|
|
@@ -97,8 +99,8 @@ public class HandleHelper {
|
|
|
}
|
|
|
setter = (null == pd) ? null : pd.getSetter();
|
|
|
if(null != setter) {
|
|
|
- value = getColumnValue(rs, columnLabel, meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
|
|
|
- ReflectUtil.invokeWithCheck(bean, setter, new Object[] {value});
|
|
|
+ value = getColumnValue(rs, i, meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
|
|
|
+ ReflectUtil.invokeWithCheck(bean, setter, value);
|
|
|
}
|
|
|
}
|
|
|
return bean;
|
|
|
@@ -149,9 +151,8 @@ public class HandleHelper {
|
|
|
String columnLabel;
|
|
|
int type;
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
- columnLabel = meta.getColumnLabel(i);
|
|
|
type = meta.getColumnType(i);
|
|
|
- row.put(columnLabel, getColumnValue(rs, columnLabel, type, null));
|
|
|
+ row.put(meta.getColumnLabel(i), getColumnValue(rs, i, type, null));
|
|
|
}
|
|
|
if (withMetaInfo) {
|
|
|
row.setTableName(meta.getTableName(1));
|
|
|
@@ -162,7 +163,7 @@ public class HandleHelper {
|
|
|
|
|
|
/**
|
|
|
* 处理单条数据
|
|
|
- *
|
|
|
+ *
|
|
|
* @param rs 数据集
|
|
|
* @return 每一行的Entity
|
|
|
* @throws SQLException SQL执行异常
|
|
|
@@ -174,6 +175,25 @@ public class HandleHelper {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 处理单行数据
|
|
|
+ *
|
|
|
+ * @param rs 数据集(行)
|
|
|
+ * @return 每一行的List
|
|
|
+ * @throws SQLException SQL执行异常
|
|
|
+ * @since 5.1.6
|
|
|
+ */
|
|
|
+ public static List<Object> handleRowToList(ResultSet rs) throws SQLException {
|
|
|
+ final ResultSetMetaData meta = rs.getMetaData();
|
|
|
+ final int columnCount = meta.getColumnCount();
|
|
|
+ final List<Object> row = new ArrayList<>(columnCount);
|
|
|
+ for (int i = 1; i <= columnCount; i++) {
|
|
|
+ row.add(getColumnValue(rs, i, meta.getColumnType(i), null));
|
|
|
+ }
|
|
|
+
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 处理多条数据
|
|
|
*
|
|
|
* @param <T> 集合类型
|
|
|
@@ -238,39 +258,6 @@ public class HandleHelper {
|
|
|
*
|
|
|
* @param <T> 返回类型
|
|
|
* @param rs {@link ResultSet}
|
|
|
- * @param label 字段标签或者字段名
|
|
|
- * @param type 字段类型,默认Object
|
|
|
- * @param targetColumnType 结果要求的类型,需进行二次转换(null或者Object不转换)
|
|
|
- * @return 字段值
|
|
|
- * @throws SQLException SQL异常
|
|
|
- */
|
|
|
- private static <T> Object getColumnValue(ResultSet rs, String label, int type, Type targetColumnType) throws SQLException {
|
|
|
- Object rawValue;
|
|
|
- switch (type) {
|
|
|
- case Types.TIMESTAMP:
|
|
|
- rawValue = rs.getTimestamp(label);
|
|
|
- break;
|
|
|
- case Types.TIME:
|
|
|
- rawValue = rs.getTime(label);
|
|
|
- break;
|
|
|
- default:
|
|
|
- rawValue = rs.getObject(label);
|
|
|
- }
|
|
|
- if (null == targetColumnType || Object.class == targetColumnType) {
|
|
|
- // 无需转换
|
|
|
- return rawValue;
|
|
|
- } else {
|
|
|
- // 按照返回值要求转换
|
|
|
- return Convert.convert(targetColumnType, rawValue);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取字段值<br>
|
|
|
- * 针对日期时间等做单独处理判断
|
|
|
- *
|
|
|
- * @param <T> 返回类型
|
|
|
- * @param rs {@link ResultSet}
|
|
|
* @param columnIndex 字段索引
|
|
|
* @param type 字段类型,默认Object
|
|
|
* @param targetColumnType 结果要求的类型,需进行二次转换(null或者Object不转换)
|