【转载】Java注解

参考

  1. B站狂神说
  2. https://zhuanlan.zhihu.com/p/60730622

测试代码

  1. TableName
package annontation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableName {
    String value();
}

  1. TableField
package annontation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableField {
    String name();
    String type();
    byte length();

}

  1. User
package entity;

import annontation.TableField;
import annontation.TableName;

@TableName("users")
public class User {
    @TableField(name="id",type = "int", length = 10)
    private Integer id;
    @TableField(name="name",type = "varchar", length = 30)
    private String name;
    @TableField(name="sex",type = "varchar", length = 2)
    private String sex;
    @TableField(name="age",type = "int", length = 3)
    private Byte age;
    @TableField(name="des",type = "varchar", length = 120)
    private String des;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Byte getAge() {
        return age;
    }

    public void setAge(Byte age) {
        this.age = age;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", des='" + des + '\'' +
                '}';
    }
}

  1. User
import annontation.TableField;
import entity.User;

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

public class Main {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        // 获取类注解
        Annotation[] annotations = User.class.getAnnotations();
        // 判断类是否存在指定注解
        // User.class.getAnnotation(TableName.class)
        // 遍历类的所有注解
        for (Annotation annotation : annotations) {
            // 注解类型
//            System.out.println(annotation.annotationType());
//            System.out.println(annotation.toString());
        }
        // 获取属性所有属性
        Field[] fields = User.class.getDeclaredFields();
        for (Field field : fields) {
            // 判断是否存在指定注解
             if( field.isAnnotationPresent(TableField.class)){
                 // 根据类型获取这个注解
                 TableField tableField = field.getAnnotation(TableField.class);
                 // 根据注解的属性获取对应的属性值
//                 tableField.name();
//                 tableField.type();
//                 tableField.length();
                 // 反射获取类方法
                  Method setName = User.class.getDeclaredMethod("setName", String.class);
                  // 执行user中的setName方法并传递参数张三
                 setName.invoke(user, "张三");
             }
        }
//        System.out.println(user);
    }
}


posted @   夏秋初  阅读(22)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2021-02-16 Springboot2.x 统一json返回格式,并设置http响应码
2021-02-16 Springboot2.x 统一json返回结构
点击右上角即可分享
微信分享提示