支持返回基本数据类型

This commit is contained in:
yulichang 2022-11-28 17:40:59 +08:00
parent 6d20f61496
commit ed2fe9c93d
3 changed files with 49 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.method.MPJResultType; import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.toolkit.support.SelectColumn;
import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.resultmap.MybatisLabel; import com.github.yulichang.wrapper.resultmap.MybatisLabel;
@ -148,6 +149,10 @@ public class MPJInterceptor implements Interceptor {
List<ResultMap> result = new ArrayList<>(); List<ResultMap> result = new ArrayList<>();
TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType); TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType);
String id = ms.getId() + StringPool.DOT + Constants.MYBATIS_PLUS + StringPool.UNDERSCORE + resultType.getName(); String id = ms.getId() + StringPool.DOT + Constants.MYBATIS_PLUS + StringPool.UNDERSCORE + resultType.getName();
//基本数据类型
if (MPJReflectionKit.isPrimitiveOrWrapper(resultType)) {
return Collections.singletonList(new ResultMap.Builder(ms.getConfiguration(), id, resultType, EMPTY_RESULT_MAPPING).build());
}
if (!(obj instanceof MPJLambdaWrapper) || Map.class.isAssignableFrom(resultType) || if (!(obj instanceof MPJLambdaWrapper) || Map.class.isAssignableFrom(resultType) ||
Collection.class.isAssignableFrom(resultType)) { Collection.class.isAssignableFrom(resultType)) {
result.add(getDefaultResultMap(tableInfo, ms, resultType, id)); result.add(getDefaultResultMap(tableInfo, ms, resultType, id));

View File

@ -1,10 +1,13 @@
package com.github.yulichang.toolkit; package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.lang.reflect.WildcardType; import java.lang.reflect.WildcardType;
import java.util.HashMap; import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -21,6 +24,28 @@ public final class MPJReflectionKit {
private static final Map<String, Field> EMPTY_MAP = new HashMap<>(); private static final Map<String, Field> EMPTY_MAP = new HashMap<>();
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
private static final Map<Class<?>, Class<?>> PRIMITIVE_WRAPPER_TYPE_MAP = new IdentityHashMap<>(8);
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPE_TO_WRAPPER_MAP = new IdentityHashMap<>(8);
static {
PRIMITIVE_WRAPPER_TYPE_MAP.put(Boolean.class, boolean.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Byte.class, byte.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Character.class, char.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Double.class, double.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Float.class, float.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Integer.class, int.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Long.class, long.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Short.class, short.class);
for (Map.Entry<Class<?>, Class<?>> entry : PRIMITIVE_WRAPPER_TYPE_MAP.entrySet()) {
PRIMITIVE_TYPE_TO_WRAPPER_MAP.put(entry.getValue(), entry.getKey());
}
}
/** /**
* Collection字段的泛型 * Collection字段的泛型
*/ */
@ -59,4 +84,9 @@ public final class MPJReflectionKit {
CLASS_FIELD_CACHE.put(clazz, map); CLASS_FIELD_CACHE.put(clazz, map);
return map; return map;
} }
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() || PRIMITIVE_WRAPPER_TYPE_MAP.containsKey(clazz));
}
} }

View File

@ -45,6 +45,20 @@ class LambdaWrapperTest {
list.forEach(System.out::println); list.forEach(System.out::println);
} }
/**
* 基本数据类型测试
*/
@Test
void testWrapper() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
.select(UserDO::getId)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId);
List<Integer> list = userMapper.selectJoinList(Integer.class, wrapper);
System.out.println(list);
}
/** /**
* 简单的分页关联查询 lambda * 简单的分页关联查询 lambda