diff --git a/pom.xml b/pom.xml index 6cb554b..bd84b4a 100644 --- a/pom.xml +++ b/pom.xml @@ -21,12 +21,6 @@ 0.1.0-SNAPSHOT - - org.apache.commons - commons-lang3 - 3.16.0 - - org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/xyz/zhouxy/jdbc/DbRecord.java b/src/main/java/xyz/zhouxy/jdbc/DbRecord.java index e4ada93..33af73c 100644 --- a/src/main/java/xyz/zhouxy/jdbc/DbRecord.java +++ b/src/main/java/xyz/zhouxy/jdbc/DbRecord.java @@ -18,9 +18,9 @@ package xyz.zhouxy.jdbc; import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; -import org.apache.commons.lang3.StringUtils; import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper; import xyz.zhouxy.plusone.commons.util.OptionalTools; +import xyz.zhouxy.plusone.commons.util.StringTools; import java.util.*; @@ -28,11 +28,11 @@ import java.util.*; public class DbRecord extends AbstractMapWrapper { public DbRecord() { - super(new HashMap<>(), k -> Preconditions.checkArgument(StringUtils.isNotBlank(k), "Key must has text."), null); + super(new HashMap<>(), k -> Preconditions.checkArgument(StringTools.isNotBlank(k), "Key must has text."), null); } public DbRecord(Map map) { - super(map, k -> Preconditions.checkArgument(StringUtils.isNotBlank(k), "Key must has text."), null); + super(map, k -> Preconditions.checkArgument(StringTools.isNotBlank(k), "Key must has text."), null); } public Optional getValueAsString(String key) { diff --git a/src/main/java/xyz/zhouxy/jdbc/DefaultBeanResultMap.java b/src/main/java/xyz/zhouxy/jdbc/DefaultBeanResultMap.java index e1c9e9b..238031c 100644 --- a/src/main/java/xyz/zhouxy/jdbc/DefaultBeanResultMap.java +++ b/src/main/java/xyz/zhouxy/jdbc/DefaultBeanResultMap.java @@ -33,6 +33,8 @@ import java.util.stream.Collectors; import javax.annotation.Nullable; +import com.google.common.base.CaseFormat; + public class DefaultBeanResultMap implements ResultMap { private final Constructor constructor; @@ -50,24 +52,28 @@ public class DefaultBeanResultMap implements ResultMap { public static DefaultBeanResultMap of(Class beanType, @Nullable Map propertyColMap) throws SQLException { try { - BeanInfo beanInfo = Introspector.getBeanInfo(beanType); - PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); - Map 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 constructor = beanType.getDeclaredConstructor(); constructor.setAccessible(true); // NOSONAR + + // 构建 column name 和 PropertyDescriptor 的 映射 + BeanInfo beanInfo = Introspector.getBeanInfo(beanType); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + + Function 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 colPropertyMap = Arrays.stream(propertyDescriptors).collect( + Collectors.toMap(keyMapper, Function.identity(), (a, b) -> b)); return new DefaultBeanResultMap<>(constructor, colPropertyMap); } catch (IntrospectionException e) { diff --git a/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java index a50436d..0f22b03 100644 --- a/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java @@ -22,13 +22,11 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import xyz.zhouxy.jdbc.DbRecord; -import xyz.zhouxy.jdbc.DefaultBeanResultMap; import xyz.zhouxy.jdbc.ResultMap; import xyz.zhouxy.jdbc.SimpleJdbcTemplate; import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor; @@ -229,7 +227,7 @@ class SimpleJdbcTemplateTests { Optional t = jdbcTemplate.queryFirst( "SELECT * FROM test_table WHERE id = ?", buildParams(22915), - ResultMap.beanResultMap(TestBean.class, ImmutableMap.of("usageDate", "usage_date", "usageDuration", "usage_duration"))); + ResultMap.beanResultMap(TestBean.class)); log.info("t: {}", t); } }