【转载】Java注解
参考
测试代码
- 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();
}
- 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();
}
- 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 + '\'' +
'}';
}
}
- 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);
}
}
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/articles/15901235.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/articles/15901235.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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返回结构