Skip to content

Commit 77031d9

Browse files
author
zhangrongfan
committed
Replace Property with Optional
1 parent 686e43b commit 77031d9

File tree

4 files changed

+11
-113
lines changed

4 files changed

+11
-113
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package cn.mybatisboost.json;
22

3-
import cn.mybatisboost.support.Property;
43
import org.apache.ibatis.executor.resultset.ResultSetHandler;
54
import org.apache.ibatis.plugin.*;
65

76
import java.lang.reflect.Field;
87
import java.lang.reflect.ParameterizedType;
98
import java.sql.Statement;
10-
import java.util.Arrays;
11-
import java.util.Collections;
12-
import java.util.List;
13-
import java.util.Properties;
9+
import java.util.*;
1410
import java.util.concurrent.ConcurrentHashMap;
1511
import java.util.concurrent.ConcurrentMap;
1612
import java.util.stream.Collectors;
@@ -28,15 +24,15 @@ public Object intercept(Invocation invocation) throws Throwable {
2824
Class<?> type = proceed.get(0).getClass();
2925
List<Field> fields = fieldCache.computeIfAbsent(type,
3026
key -> Collections.unmodifiableList(Arrays.stream(type.getDeclaredFields())
31-
.filter(it -> it.getType() == Property.class)
27+
.filter(it -> it.getType() == Optional.class)
3228
.peek(it -> it.setAccessible(true)).collect(Collectors.toList())));
3329
if (fields.isEmpty()) return proceed;
3430

3531
for (Object object : proceed) {
3632
for (Field field : fields) {
37-
Object value = ((Property<?>) field.get(object)).orElse(null);
33+
Object value = ((Optional<?>) field.get(object)).orElse(null);
3834
if (value instanceof String) {
39-
field.set(object, Property.of(JsonTypeHandler.objectMapper.readValue((String) value,
35+
field.set(object, Optional.of(JsonTypeHandler.objectMapper.readValue((String) value,
4036
(Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0])));
4137
}
4238
}

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

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

3-
import cn.mybatisboost.support.Property;
43
import com.fasterxml.jackson.core.JsonProcessingException;
54
import com.fasterxml.jackson.databind.ObjectMapper;
65
import org.apache.ibatis.type.BaseTypeHandler;
@@ -11,8 +10,9 @@
1110
import java.sql.PreparedStatement;
1211
import java.sql.ResultSet;
1312
import java.sql.SQLException;
13+
import java.util.Optional;
1414

15-
@MappedTypes(Property.class)
15+
@MappedTypes(Optional.class)
1616
public class JsonTypeHandler extends BaseTypeHandler<Object> {
1717

1818
static ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
@@ -21,9 +21,9 @@ public class JsonTypeHandler extends BaseTypeHandler<Object> {
2121
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
2222
throws SQLException {
2323
try {
24-
Property<?> property = (Property<?>) parameter;
25-
if (property.isPresent()) {
26-
ps.setString(i, objectMapper.writeValueAsString(property.get()));
24+
Optional<?> optional = (Optional<?>) parameter;
25+
if (optional.isPresent()) {
26+
ps.setString(i, objectMapper.writeValueAsString(optional.get()));
2727
} else {
2828
ps.setObject(i, null);
2929
}

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

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

33
import cn.mybatisboost.core.adaptor.NameAdaptor;
4-
import cn.mybatisboost.support.Property;
54
import cn.mybatisboost.util.function.UncheckedFunction;
65
import cn.mybatisboost.util.function.UncheckedPredicate;
76
import org.apache.commons.lang3.StringUtils;
@@ -68,10 +67,7 @@ public static List<String> getProperties(Object entity, boolean selective) {
6867
properties = properties.stream()
6968
.map(UncheckedFunction.of(type::getDeclaredField))
7069
.peek(f -> f.setAccessible(true))
71-
.filter(UncheckedPredicate.of(f -> {
72-
Object value = f.get(entity);
73-
return value != null && (!(value instanceof Property) || ((Property<?>) value).isPresent());
74-
}))
70+
.filter(UncheckedPredicate.of(f -> f.get(entity) != null))
7571
.map(UncheckedFunction.of(Field::getName))
7672
.collect(Collectors.toList());
7773
}
@@ -88,10 +84,7 @@ public static List<String> getProperties(List<?> entities) {
8884
try {
8985
Field field = type.getDeclaredField(iterator.next());
9086
boolean nonNull = entities.stream()
91-
.anyMatch(UncheckedPredicate.of(it -> {
92-
Object value = field.get(it);
93-
return value != null && (!(value instanceof Property) || ((Property<?>) value).isPresent());
94-
}));
87+
.anyMatch(UncheckedPredicate.of(it -> field.get(it) != null));
9588
if (!nonNull) iterator.remove();
9689
} catch (NoSuchFieldException e) {
9790
throw new RuntimeException(e);

0 commit comments

Comments
 (0)