Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.db.handler;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.Entity;
import org.dromara.hutool.db.handler.row.*;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.List;
/**
* 数据结果集处理辅助类
*
* @author looly
*/
public class ResultSetUtil {
/**
* 处理单条数据
*
* @param Bean类型
* @param meta ResultSetMetaData
* @param rs 数据集
* @param beanClass 目标Bean类型
* @return 每一行的Entity
* @throws SQLException SQL执行异常
* @since 3.3.1
*/
@SuppressWarnings("unchecked")
public static T toBean(final ResultSetMetaData meta, final ResultSet rs, final Class beanClass) throws SQLException {
Assert.notNull(beanClass, "Bean Class must be not null !");
if (beanClass.isArray()) {
//返回数组
return (T) new ArrayRowHandler<>(meta, beanClass.getComponentType()).handle(rs);
} else if (Iterable.class.isAssignableFrom(beanClass)) {
//集合
final Object[] objRow = toBean(meta, rs, Object[].class);
return ConvertUtil.convert(beanClass, objRow);
} else if (beanClass.isAssignableFrom(Entity.class)) {
//Entity的父类都可按照Entity返回
return (T) new EntityRowHandler(meta, false, true).handle(rs);
} else if (String.class == beanClass) {
//字符串
final Object[] objRow = toBean(meta, rs, Object[].class);
return (T) StrUtil.join(", ", objRow);
}
return new BeanRowHandler<>(meta, beanClass, true).handle(rs);
}
/**
* 处理单行数据
*
* @param rs 数据集(行)
* @return 每一行的List
* @throws SQLException SQL执行异常
* @since 5.1.6
*/
public static List