增加连表selectAsClass返回列按VO查询。

This commit is contained in:
王冰冰前男友 2022-08-10 13:29:49 +08:00
parent 914349c640
commit 2cb66ca22a
3 changed files with 27 additions and 21 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join</artifactId>
<version>1.2.5</version>
<version>Gy.13.V.2.0.9</version>
<name>mybatis-plus-join</name>
<description>An enhanced toolkit of Mybatis-Plus to simplify development.</description>
<url>https://github.com/yulichang/mybatis-plus-join</url>

View File

@ -1,6 +1,7 @@
package com.baomidou.mybatisplus.core.metadata;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
@ -28,7 +29,7 @@ public class MPJResultHelper {
/**
* 储存反射VO信息
*/
private static final Map<Class<?>, Set<String>> VO_INFO_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, Map<String, Set<String>>> VO_INFO_CACHE = new ConcurrentHashMap<>();
/**
@ -39,26 +40,30 @@ public class MPJResultHelper {
* @return: java.util.Set<java.lang.String>
* @Date: 2022/8/5 09:59
*/
public static Set<String> getVoTableInfo(Class<?> sourceEntityClass, Class<?> resultEntityClass) {
public static Map<String, Set<String>> getVoTableInfo(Class<?> resultEntityClass, Class<?>... sourceEntityClass) {
if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) {
return null;
}
Set<String> strings = VO_INFO_CACHE.get(resultEntityClass);
if (strings == null) {
Set<String> set = new HashSet<>();
MPJTableInfo info = MPJTableInfoHelper.getTableInfo(sourceEntityClass);
Assert.notNull(info, "table can not be find");
Map<String, Set<String>> maps = VO_INFO_CACHE.get(resultEntityClass);
if (maps == null) {
maps = CollectionUtils.newHashMap();
List<Field> allFields = TableInfoHelper.getAllFields(resultEntityClass);
Assert.notNull(allFields, "table can not be find");
Set<String> fieldNames = allFields.stream().collect(Collectors.groupingBy(Field::getName)).keySet();
info.getTableInfo().getFieldList().forEach(
i -> {
if (fieldNames.contains(i.getProperty())) {
set.add(i.getColumn());
}
});
for (Class<?> entityClass : sourceEntityClass) {
Set<String> set = new HashSet<>();
MPJTableInfo info = MPJTableInfoHelper.getTableInfo(entityClass);
Assert.notNull(info, "table can not be find");
info.getTableInfo().getFieldList().forEach(
i -> {
if (fieldNames.contains(i.getProperty())) {
set.add(i.getColumn());
}
});
maps.put(entityClass.getName(), set);
}
/* 添加缓存 */
VO_INFO_CACHE.put(resultEntityClass, set);
VO_INFO_CACHE.put(resultEntityClass, maps);
}
return VO_INFO_CACHE.get(resultEntityClass);
}

View File

@ -139,12 +139,13 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
return typedThis;
}
public <E> MPJLambdaWrapper<T> selectAsClass(Class<E> sourceEntityClass, Class<?> resultEntityClass) {
TableInfo info = TableInfoHelper.getTableInfo(sourceEntityClass);
Assert.notNull(info, "table can not be find");
Set<String> voTableInfo = MPJResultHelper.getVoTableInfo(sourceEntityClass, resultEntityClass);
Assert.notNull(info, "table can not be find");
voTableInfo.forEach(i -> selectColumns.add(SelectColumn.of(sourceEntityClass, i)));
public <E> MPJLambdaWrapper<T> selectAsClass(Class<?> resultEntityClass, Class<?>... sourceEntityClass) {
Map<String, Set<String>> voTableInfo = MPJResultHelper.getVoTableInfo(resultEntityClass, sourceEntityClass);
Assert.notNull(voTableInfo, "table can not be find");
for (Class<?> entityClass : sourceEntityClass) {
Set<String> columns = voTableInfo.get(entityClass.getName());
columns.forEach(i -> selectColumns.add(SelectColumn.of(entityClass, i)));
}
return typedThis;
}