com.xiaoleilu.hutool.db.handler.HandleHelper Maven / Gradle / Ivy
package com.xiaoleilu.hutool.db.handler;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.Map;
import com.xiaoleilu.hutool.bean.BeanDesc.PropDesc;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.convert.Convert;
import com.xiaoleilu.hutool.db.Entity;
import com.xiaoleilu.hutool.util.ReflectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.xiaoleilu.hutool.util.TypeUtil;
/**
* 数据结果集处理辅助类
*
* @author loolly
*
*/
public class HandleHelper {
/**
* 处理单条数据
*
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @param bean 目标Bean
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 3.3.1
*/
public static T handleRow(int columnCount, ResultSetMetaData meta, ResultSet rs, T bean) throws SQLException {
return handleRow(columnCount, meta, rs).toBeanIgnoreCase(bean);
}
/**
* 处理单条数据
*
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @param beanClass 目标Bean类型
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 3.3.1
*/
@SuppressWarnings("unchecked")
public static T handleRow(int columnCount, ResultSetMetaData meta, ResultSet rs, Class beanClass) throws SQLException {
if(beanClass.isAssignableFrom(Entity.class)) {
//Entity的父类都可按照Entity返回
return (T) handleRow(columnCount, meta, rs);
}
final T bean = ReflectUtil.newInstanceIfPossible(beanClass);
//忽略字段大小写
final Map propMap = BeanUtil.getBeanDesc(beanClass).getPropMap(true);
String columnLabel;
PropDesc pd;
Method setter = null;
Object value = null;
for (int i = 1; i <= columnCount; i++) {
columnLabel = meta.getColumnLabel(i);
//驼峰命名风格
pd = propMap.get(StrUtil.toCamelCase(columnLabel));
if(null != pd) {
setter = pd.getSetter();
}
if(null != setter) {
value = getColumnValue(rs, columnLabel, meta.getColumnType(i));
value = Convert.convert(TypeUtil.getFirstParamType(setter), value);
ReflectUtil.invoke(bean, setter, new Object[] {value});
}
}
return bean;
}
/**
* 处理单条数据
*
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @return 每一行的Entity
* @throws SQLException SQL执行异常
*/
public static Entity handleRow(int columnCount, ResultSetMetaData meta, ResultSet rs) throws SQLException {
return handleRow(Entity.create(), columnCount, meta, rs, false);
}
/**
* 处理单条数据
*
* @param Entity及其子对象
* @param row Entity对象
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @param withTableName 是否包含表名
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 3.3.1
*/
public static T handleRow(T row, int columnCount, ResultSetMetaData meta, ResultSet rs, boolean withTableName) throws SQLException {
if(withTableName) {
row.setTableName(meta.getTableName(1));
}
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));
}
return row;
}
/**
* 处理单条数据
*
* @param rs 数据集
* @return 每一行的Entity
* @throws SQLException SQL执行异常
*/
public static Entity handleRow(ResultSet rs) throws SQLException {
final ResultSetMetaData meta = rs.getMetaData();
final int columnCount = meta.getColumnCount();
return handleRow(columnCount, meta, rs);
}
/**
* 处理多条数据
*
* @param 集合类型
* @param rs 数据集
* @param collection 数据集
* @return Entity列表
* @throws SQLException SQL执行异常
*/
public static > T handleRs(ResultSet rs, T collection) throws SQLException {
final ResultSetMetaData meta = rs.getMetaData();
final int columnCount = meta.getColumnCount();
while (rs.next()) {
collection.add(HandleHelper.handleRow(columnCount, meta, rs));
}
return collection;
}
/**
* 处理多条数据并返回一个Bean列表
*
* @param 集合元素类型
* @param 集合类型
* @param rs 数据集
* @param collection 数据集
* @param elementBeanType Bean类型
* @return Entity列表
* @throws SQLException SQL执行异常
* @since 3.1.0
*/
public static > T handleRsToBeanList(ResultSet rs, T collection, Class elementBeanType) throws SQLException {
final ResultSetMetaData meta = rs.getMetaData();
final int columnCount = meta.getColumnCount();
while (rs.next()) {
collection.add(handleRow(columnCount, meta, rs, elementBeanType));
}
return collection;
}
// -------------------------------------------------------------------------------------------------------------- Private method start
/**
* 获取字段值
* 针对日期时间等做单独处理判断
*
* @param rs {@link ResultSet}
* @param label 字段名
* @param type 字段类型,默认Object
* @return 字段值
* @throws SQLException SQL异常
* @since 3.2.3
*/
private static Object getColumnValue(ResultSet rs, String label, int type) throws SQLException {
switch (type) {
case Types.TIMESTAMP:
return rs.getTimestamp(label);
case Types.TIME:
return rs.getTime(label);
default:
return rs.getObject(label);
}
}
// -------------------------------------------------------------------------------------------------------------- Private method end
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy