Browse Source

1、@KeySequence添加了直接标注在注解上
2、统一实体和字段上获取注解的方式,统一接口,可提供外部使用方自定义获取注解的方式

唐振超 2 years ago
parent
commit
70877f0637

+ 1 - 1
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/KeySequence.java

@@ -27,7 +27,7 @@ import java.lang.annotation.*;
 @Documented
 @Documented
 @Inherited
 @Inherited
 @Retention(RetentionPolicy.RUNTIME)
 @Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
+@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
 public @interface KeySequence {
 public @interface KeySequence {
 
 
     /**
     /**

+ 3 - 0
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.autoconfigure;
 
 
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 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.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 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;
@@ -211,6 +212,8 @@ public class MybatisPlusAutoConfiguration implements InitializingBean {
         GlobalConfig globalConfig = this.properties.getGlobalConfig();
         GlobalConfig globalConfig = this.properties.getGlobalConfig();
         // TODO 注入填充器
         // TODO 注入填充器
         this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
         this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
+        // TODO 注入注解控制器
+        this.getBeanThen(AnnotationHandler.class, globalConfig::setAnnotationHandler);
         // TODO 注入参与器
         // TODO 注入参与器
         this.getBeanThen(PostInitTableInfoHandler.class, globalConfig::setPostInitTableInfoHandler);
         this.getBeanThen(PostInitTableInfoHandler.class, globalConfig::setPostInitTableInfoHandler);
         // TODO 注入主键生成器
         // TODO 注入主键生成器

+ 6 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/config/GlobalConfig.java

@@ -18,6 +18,8 @@ package com.baomidou.mybatisplus.core.config;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
+import com.baomidou.mybatisplus.core.handlers.DefaultAnnotationHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 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;
@@ -76,6 +78,10 @@ public class GlobalConfig implements Serializable {
      * 元对象字段填充控制器
      * 元对象字段填充控制器
      */
      */
     private MetaObjectHandler metaObjectHandler;
     private MetaObjectHandler metaObjectHandler;
+    /**
+     * 注解控制器
+     */
+    private AnnotationHandler annotationHandler = new DefaultAnnotationHandler();
     /**
     /**
      * 参与 TableInfo 的初始化
      * 参与 TableInfo 的初始化
      */
      */

+ 51 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/AnnotationHandler.java

@@ -0,0 +1,51 @@
+package com.baomidou.mybatisplus.core.handlers;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+
+/**
+ * @author 唐振超
+ * @since 2023-02-25
+ */
+public interface AnnotationHandler {
+
+    /**
+     * 从类上获取注解
+     *
+     * @param beanClass       类的class
+     * @param annotationClass 要获取的注解class
+     * @param <T>             具体注解
+     * @return 注解
+     */
+    <T extends Annotation> T getAnnotation(Class<?> beanClass, Class<T> annotationClass);
+
+    /**
+     * 判断类上是否存在注解
+     *
+     * @param beanClass       类的class
+     * @param annotationClass 要获取的注解class
+     * @param <T>             具体注解
+     * @return 是否包含该注解
+     */
+    <T extends Annotation> boolean isAnnotationPresent(Class<?> beanClass, Class<T> annotationClass);
+
+    /**
+     * 从字段上获取注解
+     *
+     * @param field           字段
+     * @param annotationClass 要获取的注解class
+     * @param <T>             具体注解
+     * @return 注解
+     */
+    <T extends Annotation> T getAnnotation(Field field, Class<T> annotationClass);
+
+    /**
+     * 判断字段上是否存在注解
+     *
+     * @param field           字段
+     * @param annotationClass 要获取的注解class
+     * @param <T>             具体注解
+     * @return 是否包含该注解
+     */
+    <T extends Annotation> boolean isAnnotationPresent(Field field, Class<T> annotationClass);
+}

+ 29 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/DefaultAnnotationHandler.java

@@ -0,0 +1,29 @@
+package com.baomidou.mybatisplus.core.handlers;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+
+/**
+ * @author 唐振超
+ */
+public class DefaultAnnotationHandler implements AnnotationHandler {
+    @Override
+    public <T extends Annotation> T getAnnotation(Class<?> beanClass, Class<T> annotationClass) {
+        return beanClass.getAnnotation(annotationClass);
+    }
+
+    @Override
+    public <T extends Annotation> boolean isAnnotationPresent(Class<?> beanClass, Class<T> annotationClass) {
+        return beanClass.isAnnotationPresent(annotationClass);
+    }
+
+    @Override
+    public <T extends Annotation> T getAnnotation(Field field, Class<T> annotationClass) {
+        return field.getAnnotation(annotationClass);
+    }
+
+    @Override
+    public <T extends Annotation> boolean isAnnotationPresent(Field field, Class<T> annotationClass) {
+        return field.isAnnotationPresent(annotationClass);
+    }
+}

+ 6 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MybatisEnumTypeHandler.java

@@ -17,10 +17,9 @@ package com.baomidou.mybatisplus.core.handlers;
 
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
 import com.baomidou.mybatisplus.annotation.EnumValue;
 import com.baomidou.mybatisplus.annotation.IEnum;
 import com.baomidou.mybatisplus.annotation.IEnum;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.*;
 import org.apache.ibatis.reflection.DefaultReflectorFactory;
 import org.apache.ibatis.reflection.DefaultReflectorFactory;
 import org.apache.ibatis.reflection.MetaClass;
 import org.apache.ibatis.reflection.MetaClass;
 import org.apache.ibatis.reflection.ReflectorFactory;
 import org.apache.ibatis.reflection.ReflectorFactory;
@@ -87,7 +86,9 @@ public class MybatisEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E
     }
     }
 
 
     private static Optional<Field> findEnumValueAnnotationField(Class<?> clazz) {
     private static Optional<Field> findEnumValueAnnotationField(Class<?> clazz) {
-        return Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.isAnnotationPresent(EnumValue.class)).findFirst();
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
+        AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
+        return Arrays.stream(clazz.getDeclaredFields()).filter(field -> annotationHandler.isAnnotationPresent(field, EnumValue.class)).findFirst();
     }
     }
 
 
     /**
     /**

+ 21 - 18
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -179,12 +179,12 @@ public class TableFieldInfo implements Constants {
      * 全新的 存在 TableField 注解时使用的构造函数
      * 全新的 存在 TableField 注解时使用的构造函数
      */
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableField tableField,
+    public TableFieldInfo(GlobalConfig globalConfig, TableInfo tableInfo, Field field, TableField tableField,
                           Reflector reflector, boolean existTableLogic, boolean isOrderBy) {
                           Reflector reflector, boolean existTableLogic, boolean isOrderBy) {
-        this(dbConfig, tableInfo, field, tableField, reflector, existTableLogic);
+        this(globalConfig, tableInfo, field, tableField, reflector, existTableLogic);
         this.isOrderBy = isOrderBy;
         this.isOrderBy = isOrderBy;
         if (isOrderBy) {
         if (isOrderBy) {
-            initOrderBy(field);
+            initOrderBy(globalConfig.getAnnotationHandler().getAnnotation(field, OrderBy.class));
         }
         }
     }
     }
 
 
