Explorar o código

支持实体属性组合注解.

https://github.com/baomidou/mybatis-plus/pull/5965
nieqiurong hai 1 ano
pai
achega
2573ec5d09

+ 4 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/AnnotationHandler.java

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.core.handlers;
 
+import com.baomidou.mybatisplus.core.toolkit.AnnotationUtils;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -58,7 +60,7 @@ public interface AnnotationHandler {
      * @return 注解
      */
     default <T extends Annotation> T getAnnotation(Field field, Class<T> annotationClass) {
-        return field.getAnnotation(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, field);
     }
 
     /**
@@ -70,7 +72,7 @@ public interface AnnotationHandler {
      * @return 是否包含该注解
      */
     default <T extends Annotation> boolean isAnnotationPresent(Field field, Class<T> annotationClass) {
-        return field.isAnnotationPresent(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, field) != null;
     }
 
     /**

+ 53 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/AnnotationUtils.java

@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.baomidou.mybatisplus.core.toolkit;
+
+import lombok.experimental.UtilityClass;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+
+/**
+ * @author nieqiurong
+ * @since 3.5.6
+ */
+@UtilityClass
+public class AnnotationUtils {
+
+    private static final String javaPackage = Override.class.getPackage().getName();
+
+    public <T extends Annotation> T findFirstAnnotation(Class<T> annotationClazz, Field field) {
+        return getAnnotation(annotationClazz, field.getDeclaredAnnotations());
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T extends Annotation> T getAnnotation(Class<T> annotationClazz, Annotation... annotations) {
+        for (Annotation annotation : annotations) {
+            if (annotation.annotationType().getPackage().getName().startsWith(javaPackage)) {
+                continue;
+            }
+            if (annotationClazz.isAssignableFrom(annotation.annotationType())) {
+                return (T) annotation;
+            }
+            annotation = getAnnotation(annotationClazz, annotation.annotationType().getDeclaredAnnotations());
+            if (annotation != null) {
+                return (T) annotation;
+            }
+        }
+        return null;
+    }
+
+}

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

@@ -17,6 +17,12 @@ 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.reflect.Field;
 import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -71,6 +77,44 @@ class TableInfoHelperTest {
         private String name;
     }
 
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableField(exist = false)
+    @interface NotExistsField {
+
+    }
+
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableId
+    @interface MyId {
+
+    }
+
+
+    @Data
+    private static class CustomAnnotation {
+
+        @MyId
+        private Long id;
+
+        private String name;
+
+        @NotExistsField
+        private String test;
+    }
+
+    @Test
+    void testCustomAnnotation() {
+        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
+        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(mybatisConfiguration, ""), CustomAnnotation.class);
+        List<Field> fieldList = TableInfoHelper.getAllFields(CustomAnnotation.class);
+        Assertions.assertThat(fieldList.size()).isEqualTo(2);
+        Assertions.assertThat(TableInfoHelper.isExistTableId(CustomAnnotation.class, Arrays.asList(CustomAnnotation.class.getDeclaredFields()))).isTrue();
+    }
+
 
     @Test
     void testIsExistTableId() {

+ 134 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/toolkit/AnnotationUtilsTest.java

@@ -0,0 +1,134 @@
+package com.baomidou.mybatisplus.test.toolkit;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.core.toolkit.AnnotationUtils;
+import lombok.Data;
+import org.junit.jupiter.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;
+
+/**
+ * @author nieqiurong
+ */
+public class AnnotationUtilsTest {
+
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableField(exist = false)
+    @interface TableField2 {
+
+    }
+
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableField(exist = true)
+    @interface TableField3 {
+
+    }
+
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableField2
+    @interface TableField4 {
+
+    }
+
+    @Data
+    public static class Demo {
+
+        private String id;
+
+        @TableField
+        private String name;
+
+        @TableField2
+        private String name1;
+
+        @TableField3
+        private String name2;
+
+        @TableField4
+        private String name3;
+
+        @TableField4
+        @TableField3
+        private String name4;
+
+        @TableField3
+        @TableField4
+        private String name5;
+
+        @TableField(fill = FieldFill.UPDATE)
+        @TableLogic(delval = "true", value = "false")
+        private Boolean deleted;
+
+    }
+
+    @Test
+    void test() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("id"));
+        Assertions.assertNull(tableField);
+    }
+
+    @Test
+    void test1() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertTrue(tableField.exist());
+    }
+
+    @Test
+    void test2() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name1"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertFalse(tableField.exist());
+    }
+
+    @Test
+    void test3() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name2"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertTrue(tableField.exist());
+    }
+
+    @Test
+    void test4() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name3"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertFalse(tableField.exist());
+    }
+
+    @Test
+    void test5() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name4"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertFalse(tableField.exist());
+    }
+
+    @Test
+    void test6() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("name5"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertTrue(tableField.exist());
+    }
+
+    @Test
+    void test7() throws Exception {
+        TableField tableField = AnnotationUtils.findFirstAnnotation(TableField.class, Demo.class.getDeclaredField("deleted"));
+        Assertions.assertNotNull(tableField);
+        Assertions.assertTrue(tableField.exist());
+        TableLogic tableLogic = AnnotationUtils.findFirstAnnotation(TableLogic.class, Demo.class.getDeclaredField("deleted"));
+        Assertions.assertNotNull(tableLogic);
+    }
+
+}