补充文档注释
This commit is contained in:
@@ -35,9 +35,28 @@ import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
/**
|
||||
* DefaultBeanRowMapper
|
||||
*
|
||||
* <p>
|
||||
* 默认实现的将 {@link ResultSet} 转换为 Java Bean 的 {@link RowMapper}。
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: 使用反射获取类型信息,也是使用反射调用无参构造器和 {@code setter} 方法。
|
||||
* 实际使用中还是建议针对目标类型自定义 {@link RowMapper}。</b>
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
/** Bean 的无参构造器 */
|
||||
private final Constructor<T> constructor;
|
||||
|
||||
/** 列名到属性的映射 */
|
||||
private final Map<String, PropertyDescriptor> colPropertyMap;
|
||||
|
||||
private DefaultBeanRowMapper(Constructor<T> constructor, Map<String, PropertyDescriptor> colPropertyMap) {
|
||||
@@ -45,10 +64,29 @@ public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
this.colPropertyMap = colPropertyMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 DefaultBeanRowMapper
|
||||
*
|
||||
* @param <T> Bean 类型
|
||||
* @param beanType Bean 类型
|
||||
* @return DefaultBeanRowMapper 对象
|
||||
* @throws SQLException 创建 DefaultBeanRowMapper 出现错误的异常时抛出
|
||||
*/
|
||||
@StaticFactoryMethod(DefaultBeanRowMapper.class)
|
||||
public static <T> DefaultBeanRowMapper<T> of(Class<T> beanType) throws SQLException {
|
||||
return of(beanType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 DefaultBeanRowMapper
|
||||
*
|
||||
* @param <T> Bean 类型
|
||||
* @param beanType Bean 类型
|
||||
* @param propertyColMap Bean 字段与列名的映射关系。key 是字段,value 是列名。
|
||||
* @return DefaultBeanRowMapper 对象
|
||||
* @throws SQLException 创建 DefaultBeanRowMapper 出现错误的异常时抛出
|
||||
*/
|
||||
@StaticFactoryMethod(DefaultBeanRowMapper.class)
|
||||
public static <T> DefaultBeanRowMapper<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
|
||||
throws SQLException {
|
||||
try {
|
||||
@@ -60,6 +98,7 @@ public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(beanType);
|
||||
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
||||
|
||||
// Bean 的属性名为小驼峰,对应的列名为下划线
|
||||
Function<? super PropertyDescriptor, String> keyMapper;
|
||||
if (propertyColMap == null || propertyColMap.isEmpty()) {
|
||||
keyMapper = p -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, p.getName());
|
||||
@@ -84,13 +123,17 @@ public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
|
||||
try {
|
||||
// 调用无参构造器创建实例
|
||||
T newInstance = this.constructor.newInstance();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
// 遍历结果的每一列
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String colName = metaData.getColumnName(i);
|
||||
// 获取查询结果列名对应的属性,调用 setter
|
||||
PropertyDescriptor propertyDescriptor = this.colPropertyMap.get(colName);
|
||||
if (propertyDescriptor != null) {
|
||||
Method setter = propertyDescriptor.getWriteMethod();
|
||||
|
Reference in New Issue
Block a user