Skip to content

Commit

Permalink
TableName注解新增properties属性
Browse files Browse the repository at this point in the history
  • Loading branch information
miemieYaho committed Nov 7, 2024
1 parent 549d4ea commit a3f6eb1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@
*/
boolean autoResultMap() default false;

/**
* 只需要的属性名
* <p>
* 与{@link #excludeProperty()} 二选一配置,都配置了则只有此配置生效
*
* @since 3.5.10
*/
String[] properties() default {};

/**
* 需要排除的属性名
* <p>
* 与{@link #properties()} 二选一配置,都配置了则{@link #properties()} 配置生效
*
* @since 3.3.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -191,12 +172,10 @@ private static synchronized TableInfo initTableInfo(Configuration configuration,
tableInfo.setCurrentNamespace(currentNamespace);

/* 初始化表名相关 */
final String[] excludeProperty = initTableName(clazz, globalConfig, tableInfo);

List<String> 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();
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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<String> list = Arrays.asList(ep);
propertySelector = list::contains;
} else if (ArrayUtils.isNotEmpty(ep)) {
List<String> list = Arrays.asList(ep);
propertySelector = i -> !list.contains(i);
}
} else {
tableName = initTableNameWithDbConfig(tableName, dbConfig);
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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<String> 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();
Expand All @@ -325,7 +312,7 @@ private static void initTableFields(Configuration configuration, Class<?> clazz,

List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
for (Field field : list) {
if (excludeProperty.contains(field.getName())) {
if (!propertySelector.selection(field.getName())) {
continue;
}

Expand Down Expand Up @@ -384,7 +371,7 @@ private static void initTableFields(Configuration configuration, Class<?> clazz,
* </p>
*
* @param clazz 实体类
* @param list 字段列表
* @param list 字段列表
* @return true 为存在 {@link TableId} 注解;
*/
public static boolean isExistTableId(Class<?> clazz, List<Field> list) {
Expand All @@ -411,7 +398,7 @@ public static boolean isExistTableId(List<Field> list, AnnotationHandler annotat
* </p>
*
* @param clazz 实体类
* @param list 字段列表
* @param list 字段列表
* @return true 为存在 {@link TableLogic} 注解;
*/
public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
Expand All @@ -438,7 +425,7 @@ public static boolean isExistTableLogic(List<Field> list, AnnotationHandler anno
* </p>
*
* @param clazz 实体类
* @param list 字段列表
* @param list 字段列表
* @return true 为存在 {@link OrderBy} 注解;
*/
public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
Expand All @@ -452,7 +439,7 @@ public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
* 判断排序注解是否存在
* </p>
*
* @param list 字段列表
* @param list 字段列表
* @param annotationHandler 注解处理类
* @return true 为存在 {@link OrderBy} 注解;
*/
Expand All @@ -465,10 +452,10 @@ public static boolean isExistOrderBy(List<Field> list, AnnotationHandler annotat
* 主键属性初始化
* </p>
*
* @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();
Expand Down Expand Up @@ -523,8 +510,8 @@ private static void initTableIdWithAnnotation(GlobalConfig globalConfig, TableIn
* </p>
*
* @param globalConfig 全局配置
* @param tableInfo 表信息
* @param field 字段
* @param tableInfo 表信息
* @param field 字段
* @return true 继续下一个属性判断,返回 continue;
*/
private static boolean initTableIdWithoutAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -300,7 +311,6 @@ private static class Table2 {
}



@Test
void testTableAutoResultMap() {
MybatisConfiguration configuration = new MybatisConfiguration();
Expand All @@ -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;
Expand Down

0 comments on commit a3f6eb1

Please sign in to comment.