根据字符串,获取实体属性上的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
    }
}
相关推荐
幽络源小助理3 小时前
微信小程序文章管理系统开发实现
java·微信小程序·springboot
有什么东东1 天前
山东大学软件学院创新项目实训开发日志(20)之中医知识问答自动生成对话标题bug修改
java·vue·bug·springboot
我爱布朗熊1 天前
4.RabbitMQ - 延迟消息
rabbitmq·springboot
mask哥2 天前
详解springcloudalibaba采用prometheus+grafana实现服务监控
java·nacos·springboot·grafana·prometheus·springcloud·微服务监控
幽络源小助理2 天前
SpringBoot民宿管理系统开发实现
java·spring boot·springboot·民宿系统
古渡蓝按2 天前
微信支付功能的设计实现与关键实践(UniApp+Java)全代码
springboot
Nick_zcy3 天前
协同推荐算法实现的智能商品推荐系统 - [基于springboot +vue]
算法·springboot·推荐算法
洛阳泰山4 天前
LangChain4j 搭配 Kotlin:以协程、流式交互赋能语言模型开发
java·ai·语言模型·kotlin·交互·springboot·langchain4j
飞天小牛肉6 天前
拼多多面经,暑期实习Java一面
java·spring·springboot·秋招·校招
橘猫云计算机设计6 天前
django基于爬虫的网络新闻分析系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
后端·爬虫·python·django·毕业设计·springboot