首页 > 其他分享 >反射5 - 获取注解信息

反射5 - 获取注解信息

时间:2023-01-05 17:12:27浏览次数:62  
标签:反射 name int age annotation 获取 id 注解 public

反射 获取注解信息

什么是ORM

Object relationship Mapping -- 对象关系映射(数据库关系)

  • 类 -> 表
  • 类中属性 -> 表的字段
  • 类的对象 -> 表中的记录

通过注解联系属性与数据库字段

package annotation;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class ReflectAnnotation {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<Student> c1 = Student.class;

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation); //@annotation.TableAshen(value=db_student)
        }

        //获得注解的value的值
        TableAshen tableAshen = (TableAshen) c1.getAnnotation(TableAshen.class);
        System.out.println(tableAshen.value()); //db_student

        //获得类指定的注解 -- 属性name 对应注解的约束内容
        Field f = c1.getDeclaredField("name");
        FieldAshen annotation = f.getAnnotation(FieldAshen.class);
        System.out.println(annotation.columuName());
        System.out.println(annotation.type());
        System.out.println(annotation.lenth());


    }
}

@TableAshen("db_student")  //该对应数据库名为db_student
class Student{

    //通过注解约束,维持对象属性与数据库字段的一致性
    @FieldAshen(columuName = "db_id", type = "int", lenth = 10)
    private int id;
    @FieldAshen(columuName = "db_age", type = "int", lenth = 10)
    private int age;
    @FieldAshen(columuName = "db_name", type = "varchar", lenth = 3)
    private String name;

    public Student() {
    }

    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

//自定义注解 -- 约束类名 => 数据库中的表名
@Target(ElementType.TYPE) //作用在类上
@Retention(RetentionPolicy.RUNTIME)
@interface TableAshen{
    String value();
}

//自定义注解 -- 约束属性 => 数据库中的字段
@Target(ElementType.FIELD) //作用在属性上
@Retention(RetentionPolicy.RUNTIME)
@interface FieldAshen{
    String columuName(); //数据库中列名
    String type();       //存储数据类型
    int lenth();         //数据长度限制
}

标签:反射,name,int,age,annotation,获取,id,注解,public
From: https://www.cnblogs.com/Ashen-/p/17028155.html

相关文章

  • Linux C++ 获取系统CPU和网络情况
    linux下C++获取系统CPU情况和网络使用情况#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#defineMAXBUFSIZE1024#defineWAIT_SE......
  • 反射4 - 获取泛型
    反射获取泛型信息ParameterizedType:表示一种参数化类型,比如CollectionGenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型TypeVariable:是各种类型......
  • 注解2 - 元注解
    元注解meta-annotation注解其他注解这些类型和它们所支持的类在java.lang.annotation包中找到@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)@Retent......
  • Java Annotation-注解
    Java注解(Annotation)是JDK5.0引入的一种注释机制。Java语言中的类、方法、变量、参数和包等都可以被注解。和Javadoc不同,Java注解可以通过反射获取标注内容。在编译器生成......
  • 反射3 - 创建对象、调用方法
    反射-练习反射机制的优势很明显:它可以实现动态创建对象和编译,体现出语言很大的灵活性,这也是反射被广泛用于各种框架中的最主要原因。反射机制的缺点也是显而易见的......
  • 21、商品服务--三级分类--查询、递归树形数据获取
    1、编写查询分类的controller2、给CategoryEntity添加一个children字段3、编写service的接口4、编写serviceImpl的接口实现(用到了递归来设置子菜单下的子菜单)......
  • 反射2 - 类加载器
    类加载器Java的类主要分为3种Java核心类库:String,Object...JVM软件平台开发商自己声明定义的类:User,Child...相应的类加载器也有3种--Java17BootClassLoade......
  • 反射1 - 基础
    反射对象获取自身的类对象再通过类对象获取类中的属性、方法、权限等publicclass_Reflect{publicstaticvoidmain(String[]args)throwsNoSuchFieldExcepti......
  • RESTful风格与Spring注解
    RESTfulL是一种网络应用程序的设计风格和开发方式,即接口请求方式和路径的一种风格。普通风格:localhost:8080/add?a=1&b=2RestFul风格:localhost:8080/add/1/2GET获......
  • 获取某地模型并用Cesium加载
     2023-01-04最近想用Cesium给学校做一个类似智慧校园的东西,要做的东西很多,首先是获取学校模型的问题,然后怎么用Cesium加载3Dtile 1.获取学校模型想到之前被老师抓苦......