根据字符串,获取实体属性上的annotation,如:createTime” 找到对应实体属性中的 TableField(value = "create_time", fill = FieldFill.INSERT)

根据字符串,获取实体属性上的annotation,如:createTime" 找到对应实体属性中的 TableField(value = "create_time", fill = FieldFill.INSERT)

  • Field[] fields = clazz.getFields(); //仅能获取类(及其父类) public属性成员
  • Field[] declaredFields = clazz.getDeclaredFields(); //仅能获取类本身的属性成员(包括私有、共有、保护)
  • Field[] superDeclaredFields = clazz.getSuperclass().getDeclaredFields(); //因此在获取父类的私有属性时,要通过getSuperclass的之后再通过getDeclaredFiled获取。

BaseEntity.java

java 复制代码
public class BaseEntity implements Serializable {
    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

SysRole.java

java 复制代码
/**
 * 角色
 */
@Schema(description = "角色信息")
@TableName("sys_role")
public class SysRole extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主健ID
     */
    @Schema(description = "主健ID")
    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;
    /**
     * 角色标识
     */
    @Schema(description = "角色标识")
    @TableField(value = "code")
    private String code;

    

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}

MybatisPlusTest

java 复制代码
import com.baomidou.mybatisplus.annotation.TableField;
import com.cuwor.base.entity.SysRole;
import io.swagger.v3.oas.annotations.media.Schema;
import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field; 

public class MybatisPlusTest {

    @Test
    void queryWrapperTest() {
        String propertyName = "createTime";
        String columnName = getAnnotationValue(SysRole.class, propertyName, TableField.class);
        System.out.println("Database column name for '" + propertyName + "' is: " + columnName);
        String condition = getAnnotationValue(SysRole.class, propertyName, Schema.class, "description");
        System.out.println("@Schema description is: " + condition);
    }


    /**
     * 根据字符串,找到对应属性,获取Annotation的值
     *
     * @param clazz 被查找对象
     * @param propertyName  被查找属性
     * @param annotationClass 注释类
     * @param methodNames     注释类的方法
     * @param <T>
     * @return
     */
    public static <T extends Annotation> String getAnnotationValue(Class<?> clazz, String propertyName, Class<T> annotationClass, String... methodNames) {
        try {
            // 首先检查当前类中的字段 -- 仅能获取类本身的属性成员(包括私有、共有、保护)
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.getName().equals(propertyName)) {
                    T annotation = field.getAnnotation(annotationClass);
                    if (annotation != null) {
                        // 这里假设注解有一个名为 "value" 的方法返回 String 类型
                        // 你需要根据实际的注解定义来调整
                        java.lang.reflect.Method valueMethod = annotationClass.getMethod(methodNames.length > 0 ? methodNames[0] : "value");
                        return (String) valueMethod.invoke(annotation);
                    }
                }
            }

            // 如果没有在当前类中找到,就检查父类 -- 在获取父类的私有属性时,要通过getSuperclass的之后再通过getDeclaredFiled获取。
            Class<?> superclass = clazz.getSuperclass();
            while (superclass != null) {
                Field[] superDeclaredFields = superclass.getDeclaredFields();
                for (Field field : superDeclaredFields) {
                    if (field.getName().equals(propertyName)) {
                        T annotation = field.getAnnotation(annotationClass);
                        if (annotation != null) {
                            java.lang.reflect.Method valueMethod = annotationClass.getMethod(methodNames.length > 0 ? methodNames[0] : "value");
                            return (String) valueMethod.invoke(annotation);
                        }
                    }
                }
                superclass = superclass.getSuperclass();  // 递归搜索父类
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null; // 如果没有找到对应的注解或属性,返回null
    }
}
相关推荐
lssjzmn5 小时前
Spring Web 异步响应实战:从 CompletableFuture 到 ResponseBodyEmitter 的全链路优化
java·前端·后端·springboot·异步·接口优化
叫我阿柒啊16 小时前
Java全栈开发工程师的实战面试经历:从基础到微服务
java·微服务·typescript·vue·springboot·前端开发·后端开发
Jerry&Grj18 小时前
SpringBoot埋点功能技术实现方案深度解析:架构设计、性能优化与扩展性实践
java·微服务·性能优化·springboot·架构设计·埋点技术
叫我阿柒啊4 天前
从Java全栈到前端框架:一次真实的面试对话与技术解析
java·javascript·typescript·vue·springboot·react·前端开发
荣淘淘5 天前
互联网大厂求职面试记:谢飞机的搞笑答辩
java·jvm·spring·面试·springboot·线程池·多线程
叫我阿柒啊6 天前
从全栈开发到微服务架构:一位Java工程师的实战经验分享
java·ci/cd·kafka·mybatis·vue3·springboot·fullstack
原来是好奇心6 天前
Spring Boot 事务失效的八大原因及解决方案详解
spring·springboot·事务
mask哥6 天前
DP-观察者模式代码详解
java·观察者模式·微服务·设计模式·springboot·设计原则
叫我阿柒啊7 天前
从Java全栈到前端框架:一场真实面试的深度技术探索
java·redis·微服务·typescript·vue3·springboot·jwt