From a3f6eb12d0153d5a84847744cf922cc3a8d24a47 Mon Sep 17 00:00:00 2001
From: miemie
Date: Thu, 7 Nov 2024 16:00:59 +0800
Subject: [PATCH] =?UTF-8?q?TableName=E6=B3=A8=E8=A7=A3=E6=96=B0=E5=A2=9E`p?=
=?UTF-8?q?roperties`=E5=B1=9E=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../mybatisplus/annotation/TableName.java | 11 +++
.../core/metadata/TableInfoHelper.java | 77 ++++++++-----------
.../test/metadata/TableInfoHelperTest.java | 46 ++++++-----
3 files changed, 73 insertions(+), 61 deletions(-)
diff --git a/mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableName.java b/mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableName.java
index 789e0e73f6..6ca8af429d 100644
--- a/mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableName.java
+++ b/mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableName.java
@@ -67,8 +67,19 @@
*/
boolean autoResultMap() default false;
+ /**
+ * 只需要的属性名
+ *
+ * 与{@link #excludeProperty()} 二选一配置,都配置了则只有此配置生效
+ *
+ * @since 3.5.10
+ */
+ String[] properties() default {};
+
/**
* 需要排除的属性名
+ *
+ * 与{@link #properties()} 二选一配置,都配置了则{@link #properties()} 配置生效
*
* @since 3.3.1
*/
diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java
index 618779b7f9..f5a99cff79 100644
--- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java
+++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java
@@ -15,26 +15,12 @@
*/
package com.baomidou.mybatisplus.core.metadata;
-import com.baomidou.mybatisplus.annotation.DbType;
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.KeySequence;
-import com.baomidou.mybatisplus.annotation.OrderBy;
-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.annotation.*;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
-import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
-import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.*;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -50,12 +36,7 @@
import org.apache.ibatis.type.SimpleTypeRegistry;
import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.stream.Collectors.toList;
@@ -191,12 +172,10 @@ private static synchronized TableInfo initTableInfo(Configuration configuration,
tableInfo.setCurrentNamespace(currentNamespace);
/* 初始化表名相关 */
- final String[] excludeProperty = initTableName(clazz, globalConfig, tableInfo);
-
- List excludePropertyList = excludeProperty != null && excludeProperty.length > 0 ? Arrays.asList(excludeProperty) : Collections.emptyList();
+ PropertySelector propertySelector = initTableName(clazz, globalConfig, tableInfo);
/* 初始化字段相关 */
- initTableFields(configuration, clazz, globalConfig, tableInfo, excludePropertyList);
+ initTableFields(configuration, clazz, globalConfig, tableInfo, propertySelector);
/* 自动构建 resultMap */
tableInfo.initResultMapIfNeed();
@@ -219,7 +198,7 @@ private static synchronized TableInfo initTableInfo(Configuration configuration,
* @param tableInfo 数据库表反射信息
* @return 需要排除的字段名
*/
- private static String[] initTableName(Class> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
+ private static PropertySelector initTableName(Class> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
/* 数据库全局配置 */
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
@@ -229,8 +208,7 @@ private static String[] initTableName(Class> clazz, GlobalConfig globalConfig,
String tablePrefix = dbConfig.getTablePrefix();
String schema = dbConfig.getSchema();
boolean tablePrefixEffect = true;
- String[] excludeProperty = null;
-
+ PropertySelector propertySelector = i -> true;
if (table != null) {
if (StringUtils.isNotBlank(table.value())) {
tableName = PropertyParser.parse(table.value(), configuration.getVariables());
@@ -248,7 +226,15 @@ private static String[] initTableName(Class> clazz, GlobalConfig globalConfig,
tableInfo.setResultMap(table.resultMap());
}
tableInfo.setAutoInitResultMap(table.autoResultMap());
- excludeProperty = table.excludeProperty();
+ String[] ep = table.excludeProperty();
+ String[] ip = table.properties();
+ if (ArrayUtils.isNotEmpty(ip)) {
+ List list = Arrays.asList(ep);
+ propertySelector = list::contains;
+ } else if (ArrayUtils.isNotEmpty(ep)) {
+ List list = Arrays.asList(ep);
+ propertySelector = i -> !list.contains(i);
+ }
} else {
tableName = initTableNameWithDbConfig(tableName, dbConfig);
}
@@ -276,7 +262,7 @@ private static String[] initTableName(Class> clazz, GlobalConfig globalConfig,
if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
}
- return excludeProperty;
+ return propertySelector;
}
/**
@@ -311,7 +297,8 @@ private static String initTableNameWithDbConfig(String className, GlobalConfig.D
* @param globalConfig 全局配置
* @param tableInfo 数据库表反射信息
*/
- private static void initTableFields(Configuration configuration, Class> clazz, GlobalConfig globalConfig, TableInfo tableInfo, List excludeProperty) {
+ private static void initTableFields(Configuration configuration, Class> clazz, GlobalConfig globalConfig,
+ TableInfo tableInfo, PropertySelector propertySelector) {
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
Reflector reflector = tableInfo.getReflector();
@@ -325,7 +312,7 @@ private static void initTableFields(Configuration configuration, Class> clazz,
List fieldList = new ArrayList<>(list.size());
for (Field field : list) {
- if (excludeProperty.contains(field.getName())) {
+ if (!propertySelector.selection(field.getName())) {
continue;
}
@@ -384,7 +371,7 @@ private static void initTableFields(Configuration configuration, Class> clazz,
*
*
* @param clazz 实体类
- * @param list 字段列表
+ * @param list 字段列表
* @return true 为存在 {@link TableId} 注解;
*/
public static boolean isExistTableId(Class> clazz, List list) {
@@ -411,7 +398,7 @@ public static boolean isExistTableId(List list, AnnotationHandler annotat
*
*
* @param clazz 实体类
- * @param list 字段列表
+ * @param list 字段列表
* @return true 为存在 {@link TableLogic} 注解;
*/
public static boolean isExistTableLogic(Class> clazz, List list) {
@@ -438,7 +425,7 @@ public static boolean isExistTableLogic(List list, AnnotationHandler anno
*
*
* @param clazz 实体类
- * @param list 字段列表
+ * @param list 字段列表
* @return true 为存在 {@link OrderBy} 注解;
*/
public static boolean isExistOrderBy(Class> clazz, List list) {
@@ -452,7 +439,7 @@ public static boolean isExistOrderBy(Class> clazz, List list) {
* 判断排序注解是否存在
*
*
- * @param list 字段列表
+ * @param list 字段列表
* @param annotationHandler 注解处理类
* @return true 为存在 {@link OrderBy} 注解;
*/
@@ -465,10 +452,10 @@ public static boolean isExistOrderBy(List list, AnnotationHandler annotat
* 主键属性初始化
*
*
- * @param globalConfig 全局配置信息
- * @param tableInfo 表信息
- * @param field 字段
- * @param tableId 注解
+ * @param globalConfig 全局配置信息
+ * @param tableInfo 表信息
+ * @param field 字段
+ * @param tableId 注解
*/
private static void initTableIdWithAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field, TableId tableId) {
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
@@ -523,8 +510,8 @@ private static void initTableIdWithAnnotation(GlobalConfig globalConfig, TableIn
*
*
* @param globalConfig 全局配置
- * @param tableInfo 表信息
- * @param field 字段
+ * @param tableInfo 表信息
+ * @param field 字段
* @return true 继续下一个属性判断,返回 continue;
*/
private static boolean initTableIdWithoutAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field) {
@@ -644,4 +631,8 @@ public static KeyGenerator genKeyGenerator(String baseStatementId, TableInfo tab
return new SelectKeyGenerator(mappedStatement, true);
}
+ @FunctionalInterface
+ private interface PropertySelector {
+ boolean selection(String property);
+ }
}
diff --git a/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java b/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java
index 5501944c08..d9dce19c65 100644
--- a/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java
+++ b/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java
@@ -17,11 +17,7 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
+import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
@@ -77,6 +73,24 @@ private static class ModelFour extends BaseModel {
private String name;
}
+ @Test
+ void testExcludeProperty() {
+ TableInfo tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ModelThree.class);
+ assertThat(tableInfo.havePK()).isTrue();
+ assertThat(tableInfo.getKeyProperty()).isEqualTo("id");
+ assertThat(tableInfo.getFieldList().size()).isEqualTo(2);
+ assertThat(tableInfo.getFieldList()).noneMatch(i -> i.getProperty().equals("test"));
+
+ tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ModelFour.class);
+ assertThat(tableInfo.getFieldList().size()).isEqualTo(2);
+ assertThat(tableInfo.getFieldList()).noneMatch(i -> i.getProperty().equals("test"));
+
+ tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ModelFour2.class);
+ assertThat(tableInfo.getFieldList().size()).isEqualTo(1);
+ assertThat(tableInfo.getFieldList()).anyMatch(i -> i.getProperty().equals("name"));
+ assertThat(tableInfo.havePK()).isTrue();
+ }
+
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@@ -151,17 +165,14 @@ void testIsExistTableId() {
assertThat(TableInfoHelper.isExistTableId(ModelTwo.class, Arrays.asList(ModelTwo.class.getDeclaredFields()))).isFalse();
}
- @Test
- void testExcludeProperty() {
- TableInfo tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ModelThree.class);
- assertThat(tableInfo.havePK()).isTrue();
- assertThat(tableInfo.getKeyProperty()).isEqualTo("id");
- assertThat(tableInfo.getFieldList().size()).isEqualTo(2);
- assertThat(tableInfo.getFieldList()).noneMatch(i -> i.getProperty().equals("test"));
+ @Data
+ @EqualsAndHashCode(callSuper = true)
+ @TableName(properties = {"id", "name"}, excludeProperty = {"id", "name"})
+ private static class ModelFour2 extends BaseModel {
- tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ModelFour.class);
- assertThat(tableInfo.getFieldList().size()).isEqualTo(2);
- assertThat(tableInfo.getFieldList()).noneMatch(i -> i.getProperty().equals("test"));
+ private String sex;
+
+ private String name;
}
@Test
@@ -300,7 +311,6 @@ private static class Table2 {
}
-
@Test
void testTableAutoResultMap() {
MybatisConfiguration configuration = new MybatisConfiguration();
@@ -314,8 +324,8 @@ void testTableAutoResultMap() {
}
@Data
- @TableName(value = "xxx", autoResultMap = true )
- private static class AutoResultMapTable{
+ @TableName(value = "xxx", autoResultMap = true)
+ private static class AutoResultMapTable {
@TableId("`id`")
private Long id;