码迷,mamicode.com
首页 > 其他好文 > 详细

自定义注解实现简单的orm映射框架

时间:2019-08-19 13:21:56      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:stat   div   vax   XML   declare   runtime   ref   value   tab   

package com.mj;

import javax.xml.bind.Element;
import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test01 {

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

    @Target(value = {ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface myproper{
        public String name();
        public int length() default 0;

    }




    public static void main(String[] args) throws ClassNotFoundException {
        long l1 = System.currentTimeMillis();
        Class<?> aClass = Class.forName("com.mj.Student");
        myproper declaredAnnotation2 = aClass.getDeclaredAnnotation(myproper.class);
        StringBuffer sb=new StringBuffer();
        sb.append("select ");
        Field[] declaredFields = aClass.getDeclaredFields();
        for (int i=0;i<declaredFields.length;i++){
            Field declaredField = declaredFields[i];
            myproper declaredAnnotation = declaredField.getDeclaredAnnotation(myproper.class);
            String name = declaredAnnotation.name();
            sb.append(name);
            if(i<declaredFields.length-1){
                sb.append(",");
            }
        }

        table declaredAnnotation1 = aClass.getDeclaredAnnotation(table.class);
        String tablename = declaredAnnotation1.value();
        sb.append(" from ").append(tablename);
        long l2 = System.currentTimeMillis();
//        System.out.println((l2-l1));
        System.out.println(sb.toString());

    }
}
package com.mj;

@Test01.table("student")
class Student {
    @Test01.myproper(name="name",length = 255)
    private String s_name;
    @Test01.myproper(name="age",length = 11)
    private int s_age;
    @Test01.myproper(name="id",length = 11)
    private int s_id;
}

 

自定义注解实现简单的orm映射框架

标签:stat   div   vax   XML   declare   runtime   ref   value   tab   

原文地址:https://www.cnblogs.com/a1304908180/p/11376206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!