From c6582ecf6bf99cdc63b648b053f3ee407798ef07 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 3 Oct 2024 17:12:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=B1=9E=E6=80=A7=E5=90=8D=E4=B8=BA=E5=B0=8F?= =?UTF-8?q?=E9=A9=BC=E5=B3=B0=E5=91=BD=E5=90=8D=EF=BC=8C=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=88=97=E5=90=8D=E4=B8=BA=E4=B8=8B=E5=88=92=E7=BA=BF?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 --- src/main/java/xyz/zhouxy/jdbc/DbRecord.java | 6 +-- .../xyz/zhouxy/jdbc/DefaultBeanResultMap.java | 38 +++++++++++-------- .../jdbc/test/SimpleJdbcTemplateTests.java | 4 +- 4 files changed, 26 insertions(+), 28 deletions(-) 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); } }