根据字符串,获取实体属性上的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
    }
}
相关推荐
fouryears_2341715 小时前
@PathVariable与@RequestParam的区别
java·spring·mvc·springboot
飞翔的佩奇2 天前
OpenTelemetry学习笔记(十二):在APM系统中,属性的命名空间处理遵循规则
笔记·学习·springboot·sdk·apm·opentelemetry
rhyme3 天前
源码浅析:SpringBoot main方法结束为什么程序不停止
springboot·markdown·java多线程·源码解析·mermaid
fouryears_234173 天前
Spring MVC 统一响应格式:ResponseBodyAdvice 从浅入深
java·spring·mvc·springboot
666HZ6664 天前
若依框架角色菜单权限
java·spring·springboot
鼠鼠我捏,要死了捏5 天前
Spring Boot中REST与gRPC并存架构设计与性能优化实践指南
springboot·restful·grpc
fanTuanye7 天前
前端环境搭建---基于SpringBoot+MySQL+Vue+ElementUI+Mybatis前后端分离面向小白管理系统搭建
vue.js·elementui·npm·springboot·前端开发环境搭建
飞鸟_Asuka8 天前
SpringBoot集成测试笔记:缩小测试范围、提高测试效率
java·单元测试·集成测试·springboot
nextera-void8 天前
SpringBoot 3.0 挥别 spring.factories,拥抱云原生新纪元
java·开发语言·springboot
TinpeaV8 天前
Springboot3整合Elasticsearch8(elasticsearch-java)
java·大数据·elasticsearch·搜索引擎·springboot