Skip to content

Commit b6d85d4

Browse files
author
zhangrongfan
committed
Judge Property.empty
1 parent 8b1591f commit b6d85d4

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

mybatis-boost-core/src/main/java/cn/mybatisboost/json/JsonResultSetsHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public Object intercept(Invocation invocation) throws Throwable {
3434

3535
for (Object object : proceed) {
3636
for (Field field : fields) {
37-
Object value = field.get(object);
38-
if (value != null) {
39-
value = ((Property<?>) value).get();
40-
}
37+
Object value = ((Property<?>) field.get(object)).orElse(null);
4138
if (value instanceof String) {
4239
field.set(object, Property.of(JsonTypeHandler.objectMapper.readValue((String) value,
4340
(Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0])));

mybatis-boost-core/src/main/java/cn/mybatisboost/json/JsonTypeHandler.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@ public class JsonTypeHandler extends BaseTypeHandler<Property<?>> {
2121
public void setNonNullParameter(PreparedStatement ps, int i, Property<?> parameter, JdbcType jdbcType)
2222
throws SQLException {
2323
try {
24-
ps.setString(i, objectMapper.writeValueAsString(parameter.get()));
24+
if (parameter.isPresent()) {
25+
ps.setString(i, objectMapper.writeValueAsString(parameter.get()));
26+
} else {
27+
ps.setNull(i, ps.getParameterMetaData().getParameterType(i));
28+
}
2529
} catch (JsonProcessingException e) {
2630
throw new RuntimeException(e);
2731
}
2832
}
2933

3034
@Override
3135
public Property<?> getNullableResult(ResultSet rs, String columnName) throws SQLException {
32-
return Property.of(rs.getString(columnName));
36+
return Property.ofNullable(rs.getString(columnName));
3337
}
3438

3539
@Override
3640
public Property<?> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
37-
return Property.of(rs.getString(columnIndex));
41+
return Property.ofNullable(rs.getString(columnIndex));
3842
}
3943

4044
@Override
4145
public Property<?> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
42-
return Property.of(cs.getString(columnIndex));
46+
return Property.ofNullable(cs.getString(columnIndex));
4347
}
4448
}

mybatis-boost-core/src/main/java/cn/mybatisboost/support/Property.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
package cn.mybatisboost.support;
22

3+
import java.util.NoSuchElementException;
34
import java.util.Objects;
45
import java.util.function.Consumer;
56
import java.util.function.Supplier;
67

78
public class Property<T> {
89

10+
private static final Property<?> EMPTY = new Property<>();
911
private final T value;
1012

1113
private Property() {
1214
this.value = null;
1315
}
1416

1517
private Property(T value) {
16-
this.value = value;
18+
this.value = Objects.requireNonNull(value);
19+
}
20+
21+
@SuppressWarnings("unchecked")
22+
public static <T> Property<T> empty() {
23+
return (Property<T>) EMPTY;
1724
}
1825

1926
public static <T> Property<T> of(T value) {
2027
return new Property<>(value);
2128
}
2229

30+
public static <T> Property<T> ofNullable(T value) {
31+
return value == null ? empty() : of(value);
32+
}
33+
2334
public T get() {
35+
if (value == null) {
36+
throw new NoSuchElementException("No value present");
37+
}
2438
return value;
2539
}
2640

mybatis-boost-core/src/main/java/cn/mybatisboost/util/EntityUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.mybatisboost.util;
22

33
import cn.mybatisboost.core.adaptor.NameAdaptor;
4+
import cn.mybatisboost.support.Property;
45
import cn.mybatisboost.util.function.UncheckedFunction;
56
import cn.mybatisboost.util.function.UncheckedPredicate;
67
import org.apache.commons.lang3.StringUtils;
@@ -67,7 +68,10 @@ public static List<String> getProperties(Object entity, boolean selective) {
6768
properties = properties.stream()
6869
.map(UncheckedFunction.of(type::getDeclaredField))
6970
.peek(f -> f.setAccessible(true))
70-
.filter(UncheckedPredicate.of(f -> f.get(entity) != null))
71+
.filter(UncheckedPredicate.of(f -> {
72+
Object value = f.get(entity);
73+
return value != null && (!(value instanceof Property) || ((Property<?>) value).isPresent());
74+
}))
7175
.map(UncheckedFunction.of(Field::getName))
7276
.collect(Collectors.toList());
7377
}
@@ -84,7 +88,10 @@ public static List<String> getProperties(List<?> entities) {
8488
try {
8589
Field field = type.getDeclaredField(iterator.next());
8690
boolean nonNull = entities.stream()
87-
.anyMatch(UncheckedPredicate.of(it -> field.get(it) != null));
91+
.anyMatch(UncheckedPredicate.of(it -> {
92+
Object value = field.get(it);
93+
return value != null && (!(value instanceof Property) || ((Property<?>) value).isPresent());
94+
}));
8895
if (!nonNull) iterator.remove();
8996
} catch (NoSuchFieldException e) {
9097
throw new RuntimeException(e);

0 commit comments

Comments
 (0)