|
@@ -15,11 +15,26 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.core.metadata;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.annotation.*;
|
|
|
+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.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.*;
|
|
|
+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 org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
import org.apache.ibatis.builder.StaticSqlSource;
|
|
|
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
|
@@ -34,7 +49,12 @@ import org.apache.ibatis.session.Configuration;
|
|
|
import org.apache.ibatis.type.SimpleTypeRegistry;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
-import java.util.*;
|
|
|
+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.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
@@ -139,7 +159,7 @@ public class TableInfoHelper {
|
|
|
* @param clazz 反射实体类
|
|
|
* @return 数据库表反射信息
|
|
|
*/
|
|
|
- public synchronized static TableInfo initTableInfo(MapperBuilderAssistant builderAssistant, Class<?> clazz) {
|
|
|
+ public static synchronized TableInfo initTableInfo(MapperBuilderAssistant builderAssistant, Class<?> clazz) {
|
|
|
TableInfo targetTableInfo = TABLE_INFO_CACHE.get(clazz);
|
|
|
final Configuration configuration = builderAssistant.getConfiguration();
|
|
|
if (targetTableInfo != null) {
|
|
@@ -161,7 +181,7 @@ public class TableInfoHelper {
|
|
|
* @param clazz 反射实体类
|
|
|
* @return 数据库表反射信息
|
|
|
*/
|
|
|
- private synchronized static TableInfo initTableInfo(Configuration configuration, String currentNamespace, Class<?> clazz) {
|
|
|
+ private static synchronized TableInfo initTableInfo(Configuration configuration, String currentNamespace, Class<?> clazz) {
|
|
|
/* 没有获取到缓存信息,则初始化 */
|
|
|
TableInfo tableInfo = new TableInfo(configuration, clazz);
|
|
|
tableInfo.setCurrentNamespace(currentNamespace);
|
|
@@ -199,7 +219,8 @@ public class TableInfoHelper {
|
|
|
private static String[] initTableName(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
|
|
|
/* 数据库全局配置 */
|
|
|
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
- TableName table = clazz.getAnnotation(TableName.class);
|
|
|
+ AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
|
|
+ TableName table = annotationHandler.getAnnotation(clazz, TableName.class);
|
|
|
|
|
|
String tableName = clazz.getSimpleName();
|
|
|
String tablePrefix = dbConfig.getTablePrefix();
|
|
@@ -241,7 +262,7 @@ public class TableInfoHelper {
|
|
|
|
|
|
/* 开启了自定义 KEY 生成器 */
|
|
|
if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
|
|
|
- tableInfo.setKeySequence(clazz.getAnnotation(KeySequence.class));
|
|
|
+ tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
|
|
|
}
|
|
|
return excludeProperty;
|
|
|
}
|
|
@@ -279,17 +300,16 @@ public class TableInfoHelper {
|
|
|
* @param tableInfo 数据库表反射信息
|
|
|
*/
|
|
|
private static void initTableFields(Configuration configuration, Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo, List<String> excludeProperty) {
|
|
|
- /* 数据库全局配置 */
|
|
|
- GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
+ AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
|
|
PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
|
|
|
Reflector reflector = tableInfo.getReflector();
|
|
|
- List<Field> list = getAllFields(clazz);
|
|
|
+ List<Field> list = getAllFields(clazz, annotationHandler);
|
|
|
// 标记是否读取到主键
|
|
|
boolean isReadPK = false;
|
|
|
// 是否存在 @TableId 注解
|
|
|
- boolean existTableId = isExistTableId(list);
|
|
|
+ boolean existTableId = isExistTableId(list, annotationHandler);
|
|
|
// 是否存在 @TableLogic 注解
|
|
|
- boolean existTableLogic = isExistTableLogic(list);
|
|
|
+ boolean existTableLogic = isExistTableLogic(list, annotationHandler);
|
|
|
|
|
|
List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
|
|
|
for (Field field : list) {
|
|
@@ -298,43 +318,42 @@ public class TableInfoHelper {
|
|
|
}
|
|
|
|
|
|
boolean isPK = false;
|
|
|
- boolean isOrderBy = field.getAnnotation(OrderBy.class) != null;
|
|
|
+ boolean isOrderBy = annotationHandler.getAnnotation(field, OrderBy.class) != null;
|
|
|
|
|
|
/* 主键ID 初始化 */
|
|
|
if (existTableId) {
|
|
|
- TableId tableId = field.getAnnotation(TableId.class);
|
|
|
+ TableId tableId = annotationHandler.getAnnotation(field, TableId.class);
|
|
|
if (tableId != null) {
|
|
|
if (isReadPK) {
|
|
|
throw ExceptionUtils.mpe("@TableId can't more than one in Class: \"%s\".", clazz.getName());
|
|
|
}
|
|
|
|
|
|
- initTableIdWithAnnotation(dbConfig, tableInfo, field, tableId);
|
|
|
+ initTableIdWithAnnotation(globalConfig, tableInfo, field, tableId);
|
|
|
isPK = isReadPK = true;
|
|
|
}
|
|
|
} else if (!isReadPK) {
|
|
|
- isPK = isReadPK = initTableIdWithoutAnnotation(dbConfig, tableInfo, field);
|
|
|
-
|
|
|
+ isPK = isReadPK = initTableIdWithoutAnnotation(globalConfig, tableInfo, field);
|
|
|
}
|
|
|
|
|
|
if (isPK) {
|
|
|
if (isOrderBy) {
|
|
|
- tableInfo.getOrderByFields().add(new TableFieldInfo(dbConfig, tableInfo, field, reflector, existTableLogic, true));
|
|
|
+ tableInfo.getOrderByFields().add(new TableFieldInfo(globalConfig, tableInfo, field, reflector, existTableLogic, true));
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- final TableField tableField = field.getAnnotation(TableField.class);
|
|
|
+ final TableField tableField = annotationHandler.getAnnotation(field, TableField.class);
|
|
|
|
|
|
/* 有 @TableField 注解的字段初始化 */
|
|
|
if (tableField != null) {
|
|
|
- TableFieldInfo tableFieldInfo = new TableFieldInfo(dbConfig, tableInfo, field, tableField, reflector, existTableLogic, isOrderBy);
|
|
|
+ TableFieldInfo tableFieldInfo = new TableFieldInfo(globalConfig, tableInfo, field, tableField, reflector, existTableLogic, isOrderBy);
|
|
|
fieldList.add(tableFieldInfo);
|
|
|
postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
/* 无 @TableField 注解的字段初始化 */
|
|
|
- TableFieldInfo tableFieldInfo = new TableFieldInfo(dbConfig, tableInfo, field, reflector, existTableLogic, isOrderBy);
|
|
|
+ TableFieldInfo tableFieldInfo = new TableFieldInfo(globalConfig, tableInfo, field, reflector, existTableLogic, isOrderBy);
|
|
|
fieldList.add(tableFieldInfo);
|
|
|
postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
|
|
|
}
|
|
@@ -353,11 +372,41 @@ public class TableInfoHelper {
|
|
|
* 判断主键注解是否存在
|
|
|
* </p>
|
|
|
*
|
|
|
+ * @param clazz 实体类
|
|
|
* @param list 字段列表
|
|
|
* @return true 为存在 {@link TableId} 注解;
|
|
|
*/
|
|
|
- public static boolean isExistTableId(List<Field> list) {
|
|
|
- return list.stream().anyMatch(field -> field.isAnnotationPresent(TableId.class));
|
|
|
+ public static boolean isExistTableId(Class<?> clazz, List<Field> list) {
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
|
|
+ AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
|
|
|
+ return isExistTableId(list, annotationHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 判断主键注解是否存在
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param list 字段列表
|
|
|
+ * @return true 为存在 {@link TableId} 注解;
|
|
|
+ */
|
|
|
+ public static boolean isExistTableId(List<Field> list, AnnotationHandler annotationHandler) {
|
|
|
+ return list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, TableId.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 判断逻辑删除注解是否存在
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param clazz 实体类
|
|
|
+ * @param list 字段列表
|
|
|
+ * @return true 为存在 {@link TableLogic} 注解;
|
|
|
+ */
|
|
|
+ public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
|
|
+ AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
|
|
|
+ return isExistTableLogic(list, annotationHandler);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -368,8 +417,8 @@ public class TableInfoHelper {
|
|
|
* @param list 字段列表
|
|
|
* @return true 为存在 {@link TableLogic} 注解;
|
|
|
*/
|
|
|
- public static boolean isExistTableLogic(List<Field> list) {
|
|
|
- return list.stream().anyMatch(field -> field.isAnnotationPresent(TableLogic.class));
|
|
|
+ public static boolean isExistTableLogic(List<Field> list, AnnotationHandler annotationHandler) {
|
|
|
+ return list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, TableLogic.class));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -377,11 +426,27 @@ public class TableInfoHelper {
|
|
|
* 判断排序注解是否存在
|
|
|
* </p>
|
|
|
*
|
|
|
+ * @param clazz 实体类
|
|
|
* @param list 字段列表
|
|
|
* @return true 为存在 {@link OrderBy} 注解;
|
|
|
*/
|
|
|
- public static boolean isExistOrderBy(List<Field> list) {
|
|
|
- return list.stream().anyMatch(field -> field.isAnnotationPresent(OrderBy.class));
|
|
|
+ public static boolean isExistOrderBy(Class<?> clazz, List<Field> list) {
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
|
|
+ AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
|
|
|
+ return isExistOrderBy(list, annotationHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 判断排序注解是否存在
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param list 字段列表
|
|
|
+ * @param annotationHandler 注解处理类
|
|
|
+ * @return true 为存在 {@link OrderBy} 注解;
|
|
|
+ */
|
|
|
+ public static boolean isExistOrderBy(List<Field> list, AnnotationHandler annotationHandler) {
|
|
|
+ return list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, OrderBy.class));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -389,15 +454,16 @@ public class TableInfoHelper {
|
|
|
* 主键属性初始化
|
|
|
* </p>
|
|
|
*
|
|
|
- * @param dbConfig 全局配置信息
|
|
|
+ * @param globalConfig 全局配置信息
|
|
|
* @param tableInfo 表信息
|
|
|
* @param field 字段
|
|
|
* @param tableId 注解
|
|
|
*/
|
|
|
- private static void initTableIdWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableId tableId) {
|
|
|
+ private static void initTableIdWithAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field, TableId tableId) {
|
|
|
+ GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
boolean underCamel = tableInfo.isUnderCamel();
|
|
|
final String property = field.getName();
|
|
|
- if (field.getAnnotation(TableField.class) != null) {
|
|
|
+ if (globalConfig.getAnnotationHandler().isAnnotationPresent(field, TableField.class)) {
|
|
|
logger.warn(String.format("This \"%s\" is the table primary key by @TableId annotation in Class: \"%s\",So @TableField annotation will not work!",
|
|
|
property, tableInfo.getEntityType().getName()));
|
|
|
}
|
|
@@ -445,14 +511,16 @@ public class TableInfoHelper {
|
|
|
* 主键属性初始化
|
|
|
* </p>
|
|
|
*
|
|
|
+ * @param globalConfig 全局配置
|
|
|
* @param tableInfo 表信息
|
|
|
* @param field 字段
|
|
|
* @return true 继续下一个属性判断,返回 continue;
|
|
|
*/
|
|
|
- private static boolean initTableIdWithoutAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
|
|
|
+ private static boolean initTableIdWithoutAnnotation(GlobalConfig globalConfig, TableInfo tableInfo, Field field) {
|
|
|
+ GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
final String property = field.getName();
|
|
|
if (DEFAULT_ID_NAME.equalsIgnoreCase(property)) {
|
|
|
- if (field.getAnnotation(TableField.class) != null) {
|
|
|
+ if (globalConfig.getAnnotationHandler().isAnnotationPresent(field, TableField.class)) {
|
|
|
logger.warn(String.format("This \"%s\" is the table primary key by default name for `id` in Class: \"%s\",So @TableField will not work!",
|
|
|
property, tableInfo.getEntityType().getName()));
|
|
|
}
|
|
@@ -512,11 +580,26 @@ public class TableInfoHelper {
|
|
|
* @return 属性集合
|
|
|
*/
|
|
|
public static List<Field> getAllFields(Class<?> clazz) {
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
|
|
+ AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
|
|
|
+ return getAllFields(clazz, annotationHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 获取该类的所有属性列表
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param clazz 反射类
|
|
|
+ * @param annotationHandler 注解处理类
|
|
|
+ * @return 属性集合
|
|
|
+ */
|
|
|
+ public static List<Field> getAllFields(Class<?> clazz, AnnotationHandler annotationHandler) {
|
|
|
List<Field> fieldList = ReflectionKit.getFieldList(ClassUtils.getUserClass(clazz));
|
|
|
return fieldList.stream()
|
|
|
.filter(field -> {
|
|
|
/* 过滤注解非表字段属性 */
|
|
|
- TableField tableField = field.getAnnotation(TableField.class);
|
|
|
+ TableField tableField = annotationHandler.getAnnotation(field, TableField.class);
|
|
|
return (tableField == null || tableField.exist());
|
|
|
}).collect(toList());
|
|
|
}
|