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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldComment {


    /**
     *  字段名称
     */
    public String name() default "";

    /***
     * 数据库字段名称
     * @return
     */
    public String value() default "";


    /***
     * 字段解释说明
     * @return
     */
    public String desc() default "";


    /**
     * 字段名称
     * @return
     */
    public String fieId() default "";


}

@Data
public class RContactVO {

    private Long rUserId;

    @FieldComment(name = "联系人姓名", fieId = "name")
    private String name;

    @FieldComment(name = "联系人手机", fieId = "mobilePhone")
    private String mobilePhone;

    public static void main(String[] args) {
        RContactVO rContactVO = new RContactVO();
        // 得到类对象
        Class<?> userCla = rContactVO.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        FieldComment attr;
        for (Field f : fs) {
            f.setAccessible(true); // 设置些属性是可以访问的

            if (f.isAnnotationPresent(FieldComment.class)) {
                attr = f.getAnnotation(FieldComment.class);
                if (attr != null) {
                    //打印对应的参数
                    System.out.println(attr.name());
                }
            }
        }

    }

}

上一篇 下一篇