根据字符串,获取实体属性上的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
    }
}
相关推荐
带刺的坐椅2 天前
SpringBoot3 使用 SolonMCP 开发 MCP
java·ai·springboot·solon·mcp
LUCIAZZZ3 天前
JVM之虚拟机运行
java·jvm·spring·操作系统·springboot
堕落年代3 天前
SpringSecurity当中的CSRF防范详解
前端·springboot·csrf
一零贰肆3 天前
深入理解SpringBoot中的SpringCache缓存技术
java·springboot·springcache·缓存技术
LUCIAZZZ5 天前
JVM之内存管理(一)
java·jvm·spring·操作系统·springboot
LUCIAZZZ6 天前
JVM之内存管理(二)
java·jvm·后端·spring·操作系统·springboot
天上掉下来个程小白7 天前
缓存套餐-01.Spring Cache入门案例
java·redis·spring·缓存·springboot·springcache
程序员buddha9 天前
SpringBoot+Dubbo+Zookeeper实现分布式系统步骤
分布式·zookeeper·dubbo·springboot
天上掉下来个程小白9 天前
缓存套餐-03.功能测试
redis·缓存·springboot·苍穹外卖·springcache
Ten peaches10 天前
苍穹外卖(订单状态定时处理、来单提醒和客户催单)
java·数据库·sql·springboot