根据字符串,获取实体属性上的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
    }
}
相关推荐
shejizuopin1 小时前
基于JavaSSM+MySQL的实验室考勤管理系统设计与实现
java·mysql·vue·毕业设计·论文·springboot·实验室考勤管理系统设计与实现
千寻技术帮1 天前
10349_基于Springboot的万仙山旅游管理系统
mysql·springboot·旅游管理·在线旅游
jasnet_u1 天前
SpringCloudAlibaba的web微服务快速搭建
java·springboot·springlcoud
susu10830189111 天前
springboot3.5.8整合minio8.5.9
java·springboot
悟空码字2 天前
SpringBoot整合Zookeeper,实现分布式集群部署
java·zookeeper·springboot·编程技术·后端开发
Maiko Star3 天前
Word工具类——实现导出自定义Word文档(基于FreeMarker模板引擎生成动态内容的Word文档)
java·word·springboot·工具类
super_lzb3 天前
mybatis拦截器ResultSetHandler详解
java·spring·mybatis·springboot
Anakki3 天前
企业级 Elastic Stack 集成架构:Spring Boot 3.x 与 Elasticsearch 8.x 深度实践指南
运维·jenkins·springboot·elastic search
木风小助理4 天前
Spring循环依赖:原理、限制与解决方案深度解析
springboot