优化代码,默认属性名为小驼峰命名,数据库列名为下划线命名。
parent
3ad9c9fddb
commit
c6582ecf6b
6
pom.xml
6
pom.xml
|
@ -21,12 +21,6 @@
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>3.16.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
|
|
@ -18,9 +18,9 @@ package xyz.zhouxy.jdbc;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper;
|
import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper;
|
||||||
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -28,11 +28,11 @@ import java.util.*;
|
||||||
public class DbRecord extends AbstractMapWrapper<String, Object, DbRecord> {
|
public class DbRecord extends AbstractMapWrapper<String, Object, DbRecord> {
|
||||||
|
|
||||||
public DbRecord() {
|
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<String, Object> map) {
|
public DbRecord(Map<String, Object> 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<String> getValueAsString(String key) {
|
public Optional<String> getValueAsString(String key) {
|
||||||
|
|
|
@ -33,6 +33,8 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.base.CaseFormat;
|
||||||
|
|
||||||
public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
||||||
|
|
||||||
private final Constructor<T> constructor;
|
private final Constructor<T> constructor;
|
||||||
|
@ -50,24 +52,28 @@ public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
||||||
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
|
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try {
|
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<T> constructor = beanType.getDeclaredConstructor();
|
||||||
constructor.setAccessible(true); // NOSONAR
|
constructor.setAccessible(true); // NOSONAR
|
||||||
|
|
||||||
|
// 构建 column name 和 PropertyDescriptor 的 映射
|
||||||
|
BeanInfo beanInfo = Introspector.getBeanInfo(beanType);
|
||||||
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
||||||
|
|
||||||
|
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));
|
||||||
return new DefaultBeanResultMap<>(constructor, colPropertyMap);
|
return new DefaultBeanResultMap<>(constructor, colPropertyMap);
|
||||||
}
|
}
|
||||||
catch (IntrospectionException e) {
|
catch (IntrospectionException e) {
|
||||||
|
|
|
@ -22,13 +22,11 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
import xyz.zhouxy.jdbc.DbRecord;
|
import xyz.zhouxy.jdbc.DbRecord;
|
||||||
import xyz.zhouxy.jdbc.DefaultBeanResultMap;
|
|
||||||
import xyz.zhouxy.jdbc.ResultMap;
|
import xyz.zhouxy.jdbc.ResultMap;
|
||||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate;
|
import xyz.zhouxy.jdbc.SimpleJdbcTemplate;
|
||||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor;
|
import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor;
|
||||||
|
@ -229,7 +227,7 @@ class SimpleJdbcTemplateTests {
|
||||||
Optional<TestBean> t = jdbcTemplate.queryFirst(
|
Optional<TestBean> t = jdbcTemplate.queryFirst(
|
||||||
"SELECT * FROM test_table WHERE id = ?",
|
"SELECT * FROM test_table WHERE id = ?",
|
||||||
buildParams(22915),
|
buildParams(22915),
|
||||||
ResultMap.beanResultMap(TestBean.class, ImmutableMap.of("usageDate", "usage_date", "usageDuration", "usage_duration")));
|
ResultMap.beanResultMap(TestBean.class));
|
||||||
log.info("t: {}", t);
|
log.info("t: {}", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue