优化字段获取

This commit is contained in:
yulichang 2023-11-17 06:54:58 +08:00
parent 40006863f6
commit 7b33070fa4
7 changed files with 68 additions and 31 deletions

View File

@ -1,7 +1,7 @@
package com.github.yulichang.toolkit; package com.github.yulichang.toolkit;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects; import java.util.Locale;
/** /**
* @author yulichang * @author yulichang
@ -12,12 +12,11 @@ public class FieldStringMap<V> extends HashMap<String, V> {
@Override @Override
public V get(Object key) { public V get(Object key) {
V v = super.get(key); return super.get(((String) key).toUpperCase(Locale.ENGLISH));
if (Objects.isNull(v)) { }
String k = (String) key;
return entrySet().stream().filter(f -> k.equalsIgnoreCase(f.getKey())).findFirst() @Override
.map(Entry::getValue).orElse(null); public boolean containsKey(Object key) {
} return super.containsKey(((String) key).toUpperCase(Locale.ENGLISH));
return v;
} }
} }

View File

@ -2,32 +2,24 @@ package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.support.*; import com.github.yulichang.toolkit.support.IdeaProxyLambdaMeta;
import com.github.yulichang.toolkit.support.LambdaMeta;
import com.github.yulichang.toolkit.support.ReflectLambdaMeta;
import com.github.yulichang.toolkit.support.ShadowLambdaMeta;
import org.apache.ibatis.reflection.property.PropertyNamer; import org.apache.ibatis.reflection.property.PropertyNamer;
import java.lang.invoke.SerializedLambda; import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.Map;
/** /**
* copy {@link com.baomidou.mybatisplus.core.toolkit.LambdaUtils} * copy {@link com.baomidou.mybatisplus.core.toolkit.LambdaUtils}
*/ */
@SuppressWarnings("unused")
public final class LambdaUtils { public final class LambdaUtils {
public static <T> String getName(SFunction<T, ?> fn) { public static <T> String getName(SFunction<T, ?> fn) {
LambdaMeta extract = extract(fn); LambdaMeta extract = extract(fn);
String name = PropertyNamer.methodToProperty(extract.getImplMethodName()); return PropertyNamer.methodToProperty(extract.getImplMethodName());
if (Character.isUpperCase(name.charAt(0))) {
Map<String, FieldCache> map = MPJReflectionKit.getFieldMap(extract.getInstantiatedClass());
if (map.containsKey(name)) {
return name;
} else {
return map.keySet().stream().filter(i -> i.equalsIgnoreCase(name)).findFirst().orElse(null);
}
}
return name;
} }

View File

@ -78,7 +78,7 @@ public final class MPJReflectionKit {
*/ */
public static Map<String, FieldCache> getFieldMap(Class<?> clazz) { public static Map<String, FieldCache> getFieldMap(Class<?> clazz) {
return CLASS_FIELD_CACHE.computeIfAbsent(clazz, key -> getFieldList(key).stream().collect(Collectors.toMap(f -> return CLASS_FIELD_CACHE.computeIfAbsent(clazz, key -> getFieldList(key).stream().collect(Collectors.toMap(f ->
f.getField().getName(), Function.identity(), (o, n) -> n, FieldStringMap::new))); f.getField().getName().toUpperCase(Locale.ENGLISH), Function.identity(), (o, n) -> n, FieldStringMap::new)));
} }
public static List<FieldCache> getFieldList(Class<?> clazz) { public static List<FieldCache> getFieldList(Class<?> clazz) {

View File

@ -9,6 +9,7 @@ import com.github.yulichang.wrapper.segments.SelectCache;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
@ -43,6 +44,7 @@ public class ColumnCache {
public static Map<String, SelectCache> getMapField(Class<?> clazz) { public static Map<String, SelectCache> getMapField(Class<?> clazz) {
return MAP_CACHE.computeIfAbsent(clazz, c -> getListField(c).stream().collect(Collectors.toMap( return MAP_CACHE.computeIfAbsent(clazz, c -> getListField(c).stream().collect(Collectors.toMap(
SelectCache::getColumProperty, Function.identity(), (i, j) -> j, FieldStringMap::new))); i -> i.getColumProperty().toUpperCase(Locale.ENGLISH),
Function.identity(), (i, j) -> j, FieldStringMap::new)));
} }
} }

View File

@ -11,10 +11,7 @@ import com.github.yulichang.extension.mapping.mapper.MPJTableInfoHelper;
import com.github.yulichang.extension.mapping.wrapper.MappingQuery; import com.github.yulichang.extension.mapping.wrapper.MappingQuery;
import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.LambdaUtils;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -80,10 +77,10 @@ public class Relation {
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(entityClass); MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(entityClass);
if (tableInfo.isHasMappingOrField()) { if (tableInfo.isHasMappingOrField()) {
boolean hasProperty = CollectionUtils.isNotEmpty(config.getProperty()); boolean hasProperty = CollectionUtils.isNotEmpty(config.getProperty());
List<String> listProperty = hasProperty ? config.getProperty().stream().map(LambdaUtils::getName).collect( List<String> listProperty = hasProperty ? config.getProperty().stream().map(i ->
Collectors.toList()) : null; LambdaUtils.getName(i).toUpperCase(Locale.ENGLISH)).collect(Collectors.toList()) : null;
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) { for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
if (!hasProperty || listProperty.contains(fieldInfo.getProperty())) { if (!hasProperty || listProperty.contains(fieldInfo.getProperty().toUpperCase(Locale.ENGLISH))) {
List<Object> itemList = data.stream().map(fieldInfo::thisFieldGet).collect(Collectors.toList()); List<Object> itemList = data.stream().map(fieldInfo::thisFieldGet).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(itemList)) { if (CollectionUtils.isNotEmpty(itemList)) {
List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(), SqlKeyword.IN, List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(), SqlKeyword.IN,

View File

@ -26,7 +26,7 @@ public class AreaDO implements Serializable {
private String area; private String area;
private String postcode; private String Postcode;
@TableLogic @TableLogic
private Boolean del; private Boolean del;

View File

@ -0,0 +1,47 @@
package com.github.yulichang.test.join.m;
import com.github.yulichang.test.join.dto.AreaDTO;
import com.github.yulichang.test.join.entity.AreaDO;
import com.github.yulichang.test.join.entity.UserDto;
import com.github.yulichang.test.join.mapper.AreaMapper;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.toolkit.JoinWrappers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class FieldNameTest {
@Autowired
private AreaMapper areaMapper;
@BeforeEach
void setUp() {
Reset.reset();
}
@Test
void testFieldName() {
List<AreaDO> list = areaMapper.selectJoinList(AreaDO.class, JoinWrappers.lambda(AreaDO.class)
.select(AreaDO::getPostcode)
.leftJoin(UserDto.class,UserDto::getId,AreaDO::getId));
list.forEach(System.out::println);
assert list.get(0).getPostcode() != null;
}
@Test
void testFieldName1() {
List<AreaDTO> list = areaMapper.selectJoinList(AreaDTO.class, JoinWrappers.lambda(AreaDO.class)
.selectAs(AreaDO::getPostcode,AreaDTO::getPostcode)
.leftJoin(UserDto.class,UserDto::getId,AreaDO::getId));
list.forEach(System.out::println);
assert list.get(0).getPostcode() != null;
}
}