@@ -192,11 +192,13 @@ public class TableFieldInfo implements Constants {
      * 全新的 存在 TableField 注解时使用的构造函数
      * 全新的 存在 TableField 注解时使用的构造函数
      */
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableField tableField,
+    public TableFieldInfo(GlobalConfig globalConfig, TableInfo tableInfo, Field field, TableField tableField,
                           Reflector reflector, boolean existTableLogic) {
                           Reflector reflector, boolean existTableLogic) {
+
+        GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
         field.setAccessible(true);
         field.setAccessible(true);
         this.field = field;
         this.field = field;
-        this.version = field.getAnnotation(Version.class) != null;
+        this.version = globalConfig.getAnnotationHandler().isAnnotationPresent(field, Version.class);
         this.property = field.getName();
         this.property = field.getName();
         this.propertyType = reflector.getGetterType(this.property);
         this.propertyType = reflector.getGetterType(this.property);
         this.isPrimitive = this.propertyType.isPrimitive();
         this.isPrimitive = this.propertyType.isPrimitive();
@@ -244,7 +246,7 @@ public class TableFieldInfo implements Constants {
         this.el = el;
         this.el = el;
         int index = el.indexOf(COMMA);
         int index = el.indexOf(COMMA);
         this.mapping = index > 0 ? el.substring(++index) : null;
         this.mapping = index > 0 ? el.substring(++index) : null;
-        this.initLogicDelete(dbConfig, field, existTableLogic);
+        this.initLogicDelete(globalConfig, field, existTableLogic);
 
 
         String column = tableField.value();
         String column = tableField.value();
         if (StringUtils.isBlank(column)) {
         if (StringUtils.isBlank(column)) {
@@ -306,33 +308,34 @@ public class TableFieldInfo implements Constants {
     /**
     /**
      * 不存在 TableField 注解时, 使用的构造函数
      * 不存在 TableField 注解时, 使用的构造函数
      */
      */
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, Reflector reflector,
+    public TableFieldInfo(GlobalConfig globalConfig, TableInfo tableInfo, Field field, Reflector reflector,
                           boolean existTableLogic, boolean isOrderBy) {
                           boolean existTableLogic, boolean isOrderBy) {
-        this(dbConfig, tableInfo, field, reflector, existTableLogic);
+        this(globalConfig, tableInfo, field, reflector, existTableLogic);
         this.isOrderBy = isOrderBy;
         this.isOrderBy = isOrderBy;
         if (isOrderBy) {
         if (isOrderBy) {
-            initOrderBy(field);
+            initOrderBy(globalConfig.getAnnotationHandler().getAnnotation(field, OrderBy.class));
         }
         }
     }
     }
 
 
     /**
     /**
      * 不存在 TableField 注解时, 使用的构造函数
      * 不存在 TableField 注解时, 使用的构造函数
      */
      */
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, Reflector reflector,
+    public TableFieldInfo(GlobalConfig globalConfig, TableInfo tableInfo, Field field, Reflector reflector,
                           boolean existTableLogic) {
                           boolean existTableLogic) {
         field.setAccessible(true);
         field.setAccessible(true);
         this.field = field;
         this.field = field;
-        this.version = field.getAnnotation(Version.class) != null;
+        this.version = globalConfig.getAnnotationHandler().isAnnotationPresent(field, Version.class);
         this.property = field.getName();
         this.property = field.getName();
         this.propertyType = reflector.getGetterType(this.property);
         this.propertyType = reflector.getGetterType(this.property);
         this.isPrimitive = this.propertyType.isPrimitive();
         this.isPrimitive = this.propertyType.isPrimitive();
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.el = this.property;
         this.el = this.property;
         this.mapping = null;
         this.mapping = null;
+        GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
         this.whereStrategy = dbConfig.getWhereStrategy();
         this.whereStrategy = dbConfig.getWhereStrategy();
-        this.initLogicDelete(dbConfig, field, existTableLogic);
+        this.initLogicDelete(globalConfig, field, existTableLogic);
 
 
         String column = this.property;
         String column = this.property;
         if (tableInfo.isUnderCamel()) {
         if (tableInfo.isUnderCamel()) {
@@ -366,10 +369,9 @@ public class TableFieldInfo implements Constants {
     /**
     /**
      * 排序初始化
      * 排序初始化
      *
      *
-     * @param field 字段
+     * @param orderBy 排序注解
      */
      */
-    private void initOrderBy(Field field) {
-        OrderBy orderBy = field.getAnnotation(OrderBy.class);
+    private void initOrderBy(OrderBy orderBy) {
         if (null != orderBy) {
         if (null != orderBy) {
             this.isOrderBy = true;
             this.isOrderBy = true;
             this.orderBySort = orderBy.sort();
             this.orderBySort = orderBy.sort();
@@ -386,12 +388,13 @@ public class TableFieldInfo implements Constants {
     /**
     /**
      * 逻辑删除初始化
      * 逻辑删除初始化
      *
      *
-     * @param dbConfig 数据库全局配置
+     * @param globalConfig 全局配置
      * @param field    字段属性对象
      * @param field    字段属性对象
      */
      */
-    private void initLogicDelete(GlobalConfig.DbConfig dbConfig, Field field, boolean existTableLogic) {
+    private void initLogicDelete(GlobalConfig globalConfig, Field field, boolean existTableLogic) {
+        GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
         /* 获取注解属性,逻辑处理字段 */
         /* 获取注解属性,逻辑处理字段 */
-        TableLogic tableLogic = field.getAnnotation(TableLogic.class);
+        TableLogic tableLogic = globalConfig.getAnnotationHandler().getAnnotation(field, TableLogic.class);
         if (null != tableLogic) {
         if (null != tableLogic) {
             if (StringUtils.isNotBlank(tableLogic.value())) {
             if (StringUtils.isNotBlank(tableLogic.value())) {
                 this.logicNotDeleteValue = tableLogic.value();
                 this.logicNotDeleteValue = tableLogic.value();

+ 41 - 27
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.core.metadata;
 
 
 import com.baomidou.mybatisplus.annotation.*;
 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.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.*;
 import com.baomidou.mybatisplus.core.toolkit.*;
@@ -199,7 +200,8 @@ public class TableInfoHelper {
     private static String[] initTableName(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
     private static String[] initTableName(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
         /* 数据库全局配置 */
         /* 数据库全局配置 */
         GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
         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 tableName = clazz.getSimpleName();
         String tablePrefix = dbConfig.getTablePrefix();
         String tablePrefix = dbConfig.getTablePrefix();
@@ -241,7 +243,7 @@ public class TableInfoHelper {
 
 
         /* 开启了自定义 KEY 生成器 */
         /* 开启了自定义 KEY 生成器 */
         if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
         if (CollectionUtils.isNotEmpty(dbConfig.getKeyGenerators())) {
-            tableInfo.setKeySequence(clazz.getAnnotation(KeySequence.class));
+            tableInfo.setKeySequence(annotationHandler.getAnnotation(clazz, KeySequence.class));
         }
         }
         return excludeProperty;
         return excludeProperty;
     }
     }
@@ -279,17 +281,16 @@ public class TableInfoHelper {
      * @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, List<String> excludeProperty) {
-        /* 数据库全局配置 */
-        GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
+        AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
         PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
         PostInitTableInfoHandler postInitTableInfoHandler = globalConfig.getPostInitTableInfoHandler();
         Reflector reflector = tableInfo.getReflector();
         Reflector reflector = tableInfo.getReflector();
         List<Field> list = getAllFields(clazz);
         List<Field> list = getAllFields(clazz);
         // 标记是否读取到主键
         // 标记是否读取到主键
         boolean isReadPK = false;
         boolean isReadPK = false;
         // 是否存在 @TableId 注解
         // 是否存在 @TableId 注解
-        boolean existTableId = isExistTableId(list);
+        boolean existTableId = isExistTableId(clazz, list);
         // 是否存在 @TableLogic 注解
         // 是否存在 @TableLogic 注解
-        boolean existTableLogic = isExistTableLogic(list);
+        boolean existTableLogic = isExistTableLogic(clazz, list);
 
 
         List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
         List<TableFieldInfo> fieldList = new ArrayList<>(list.size());
         for (Field field : list) {
         for (Field field : list) {
@@ -298,43 +299,42 @@ public class TableInfoHelper {
             }
             }
 
 
             boolean isPK = false;
             boolean isPK = false;
-            boolean isOrderBy = field.getAnnotation(OrderBy.class) != null;
+            boolean isOrderBy = annotationHandler.getAnnotation(field, OrderBy.class) != null;
 
 
             /* 主键ID 初始化 */
             /* 主键ID 初始化 */
             if (existTableId) {
             if (existTableId) {
-                TableId tableId = field.getAnnotation(TableId.class);
+                TableId tableId = annotationHandler.getAnnotation(field, TableId.class);
                 if (tableId != null) {
                 if (tableId != null) {
                     if (isReadPK) {
                     if (isReadPK) {
                         throw ExceptionUtils.mpe("@TableId can't more than one in Class: \"%s\".", clazz.getName());
                         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;
                     isPK = isReadPK = true;
                 }
                 }
             } else if (!isReadPK) {
             } else if (!isReadPK) {
-                isPK = isReadPK = initTableIdWithoutAnnotation(dbConfig, tableInfo, field);
-
+                isPK = isReadPK = initTableIdWithoutAnnotation(globalConfig, tableInfo, field);
             }
             }
 
 
             if (isPK) {
             if (isPK) {
                 if (isOrderBy) {
                 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;
                 continue;
             }
             }
 
 
-            final TableField tableField = field.getAnnotation(TableField.class);
+            final TableField tableField = annotationHandler.getAnnotation(field, TableField.class);
 
 
             /* 有 @TableField 注解的字段初始化 */
             /* 有 @TableField 注解的字段初始化 */
             if (tableField != null) {
             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);
                 fieldList.add(tableFieldInfo);
                 postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
                 postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
                 continue;
                 continue;
             }
             }
 
 
             /* 无 @TableField  注解的字段初始化 */
             /* 无 @TableField  注解的字段初始化 */
-            TableFieldInfo tableFieldInfo = new TableFieldInfo(dbConfig, tableInfo, field, reflector, existTableLogic, isOrderBy);
+            TableFieldInfo tableFieldInfo = new TableFieldInfo(globalConfig, tableInfo, field, reflector, existTableLogic, isOrderBy);
             fieldList.add(tableFieldInfo);
             fieldList.add(tableFieldInfo);
             postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
             postInitTableInfoHandler.postFieldInfo(tableFieldInfo, configuration);
         }
         }
@@ -353,11 +353,14 @@ public class TableInfoHelper {
      * 判断主键注解是否存在
      * 判断主键注解是否存在
      * </p>
      * </p>
      *
      *
+     * @param clazz 实体类
      * @param list 字段列表
      * @param list 字段列表
      * @return true 为存在 {@link TableId} 注解;
      * @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 list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, TableId.class));
     }
     }
 
 
     /**
     /**
@@ -365,11 +368,14 @@ public class TableInfoHelper {
      * 判断逻辑删除注解是否存在
      * 判断逻辑删除注解是否存在
      * </p>
      * </p>
      *
      *
+     * @param clazz 实体类
      * @param list 字段列表
      * @param list 字段列表
      * @return true 为存在 {@link TableLogic} 注解;
      * @return true 为存在 {@link TableLogic} 注解;
      */
      */
-    public static boolean isExistTableLogic(List<Field> list) {
-        return list.stream().anyMatch(field -> field.isAnnotationPresent(TableLogic.class));
+    public static boolean isExistTableLogic(Class<?> clazz, List<Field> list) {
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
+        AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
+        return list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, TableLogic.class));
     }
     }
 
 
     /**
     /**
@@ -377,11 +383,14 @@ public class TableInfoHelper {
      * 判断排序注解是否存在
      * 判断排序注解是否存在
      * </p>
      * </p>
      *
      *
+     * @param clazz 实体类
      * @param list 字段列表
      * @param list 字段列表
      * @return true 为存在 {@link OrderBy} 注解;
      * @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 list.stream().anyMatch(field -> annotationHandler.isAnnotationPresent(field, OrderBy.class));
     }
     }
 
 
     /**
     /**
@@ -389,15 +398,16 @@ public class TableInfoHelper {
      * 主键属性初始化
      * 主键属性初始化
      * </p>
      * </p>
      *
      *
-     * @param dbConfig  全局配置信息
+     * @param globalConfig  全局配置信息
      * @param tableInfo 表信息
      * @param tableInfo 表信息
      * @param field     字段
      * @param field     字段
      * @param tableId   注解
      * @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();
         boolean underCamel = tableInfo.isUnderCamel();
         final String property = field.getName();
         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!",
             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()));
                 property, tableInfo.getEntityType().getName()));
         }
         }
@@ -445,14 +455,16 @@ public class TableInfoHelper {
      * 主键属性初始化
      * 主键属性初始化
      * </p>
      * </p>
      *
      *
+     * @param globalConfig 全局配置
      * @param tableInfo 表信息
      * @param tableInfo 表信息
      * @param field     字段
      * @param field     字段
      * @return true 继续下一个属性判断,返回 continue;
      * @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();
         final String property = field.getName();
         if (DEFAULT_ID_NAME.equalsIgnoreCase(property)) {
         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!",
                 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()));
                     property, tableInfo.getEntityType().getName()));
             }
             }
@@ -512,11 +524,13 @@ public class TableInfoHelper {
      * @return 属性集合
      * @return 属性集合
      */
      */
     public static List<Field> getAllFields(Class<?> clazz) {
     public static List<Field> getAllFields(Class<?> clazz) {
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
+        AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
         List<Field> fieldList = ReflectionKit.getFieldList(ClassUtils.getUserClass(clazz));
         List<Field> fieldList = ReflectionKit.getFieldList(ClassUtils.getUserClass(clazz));
         return fieldList.stream()
         return fieldList.stream()
             .filter(field -> {
             .filter(field -> {
                 /* 过滤注解非表字段属性 */
                 /* 过滤注解非表字段属性 */
-                TableField tableField = field.getAnnotation(TableField.class);
+                TableField tableField = annotationHandler.getAnnotation(field, TableField.class);
                 return (tableField == null || tableField.exist());
                 return (tableField == null || tableField.exist());
             }).collect(toList());
             }).collect(toList());
     }
     }

+ 5 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GlobalConfigUtils.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.core.toolkit;
 
 
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.IdType;
 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.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
@@ -108,6 +109,10 @@ public class GlobalConfigUtils {
         return Optional.ofNullable(getGlobalConfig(configuration).getMetaObjectHandler());
         return Optional.ofNullable(getGlobalConfig(configuration).getMetaObjectHandler());
     }
     }
 
 
+    public static Optional<AnnotationHandler> getAnnotationHandler(Configuration configuration) {
+        return Optional.ofNullable(getGlobalConfig(configuration).getAnnotationHandler());
+    }
+
     public static Class<?> getSuperMapperClass(Configuration configuration) {
     public static Class<?> getSuperMapperClass(Configuration configuration) {
         return getGlobalConfig(configuration).getSuperMapperClass();
         return getGlobalConfig(configuration).getSuperMapperClass();
     }
     }

+ 2 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java

@@ -74,8 +74,8 @@ class TableInfoHelperTest {
 
 
     @Test
     @Test
     void testIsExistTableId() {
     void testIsExistTableId() {
-        Assertions.assertThat(TableInfoHelper.isExistTableId(Arrays.asList(ModelOne.class.getDeclaredFields()))).isTrue();
-        assertThat(TableInfoHelper.isExistTableId(Arrays.asList(ModelTwo.class.getDeclaredFields()))).isFalse();
+        Assertions.assertThat(TableInfoHelper.isExistTableId(ModelOne.class, Arrays.asList(ModelOne.class.getDeclaredFields()))).isTrue();
+        assertThat(TableInfoHelper.isExistTableId(ModelTwo.class, Arrays.asList(ModelTwo.class.getDeclaredFields()))).isFalse();
     }
     }
 
 
     @Test
     @Test

+ 6 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/Entity.java

@@ -18,7 +18,9 @@ package com.baomidou.mybatisplus.generator.config.builder;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.generator.IFill;
 import com.baomidou.mybatisplus.generator.IFill;
 import com.baomidou.mybatisplus.generator.ITemplate;
 import com.baomidou.mybatisplus.generator.ITemplate;
@@ -187,13 +189,15 @@ public class Entity implements ITemplate {
      * @param clazz 实体父类 Class
      * @param clazz 实体父类 Class
      */
      */
     public void convertSuperEntityColumns(Class<?> clazz) {
     public void convertSuperEntityColumns(Class<?> clazz) {
+        com.baomidou.mybatisplus.core.metadata.TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
+        AnnotationHandler annotationHandler = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getAnnotationHandler();
         List<Field> fields = TableInfoHelper.getAllFields(clazz);
         List<Field> fields = TableInfoHelper.getAllFields(clazz);
         this.superEntityColumns.addAll(fields.stream().map(field -> {
         this.superEntityColumns.addAll(fields.stream().map(field -> {
-            TableId tableId = field.getAnnotation(TableId.class);
+            TableId tableId = annotationHandler.getAnnotation(field, TableId.class);
             if (tableId != null && StringUtils.isNotBlank(tableId.value())) {
             if (tableId != null && StringUtils.isNotBlank(tableId.value())) {
                 return tableId.value();
                 return tableId.value();
             }
             }
-            TableField tableField = field.getAnnotation(TableField.class);
+            TableField tableField = annotationHandler.getAnnotation(field, TableField.class);
             if (tableField != null && StringUtils.isNotBlank(tableField.value())) {
             if (tableField != null && StringUtils.isNotBlank(tableField.value())) {
                 return tableField.value();
                 return tableField.value();
             }
             }