新增 DefaultBeanResultMap

This commit is contained in:
2024-10-03 16:27:19 +08:00
parent fda69cea6b
commit 7f31d477d6
4 changed files with 165 additions and 7 deletions

View File

@@ -0,0 +1,88 @@
package xyz.zhouxy.jdbc;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class DefaultBeanResultMap<T> implements ResultMap<T> {
private final Constructor<T> constructor;
private final Map<String, PropertyDescriptor> colPropertyMap;
private DefaultBeanResultMap(Constructor<T> constructor, Map<String, PropertyDescriptor> colPropertyMap) {
this.constructor = constructor;
this.colPropertyMap = colPropertyMap;
}
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType) throws SQLException {
return of(beanType, null);
}
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
throws SQLException {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(beanType);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
Map<String, PropertyDescriptor> colPropertyMap;
if (propertyColMap != null && !propertyColMap.isEmpty()) {
colPropertyMap = Arrays.stream(propertyDescriptors)
.collect(Collectors.toMap(p -> {
String propertyName = p.getName();
String colName = propertyColMap.get(propertyName);
return colName != null ? colName : propertyName;
}, Function.identity(), (a, b) -> b));
}
else {
colPropertyMap = Arrays.stream(propertyDescriptors)
.collect(Collectors.toMap(PropertyDescriptor::getName, Function.identity(), (a, b) -> b));
}
Constructor<T> constructor = beanType.getDeclaredConstructor();
constructor.setAccessible(true); // NOSONAR
return new DefaultBeanResultMap<>(constructor, colPropertyMap);
}
catch (IntrospectionException e) {
throw new SQLException("There is an exception occurs during introspection.", e);
}
catch (NoSuchMethodException e) {
throw new SQLException("Could not find a no-args constructor in " + beanType.getName(), e);
}
}
@Override
public T map(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);
PropertyDescriptor propertyDescriptor = this.colPropertyMap.get(colName);
if (propertyDescriptor != null) {
Method setter = propertyDescriptor.getWriteMethod();
if (setter != null) {
Class<?> propertyType = propertyDescriptor.getPropertyType();
setter.setAccessible(true); // NOSONAR
setter.invoke(newInstance, rs.getObject(colName, propertyType));
}
}
}
return newInstance;
}
catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new SQLException(e);
}
}
}