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();
}
}
}
查看效果: