forked from plusone/simple-jdbc
Compare commits
3 Commits
7e2072df06
...
0317a9e561
Author | SHA1 | Date | |
---|---|---|---|
0317a9e561 | |||
1f153510e6 | |||
f04a34f366 |
@@ -94,25 +94,7 @@ public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
Constructor<T> constructor = beanType.getDeclaredConstructor();
|
||||
constructor.setAccessible(true); // NOSONAR
|
||||
|
||||
// 构建 column name 和 PropertyDescriptor 的 映射
|
||||
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());
|
||||
}
|
||||
else {
|
||||
keyMapper = p -> {
|
||||
String propertyName = p.getName();
|
||||
String colName = propertyColMap.get(propertyName);
|
||||
return colName != null ? colName
|
||||
: CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, propertyName);
|
||||
};
|
||||
}
|
||||
Map<String, PropertyDescriptor> colPropertyMap = Arrays.stream(propertyDescriptors).collect(
|
||||
Collectors.toMap(keyMapper, Function.identity(), (a, b) -> b));
|
||||
final Map<String, PropertyDescriptor> colPropertyMap = buildColPropertyMap(beanType, propertyColMap);
|
||||
return new DefaultBeanRowMapper<>(constructor, colPropertyMap);
|
||||
}
|
||||
catch (IntrospectionException e) {
|
||||
@@ -150,4 +132,36 @@ public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 column name 和 PropertyDescriptor 的 映射
|
||||
*
|
||||
* @param <T> Java bean 类型
|
||||
* @param beanType Java bean 类型
|
||||
* @param propertyColMap 属性与列名的映射
|
||||
* @return column name 和 PropertyDescriptor 的映射
|
||||
* @throws IntrospectionException if an exception occurs during introspection.
|
||||
*/
|
||||
private static <T> Map<String, PropertyDescriptor> buildColPropertyMap(
|
||||
Class<T> beanType, Map<String, String> propertyColMap) throws IntrospectionException {
|
||||
|
||||
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());
|
||||
}
|
||||
else {
|
||||
keyMapper = p -> {
|
||||
String propertyName = p.getName();
|
||||
String colName = propertyColMap.get(propertyName);
|
||||
return colName != null ? colName
|
||||
: CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, propertyName);
|
||||
};
|
||||
}
|
||||
return Arrays.stream(propertyDescriptors)
|
||||
.collect(Collectors.toMap(keyMapper, Function.identity(), (a, b) -> b));
|
||||
}
|
||||
}
|
||||
|
@@ -252,7 +252,7 @@ class JdbcOperationSupport {
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
fillStatement(stmt, params);
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet generatedKeys = stmt.getGeneratedKeys();) {
|
||||
try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
|
||||
int rowNumber = 0;
|
||||
while (generatedKeys.next()) {
|
||||
T e = rowMapper.mapRow(generatedKeys, rowNumber++);
|
||||
@@ -399,12 +399,13 @@ class JdbcOperationSupport {
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行查询,将查询结果的第一行数据按照指定逻辑进行处理,返回 {@link Optional}
|
||||
* 执行查询,将查询结果的第一行数据按照指定逻辑进行处理,返回映射结果
|
||||
*
|
||||
* @param conn 数据库连接
|
||||
* @param sql SQL
|
||||
* @param params 参数
|
||||
* @param rowMapper 行数据映射逻辑
|
||||
* @return 映射结果。如果查询结果为空,则返回 null
|
||||
*/
|
||||
private static <T> T queryFirstInternal(@Nonnull Connection conn,
|
||||
@Nonnull String sql,
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package xyz.zhouxy.jdbc;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
@@ -37,7 +37,7 @@ public interface RowMapper<T> {
|
||||
T mapRow(ResultSet rs, int rowNumber) throws SQLException;
|
||||
|
||||
/** 每一行数据转换为 {@link HashMap} */
|
||||
public static final RowMapper<Map<String, Object>> HASH_MAP_MAPPER = (rs, rowNumber) -> {
|
||||
RowMapper<Map<String, Object>> HASH_MAP_MAPPER = (rs, rowNumber) -> {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
@@ -49,12 +49,12 @@ public interface RowMapper<T> {
|
||||
};
|
||||
|
||||
/** 默认实现的将 {@link ResultSet} 转换为 Java Bean 的 {@link RowMapper}。 */
|
||||
public static <T> RowMapper<T> beanRowMapper(Class<T> beanType) throws SQLException {
|
||||
static <T> RowMapper<T> beanRowMapper(Class<T> beanType) throws SQLException {
|
||||
return DefaultBeanRowMapper.of(beanType);
|
||||
}
|
||||
|
||||
/** 默认实现的将 {@link ResultSet} 转换为 Java Bean 的 {@link RowMapper}。 */
|
||||
public static <T> RowMapper<T> beanRowMapper(Class<T> beanType, Map<String, String> propertyColMap)
|
||||
static <T> RowMapper<T> beanRowMapper(Class<T> beanType, Map<String, String> propertyColMap)
|
||||
throws SQLException {
|
||||
return DefaultBeanRowMapper.of(beanType, propertyColMap);
|
||||
}
|
||||
|
Reference in New Issue
Block a user