diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 63db8b4..8a35340 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.toolkit.*; import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.toolkit.MPJReflectionKit; import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.wrapper.resultmap.MybatisLabel; @@ -148,6 +149,10 @@ public class MPJInterceptor implements Interceptor { List result = new ArrayList<>(); TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType); 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) || Collection.class.isAssignableFrom(resultType)) { result.add(getDefaultResultMap(tableInfo, ms, resultType, id)); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java index 1f923e4..772488b 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java @@ -1,10 +1,13 @@ package com.github.yulichang.toolkit; +import com.baomidou.mybatisplus.core.toolkit.Assert; + import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -21,6 +24,28 @@ public final class MPJReflectionKit { private static final Map EMPTY_MAP = new HashMap<>(); + + @Deprecated + @SuppressWarnings("DeprecatedIsStillUsed") + private static final Map, Class> PRIMITIVE_WRAPPER_TYPE_MAP = new IdentityHashMap<>(8); + + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") + private static final Map, 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> entry : PRIMITIVE_WRAPPER_TYPE_MAP.entrySet()) { + PRIMITIVE_TYPE_TO_WRAPPER_MAP.put(entry.getValue(), entry.getKey()); + } + } + /** * Collection字段的泛型 */ @@ -59,4 +84,9 @@ public final class MPJReflectionKit { CLASS_FIELD_CACHE.put(clazz, 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)); + } } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java index 6b46a95..e45f746 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java @@ -45,6 +45,20 @@ class LambdaWrapperTest { list.forEach(System.out::println); } + /** + * 基本数据类型测试 + */ + @Test + void testWrapper() { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .select(UserDO::getId) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); + List list = userMapper.selectJoinList(Integer.class, wrapper); + + System.out.println(list); + } + /** * 简单的分页关联查询 lambda