cn.hutool.db.handler.HandleHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.db.handler;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.PropDesc;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.db.Entity;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
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;
/**
* 数据结果集处理辅助类
*
* @author loolly
*
*/
public class HandleHelper {
/**
* 处理单条数据
*
* @param Bean类型
* @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 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 {
Assert.notNull(beanClass, "Bean Class must be not null !");
if(beanClass.isArray()) {
//返回数组
final Class> componentType = beanClass.getComponentType();
final Object[] result = ArrayUtil.newArray(componentType, columnCount);
for(int i = 0,j = 1; i < columnCount; i++, j++) {
result[i] = getColumnValue(rs, j, meta.getColumnType(j), componentType);
}
return (T) result;
} else if(Iterable.class.isAssignableFrom(beanClass)) {
//集合
final Object[] objRow = handleRow(columnCount, meta, rs, Object[].class);
return Convert.convert(beanClass, objRow);
} else if(beanClass.isAssignableFrom(Entity.class)) {
//Entity的父类都可按照Entity返回
return (T) handleRow(columnCount, meta, rs);
} else if(String.class == beanClass) {
//字符串
final Object[] objRow = handleRow(columnCount, meta, rs, Object[].class);
return (T) StrUtil.join(", ", objRow);
}
//普通bean
final T bean = ReflectUtil.newInstanceIfPossible(beanClass);
//忽略字段大小写
final Map propMap = BeanUtil.getBeanDesc(beanClass).getPropMap(true);
String columnLabel;
PropDesc pd;
Method setter;
Object value;
for (int i = 1; i <= columnCount; i++) {
columnLabel = meta.getColumnLabel(i);
pd = propMap.get(columnLabel);
if(null == pd) {
// 尝试驼峰命名风格
pd = propMap.get(StrUtil.toCamelCase(columnLabel));
}
setter = (null == pd) ? null : pd.getSetter();
if(null != setter) {
value = getColumnValue(rs, i, meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
ReflectUtil.invokeWithCheck(bean, setter, 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(columnCount, meta, rs, false);
}
/**
* 处理单条数据
*
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @param caseInsensitive 是否大小写不敏感
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 4.5.16
*/
public static Entity handleRow(int columnCount, ResultSetMetaData meta, ResultSet rs, boolean caseInsensitive) throws SQLException {
return handleRow(new Entity(null, caseInsensitive), columnCount, meta, rs, true);
}
/**
* 处理单条数据
*
* @param Entity及其子对象
* @param row Entity对象
* @param columnCount 列数
* @param meta ResultSetMetaData
* @param rs 数据集
* @param withMetaInfo 是否包含表名、字段名等元信息
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 3.3.1
*/
public static T handleRow(T row, int columnCount, ResultSetMetaData meta, ResultSet rs, boolean withMetaInfo) throws SQLException {
int type;
String columnLabel;
for (int i = 1; i <= columnCount; i++) {
type = meta.getColumnType(i);
columnLabel = meta.getColumnLabel(i);
if("rownum_".equalsIgnoreCase(columnLabel)){
// issue#2618@Github
// 分页时会查出rownum字段,此处忽略掉读取
continue;
}
row.put(columnLabel, getColumnValue(rs, i, type, null));
}
if (withMetaInfo) {
try {
row.setTableName(meta.getTableName(1));
} catch (SQLException ignore){
//issue#I2AGLU@Gitee
// Hive等NoSQL中无表的概念,此处报错,跳过。
}
row.setFieldNames(row.keySet());
}
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 rs 数据集(行)
* @return 每一行的List
* @throws SQLException SQL执行异常
* @since 5.1.6
*/
public static List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy