增加连表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> <modelVersion>4.0.0</modelVersion>
<groupId>com.github.yulichang</groupId> <groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join</artifactId> <artifactId>mybatis-plus-join</artifactId>
<version>1.2.5</version> <version>Gy.13.V.2.0.9</version>
<name>mybatis-plus-join</name> <name>mybatis-plus-join</name>
<description>An enhanced toolkit of Mybatis-Plus to simplify development.</description> <description>An enhanced toolkit of Mybatis-Plus to simplify development.</description>
<url>https://github.com/yulichang/mybatis-plus-join</url> <url>https://github.com/yulichang/mybatis-plus-join</url>

View File

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

View File

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