TableName注解新增properties
属性
This commit is contained in:
parent
549d4ea598
commit
a3f6eb12d0
@ -67,8 +67,19 @@ public @interface TableName {
|
||||
*/
|
||||
boolean autoResultMap() default false;
|
||||
|
||||
/**
|
||||
* 只需要的属性名
|
||||
* <p>
|
||||
* 与{@link #excludeProperty()} 二选一配置,都配置了则只有此配置生效
|
||||
*
|
||||
* @since 3.5.10
|
||||
*/
|
||||
String[] properties() default {};
|
||||
|
||||
/**
|
||||
* 需要排除的属性名
|
||||
* <p>
|
||||
* 与{@link #properties()} 二选一配置,都配置了则{@link #properties()} 配置生效
|
||||
*
|
||||
* @since 3.3.1
|
||||
*/
|
||||
|
@ -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.session.Configuration;
|
||||
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 @@ public class TableInfoHelper {
|
||||
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();
|
||||
@ -219,7 +198,7 @@ public class TableInfoHelper {
|
||||
* @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 @@ public class TableInfoHelper {
|
||||
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 @@ public class TableInfoHelper {
|
||||
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);
|
||||
}
|
||||
@ -276,7 +262,7 @@ public class TableInfoHelper {
|
||||
if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
|
||||
tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
|
||||
}
|
||||
return excludeProperty;
|
||||
return propertySelector;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -311,7 +297,8 @@ public class TableInfoHelper {
|
||||
* @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();
|
||||
@ -325,7 +312,7 @@ public class TableInfoHelper {
|
||||
|
||||
List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
|
||||
for (Field field : list) {
|
||||
if (excludeProperty.contains(field.getName())) {
|
||||
if (!propertySelector.selection(field.getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -384,7 +371,7 @@ public class TableInfoHelper {
|
||||
* </p>
|
||||
*
|
||||
* @param clazz 实体类
|
||||
* @param list 字段列表
|
||||
* @param list 字段列表
|
||||
* @return true 为存在 {@link TableId} 注解;
|
||||
*/
|
||||
public static boolean isExistTableId(Class<?> clazz, List<Field> list) {
|
||||
@ -411,7 +398,7 @@ public class TableInfoHelper {
|
||||
* </p>
|
||||
*
|
||||
* @param clazz 实体类
|
||||
* @param list 字段列表
|
||||
* @param list 字段列表
|
||||
* @return true 为存在 {@link TableLogic} 注解;
|
||||
*/
|
||||
public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
|
||||
@ -438,7 +425,7 @@ public class TableInfoHelper {
|
||||
* </p>
|
||||
*
|
||||
* @param clazz 实体类
|
||||
* @param list 字段列表
|
||||
* @param list 字段列表
|
||||
* @return true 为存在 {@link OrderBy} 注解;
|
||||
*/
|
||||
public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
|
||||
@ -452,7 +439,7 @@ public class TableInfoHelper {
|
||||
* 判断排序注解是否存在
|
||||
* </p>
|
||||
*
|
||||
* @param list 字段列表
|
||||
* @param list 字段列表
|
||||
* @param annotationHandler 注解处理类
|
||||
* @return true 为存在 {@link OrderBy} 注解;
|
||||
*/
|
||||
@ -465,10 +452,10 @@ public class TableInfoHelper {
|
||||
* 主键属性初始化
|
||||
* </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();
|
||||
@ -523,8 +510,8 @@ public class TableInfoHelper {
|
||||
* </p>
|
||||
*
|
||||
* @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 class TableInfoHelper {
|
||||
return new SelectKeyGenerator(mappedStatement, true);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface PropertySelector {
|
||||
boolean selection(String property);
|
||||
}
|
||||
}
|
||||
|
@ -17,11 +17,7 @@ import org.apache.ibatis.mapping.ResultMap;
|
||||
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 @@ class TableInfoHelperTest {
|
||||
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 @@ class TableInfoHelperTest {
|
||||
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 @@ class TableInfoHelperTest {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testTableAutoResultMap() {
|
||||
MybatisConfiguration configuration = new MybatisConfiguration();
|
||||
@ -314,8 +324,8 @@ class TableInfoHelperTest {
|
||||
}
|
||||
|
||||
@Data
|
||||
@TableName(value = "xxx", autoResultMap = true )
|
||||
private static class AutoResultMapTable{
|
||||
@TableName(value = "xxx", autoResultMap = true)
|
||||
private static class AutoResultMapTable {
|
||||
|
||||
@TableId("`id`")
|
||||
private Long id;
|
||||
|
Loading…
x
Reference in New Issue
Block a user