From 2cb66ca22a35e7676262d3deb2cd53b8740c4914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Wed, 10 Aug 2022 13:29:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=9E=E8=A1=A8selectAsCla?= =?UTF-8?q?ss=E8=BF=94=E5=9B=9E=E5=88=97=E6=8C=89VO=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../core/metadata/MPJResultHelper.java | 33 +++++++++++-------- .../yulichang/wrapper/MPJLambdaWrapper.java | 13 ++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 0d58016..74851ba 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.github.yulichang mybatis-plus-join - 1.2.5 + Gy.13.V.2.0.9 mybatis-plus-join An enhanced toolkit of Mybatis-Plus to simplify development. https://github.com/yulichang/mybatis-plus-join diff --git a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java index d02be92..fb1aaa7 100644 --- a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java @@ -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, Set> VO_INFO_CACHE = new ConcurrentHashMap<>(); + private static final Map, Map>> VO_INFO_CACHE = new ConcurrentHashMap<>(); /** @@ -39,26 +40,30 @@ public class MPJResultHelper { * @return: java.util.Set * @Date: 2022/8/5 09:59 */ - public static Set getVoTableInfo(Class sourceEntityClass, Class resultEntityClass) { + public static Map> getVoTableInfo(Class resultEntityClass, Class... sourceEntityClass) { if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) { return null; } - Set strings = VO_INFO_CACHE.get(resultEntityClass); - if (strings == null) { - Set set = new HashSet<>(); - MPJTableInfo info = MPJTableInfoHelper.getTableInfo(sourceEntityClass); - Assert.notNull(info, "table can not be find"); + Map> maps = VO_INFO_CACHE.get(resultEntityClass); + if (maps == null) { + maps = CollectionUtils.newHashMap(); List allFields = TableInfoHelper.getAllFields(resultEntityClass); Assert.notNull(allFields, "table can not be find"); Set 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 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); } diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 539415d..95bcbea 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -139,12 +139,13 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAsClass(Class sourceEntityClass, Class resultEntityClass) { - TableInfo info = TableInfoHelper.getTableInfo(sourceEntityClass); - Assert.notNull(info, "table can not be find"); - Set voTableInfo = MPJResultHelper.getVoTableInfo(sourceEntityClass, resultEntityClass); - Assert.notNull(info, "table can not be find"); - voTableInfo.forEach(i -> selectColumns.add(SelectColumn.of(sourceEntityClass, i))); + public MPJLambdaWrapper selectAsClass(Class resultEntityClass, Class... sourceEntityClass) { + Map> voTableInfo = MPJResultHelper.getVoTableInfo(resultEntityClass, sourceEntityClass); + Assert.notNull(voTableInfo, "table can not be find"); + for (Class entityClass : sourceEntityClass) { + Set columns = voTableInfo.get(entityClass.getName()); + columns.forEach(i -> selectColumns.add(SelectColumn.of(entityClass, i))); + } return typedThis; }