|
@@ -15,26 +15,12 @@
|
|
*/
|
|
*/
|
|
package com.baomidou.mybatisplus.core.metadata;
|
|
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.config.GlobalConfig;
|
|
import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
|
|
import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
|
|
import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
|
|
import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
|
|
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
|
|
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.MapperBuilderAssistant;
|
|
import org.apache.ibatis.builder.StaticSqlSource;
|
|
import org.apache.ibatis.builder.StaticSqlSource;
|
|
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
|
@@ -50,12 +36,7 @@ import org.apache.ibatis.session.Configuration;
|
|
import org.apache.ibatis.type.SimpleTypeRegistry;
|
|
import org.apache.ibatis.type.SimpleTypeRegistry;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
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 java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
import static java.util.stream.Collectors.toList;
|
|
@@ -191,12 +172,10 @@ public class TableInfoHelper {
|
|
tableInfo.setCurrentNamespace(currentNamespace);
|
|
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 */
|
|
/* 自动构建 resultMap */
|
|
tableInfo.initResultMapIfNeed();
|
|
tableInfo.initResultMapIfNeed();
|
|
@@ -219,7 +198,7 @@ public class TableInfoHelper {
|
|
* @param tableInfo 数据库表反射信息
|
|
* @param tableInfo 数据库表反射信息
|
|
* @return 需要排除的字段名
|
|
* @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();
|
|
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
|
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
|
@@ -229,8 +208,7 @@ public class TableInfoHelper {
|
|
String tablePrefix = dbConfig.getTablePrefix();
|
|
String tablePrefix = dbConfig.getTablePrefix();
|
|
String schema = dbConfig.getSchema();
|
|
String schema = dbConfig.getSchema();
|
|
boolean tablePrefixEffect = true;
|
|
boolean tablePrefixEffect = true;
|
|
- String[] excludeProperty = null;
|
|
|
|
-
|
|
|
|
|
|
+ PropertySelector propertySelector = i -> true;
|
|
if (table != null) {
|
|
if (table != null) {
|
|
if (StringUtils.isNotBlank(table.value())) {
|
|
if (StringUtils.isNotBlank(table.value())) {
|
|
tableName = PropertyParser.parse(table.value(), configuration.getVariables());
|
|
tableName = PropertyParser.parse(table.value(), configuration.getVariables());
|
|
@@ -248,7 +226,15 @@ public class TableInfoHelper {
|
|
tableInfo.setResultMap(table.resultMap());
|
|
tableInfo.setResultMap(table.resultMap());
|
|
}
|
|
}
|
|
tableInfo.setAutoInitResultMap(table.autoResultMap());
|
|
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 {
|
|
} else {
|
|
tableName = initTableNameWithDbConfig(tableName, dbConfig);
|
|
tableName = initTableNameWithDbConfig(tableName, dbConfig);
|
|
}
|
|
}
|
|
@@ -276,7 +262,7 @@ public class TableInfoHelper {
|
|
if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
|
|
if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
|
|
tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
|
|
tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
|
|
}
|
|
}
|
|
- return excludeProperty;
|
|
|
|
|
|
+ return propertySelector;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -311,7 +297,8 @@ public class TableInfoHelper {
|
|
* @param globalConfig 全局配置
|
|
* @param globalConfig 全局配置
|
|
* @param tableInfo 数据库表反射信息
|
|
* @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();
|
|
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
|
PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
|
|
PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
|
|
Reflector reflector = tableInfo.getReflector();
|
|
Reflector reflector = tableInfo.getReflector();
|
|
@@ -325,7 +312,7 @@ public class TableInfoHelper {
|
|
|
|
|
|
List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
|
|
List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
|
|
for (Field field : list) {
|
|
for (Field field : list) {
|
|
- if (excludeProperty.contains(field.getName())) {
|
|
|
|
|
|
+ if (!propertySelector.selection(field.getName())) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -384,7 +371,7 @@ public class TableInfoHelper {
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
* @param clazz 实体类
|
|
* @param clazz 实体类
|
|
- * @param list 字段列表
|
|
|
|
|
|
+ * @param list 字段列表
|
|
* @return true 为存在 {@link TableId} 注解;
|
|
* @return true 为存在 {@link TableId} 注解;
|
|
*/
|
|
*/
|
|
public static boolean isExistTableId(Class<?> clazz, List<Field> list) {
|
|
public static boolean isExistTableId(Class<?> clazz, List<Field> list) {
|
|
@@ -411,7 +398,7 @@ public class TableInfoHelper {
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
* @param clazz 实体类
|
|
* @param clazz 实体类
|
|
- * @param list 字段列表
|
|
|
|
|
|
+ * @param list 字段列表
|
|
* @return true 为存在 {@link TableLogic} 注解;
|
|
* @return true 为存在 {@link TableLogic} 注解;
|
|
*/
|
|
*/
|
|
public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
|
|
public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
|
|
@@ -438,7 +425,7 @@ public class TableInfoHelper {
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
* @param clazz 实体类
|
|
* @param clazz 实体类
|
|
- * @param list 字段列表
|
|
|
|
|
|
+ * @param list 字段列表
|
|
* @return true 为存在 {@link OrderBy} 注解;
|
|
* @return true 为存在 {@link OrderBy} 注解;
|
|
*/
|
|
*/
|
|
public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
|
|
public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
|
|
@@ -452,7 +439,7 @@ public class TableInfoHelper {
|
|
* 判断排序注解是否存在
|
|
* 判断排序注解是否存在
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
- * @param list 字段列表
|
|
|
|
|
|
+ * @param list 字段列表
|
|
* @param annotationHandler 注解处理类
|
|
* @param annotationHandler 注解处理类
|
|
* @return true 为存在 {@link OrderBy} 注解;
|
|
* @return true 为存在 {@link OrderBy} 注解;
|
|
*/
|
|
*/
|
|
@@ -465,10 +452,10 @@ public class TableInfoHelper {
|
|
* 主键属性初始化
|
|
* 主键属性初始化
|
|
* </p>
|
|
* </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) {
|
|
private static void initTableIdWithAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field, TableId tableId) {
|
|
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
@@ -523,8 +510,8 @@ public class TableInfoHelper {
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
* @param globalConfig 全局配置
|
|
* @param globalConfig 全局配置
|
|
- * @param tableInfo 表信息
|
|
|
|
- * @param field 字段
|
|
|
|
|
|
+ * @param tableInfo 表信息
|
|
|
|
+ * @param field 字段
|
|
* @return true 继续下一个属性判断,返回 continue;
|
|
* @return true 继续下一个属性判断,返回 continue;
|
|
*/
|
|
*/
|
|
private static boolean initTableIdWithoutAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field) {
|
|
private static boolean initTableIdWithoutAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field) {
|
|
@@ -644,4 +631,8 @@ public class TableInfoHelper {
|
|
return new SelectKeyGenerator(mappedStatement, true);
|
|
return new SelectKeyGenerator(mappedStatement, true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @FunctionalInterface
|
|
|
|
+ private interface PropertySelector {
|
|
|
|
+ boolean selection(String property);
|
|
|
|
+ }
|
|
}
|
|
}
|