mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
This commit is contained in:
parent
d75144e57a
commit
516c6a24d6
@ -163,7 +163,7 @@ public class MPJInterceptor implements Interceptor {
|
||||
return result;
|
||||
}
|
||||
MPJLambdaWrapper wrapper = (MPJLambdaWrapper) obj;
|
||||
Map<String, Field> fieldMap = ReflectionKit.getFieldMap(resultType);
|
||||
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(resultType);
|
||||
List<Select> columnList = wrapper.getSelectColumns();
|
||||
//移除对多查询列,为了可重复使用wrapper
|
||||
columnList.removeIf(Select::isLabel);
|
||||
@ -175,7 +175,7 @@ public class MPJInterceptor implements Interceptor {
|
||||
columnSet.add(i.getAlias());
|
||||
if (Objects.nonNull(field)) {
|
||||
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getAlias(),
|
||||
i.getAlias(), field.getType());
|
||||
i.getAlias(), MPJReflectionKit.getFieldType(resultType, i.getAlias()));
|
||||
resultMappings.add(selectToResult(wrapper.getEntityClass(), i, field.getType(), builder));
|
||||
}
|
||||
} else {
|
||||
@ -183,7 +183,7 @@ public class MPJInterceptor implements Interceptor {
|
||||
columnSet.add(i.getTagColumn());
|
||||
if (Objects.nonNull(field)) {
|
||||
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getColumProperty(),
|
||||
i.getTagColumn(), field.getType());
|
||||
i.getTagColumn(), MPJReflectionKit.getFieldType(resultType, i.getColumProperty()));
|
||||
resultMappings.add(selectToResult(wrapper.getEntityClass(), i, field.getType(), builder));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.toolkit;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import org.apache.ibatis.reflection.Reflector;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
@ -22,6 +23,9 @@ public final class MPJReflectionKit {
|
||||
|
||||
private static final Map<Class<?>, Map<String, Field>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
//mybatis 缓存
|
||||
private static final Map<Class<?>, Reflector> CLASS_REFLECTOR_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Map<String, Field> EMPTY_MAP = new HashMap<>();
|
||||
|
||||
|
||||
@ -86,6 +90,18 @@ public final class MPJReflectionKit {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Class<?> getFieldType(Class<?> clazz, String name) {
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
Reflector reflector = CLASS_REFLECTOR_CACHE.computeIfAbsent(clazz, Reflector::new);
|
||||
Class<?> getterType = reflector.getGetterType(name);
|
||||
if (getterType != null) {
|
||||
return getterType;
|
||||
}
|
||||
return getFieldMap(clazz).get(name).getType();
|
||||
}
|
||||
|
||||
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
return (clazz.isPrimitive() || PRIMITIVE_WRAPPER_TYPE_MAP.containsKey(clazz));
|
||||
|
@ -0,0 +1,16 @@
|
||||
package com.github.yulichang.test.join.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ID<T extends Serializable> extends Model<ID<T>> {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@TableId
|
||||
private T id;
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.github.yulichang.test.join.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
@ -19,12 +18,9 @@ import java.util.Map;
|
||||
@Data
|
||||
@ToString
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName(value = "`user`", autoResultMap = true)
|
||||
public class UserDO implements Serializable {
|
||||
|
||||
@TableId
|
||||
private Integer id;
|
||||
public class UserDO extends ID<Integer> implements Serializable {
|
||||
|
||||
private Integer pid;
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
package com.github.yulichang.test.join.entity;
|
||||
|
||||
public class UserTTT extends UserDO{
|
||||
}
|
@ -6,10 +6,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.yulichang.test.join.dto.AddressDTO;
|
||||
import com.github.yulichang.test.join.dto.UserDTO;
|
||||
import com.github.yulichang.test.join.entity.AddressDO;
|
||||
import com.github.yulichang.test.join.entity.AreaDO;
|
||||
import com.github.yulichang.test.join.entity.UserDO;
|
||||
import com.github.yulichang.test.join.entity.UserDto;
|
||||
import com.github.yulichang.test.join.entity.*;
|
||||
import com.github.yulichang.test.join.mapper.UserDTOMapper;
|
||||
import com.github.yulichang.test.join.mapper.UserMapper;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
@ -684,4 +681,16 @@ class LambdaWrapperTest {
|
||||
List<UserDO> dos = userMapper.selectJoinList(UserDO.class, wrapper);
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 泛型测试
|
||||
*/
|
||||
@Test
|
||||
void testGeneric() {
|
||||
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.le(UserDO::getId, 10000)
|
||||
.orderByDesc(UserDO::getId);
|
||||
List<UserTTT> list = userMapper.selectJoinList(UserTTT.class, wrapper);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user