2022-06-21 18:42:52 +08:00

41 lines
1023 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.hutool.db.handler;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* Bean对象处理器只处理第一条数据
*
* @param <E> 处理对象类型
* @author loolly
*@since 3.1.0
*/
public class BeanHandler<E> implements RsHandler<E>{
private static final long serialVersionUID = -5491214744966544475L;
private final Class<E> elementBeanType;
/**
* 创建一个 BeanHandler对象
*
* @param <E> 处理对象类型
* @param beanType Bean类型
* @return BeanHandler对象
*/
public static <E> BeanHandler<E> of(final Class<E> beanType) {
return new BeanHandler<>(beanType);
}
public BeanHandler(final Class<E> beanType) {
this.elementBeanType = beanType;
}
@Override
public E handle(final ResultSet rs) throws SQLException {
final ResultSetMetaData meta = rs.getMetaData();
final int columnCount = meta.getColumnCount();
return rs.next() ? ResultSetUtil.toBean(columnCount, meta, rs, this.elementBeanType) : null;
}
}