zoukankan      html  css  js  c++  java
  • 反射操作注解

    ORM:什么是ORM

    Object relationship Mapping - > 对象关系映射

    反射获得注解

    public class test12 {
        
        public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
            Class c1 = Class.forName("dome_05_注解.Student2");
            //通过反射 获得注解
            Annotation[] annotations = c1.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }
    
            // 获得 注解 value 的值
            Tablekuang annotation = (Tablekuang) c1.getAnnotation(Tablekuang.class);
            System.out.println(annotation.value());
    
            // 获得类指定的注解
            Field name = c1.getDeclaredField("name");
            Fieldkuang annotation2 = name.getAnnotation(Fieldkuang.class);
    
            System.out.println(annotation2.Columname());
            System.out.println(annotation2.type());
            System.out.println(annotation2.length());
    
        }
    }
    

    使用下面的自定义注解 和 测试类

    // 类名的注解
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Tablekuang {
        String value();
    }
    
    // 属性的注解
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Fieldkuang {
        String Columname();
        String type();
        int length();
    }
    
    class Student2 {
        private int id;
        private int age;
        private String name;
    
        public Student2() {    }
        public Student2(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;    }
    }
    

    运行结果

  • 相关阅读:
    elk2
    elk
    skywalking学习ppt
    Spring Boot]SpringBoot四大神器之Actuator
    黑马程序员spring data jpa 2019年第一版本
    css总结7:盒子模型理解
    css总结5:px、em、rem区别介绍
    css总结4:input 去掉外边框,placeholder的字体颜色、字号
    css总结3:Flex 布局教程:Flex-demos(转)
    css总结2:Flex 布局教程:Flex 语法(转)
  • 原文地址:https://www.cnblogs.com/kutsu/p/14008693.html
Copyright © 2011-2022 走看看