自定义注解的使用以及利用自定义注解实现ORM

1.如何自定义注解?

package com.dudu.lizhen.customannotations;

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

/**
 * 自定义注解
 * ---@Target 标识可以允许在什么地方使用 (比如 :方法或者类什么的)
 * --@Retention 标识  允许什么方式获取信息  (比如类似例子:就是可以通过java反射机制获取信息)
 * Created by lizhen on 2018/1/29.
 */
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotations {
    String value() default "";
    int classId() default 0;
    String[] arrAr();
 }

/**
 * 使用自定义注解的例子
 */
class AnnDemo{
    private String name;
    @CustomAnnotations(value = "lizhen",classId = 175,arrAr = {"11","22"})
    public  void add(){
        
    }
}

2.利用自定义注解实现ORM

package com.dudu.lizhen.customannotations;

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

/**
 * 实体类的注解
 * 目的:通过自定义注解实现实体类与表的对应  select user_name ,user_age from user_table
 * Created by lizhen on 2018/1/30.
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface SetTable {
    String name();
}

package com.dudu.lizhen.customannotations;

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

/**
 * 属性注解
 * 目的:通过自定义注解实现实体类与表的对应  select user_name ,user_age from user_table
 * Created by lizhen on 2018/1/30.
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface SetProperty {
    String name();
    int length();
}

package com.dudu.lizhen.customannotations;

/**
 * java实体类
 * 目的 :通过自定义注解实现实体类与表的对应  select user_name ,user_age from user_table
 * Created by lizhen on 2018/1/30.
 */
@SetTable(name = "user_table")
public class UserEntry {
    /**
     * 姓名
     */
    @SetProperty(name = "user_name", length =10 )
    private String name;
    /**
     * 年龄
     */
    @SetProperty(name ="user_age" , length =10 )
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "UserEntry{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.dudu.lizhen.customannotations;

import java.lang.reflect.Field;

/**
 * 实体类与表的映射测试
 * 目的 :通过自定义注解实现实体类与表的对应  select user_name ,user_age from user_table
 * Created by lizhen on 2018/1/30.
 */
public class UserEntryTest {
    public static void main(String[] args)  {
        try {
            //项目使用注解肯定会用到java反射,java反射的应用场景  Spring IOC ,jdbc ,自定义注解的实现
            Class<?> aClass = Class.forName("com.dudu.lizhen.customannotations.UserEntry");
            //getAnnotations  获取该类上使用了那些注解
//            Annotation[] annotations = aClass.getAnnotations();
//            for (Annotation annotation:annotations){
//                System.out.println(annotation);
//            }
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(" select ");
            Field[] declaredFields = aClass.getDeclaredFields();
            for (int i= 0;i<declaredFields.length;i++){
                SetProperty annotation = declaredFields[i].getAnnotation(SetProperty.class);
                String name = annotation.name();
                stringBuffer.append(name);
                if (i == declaredFields.length-1){
                    stringBuffer.append(" from ");
                }else{
                    stringBuffer.append(" , ");
                }

            }
            //获取某个注解对象
            SetTable annotation = aClass.getAnnotation(SetTable.class);
            String name = annotation.name();
            stringBuffer.append(name);
            System.out.println(stringBuffer.toString());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
查看效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值