Annotation是给类,方法或域上加的一种特殊的标记,可以通过反射取到注解的类型和值,从而完成某种特定的操作。
定义注解需要使用元注解,元注解有@Retention和@Target
//@Retention用来定义该注解在哪一个级别可用,在源代码中(SOURCE),类文件中(CLASS),或者运行时(RUNTIME)
//@Target用来定义注解将用在什么地方 类,方法,域...
比如:
@Retention(RetentionPolicy.RUNTIME) 表明在运行时保留该注解
@Target(ElementType.METHOD) 表明为方法注解,只能在方法声明中使用
注解不支持继承,注解元素的类型可以是基本类型,String,Class, enum, 和另一个注解。
没有元素的注解称为标记注解。
简单例子:
----------根据Java Bean建立数据库表-----------
//数据库字段属性枚举 public enum EOption { //可空 NONE, //非空 NOT_NULL, PRIMARY, UNIQUE; public boolean withIn(EOption[] options){ for(EOption o : options){ if(o==this){ return true; } } return false; } }
----------
//数据库字段类型枚举
public enum EType { //自动判断 AUTO, CHAR, VARCHAR, SMALLINT, INT, BIGINT, TEXT, BLOB, DATE; }
---------数据表注解------
@Retention(RUNTIME) @Target(TYPE) public @interface DTable { //表名 public String name() default ""; }
--------表字段注解------
@Retention(RUNTIME) @Target(FIELD) public @interface DColumn { //字段类型 EType type() default EType.VARCHAR; //类型相关值 int value() default 10; //字段名称 String name() default ""; //字段属性 EOption[] option() default EOption.NOT_NULL; }
--------测试Bean-----
@DTable(name="user_t") public class UserBean { @DColumn(type=EType.INT, option={EOption.NOT_NULL, EOption.PRIMARY}) private int id; @DColumn(30) private String name; @DColumn(type=EType.INT) private Integer age; @DColumn(30) private String email; @DColumn(type=EType.DATE) private String create_date; }
--------执行建表--------
public class TableCreator { private static String getCon(EOption[] options) { String ret = ""; if (EOption.NOT_NULL.withIn(options)) { ret += " NOT NULL "; } if (EOption.PRIMARY.withIn(options)) { ret += " PRIMARY KEY "; } if (EOption.UNIQUE.withIn(options)) { ret += " UNIQUE "; } return ret; } public static void main(String[] args) throws ClassNotFoundException { String classname = "dbAnnotation.UserBean"; Class<?> c = Class.forName(classname); DTable table = (DTable) c.getAnnotation(DTable.class); if (table == null) { System.out.println(c.getName() + " no DTable Annotation"); return; } String tableName = table.name(); if (tableName.length() < 1) { tableName = c.getName().toLowerCase(); } List<String> columnNames = new ArrayList<>(); for (Field f : c.getDeclaredFields()) { Annotation[] a = f.getDeclaredAnnotations(); if (a.length < 1) { continue; } DColumn dcol = (DColumn) a[0]; String cName = dcol.name().length() < 1 ? f.getName().toLowerCase() : dcol.name(); if (dcol.type() == EType.INT) { columnNames.add(cName + " INT" + getCon(dcol.option())); } else if (dcol.type() == EType.VARCHAR) { columnNames.add(cName + " VARCHAR(" + dcol.value() + ")" + getCon(dcol.option())); } } StringBuilder sb = new StringBuilder(); sb.append("create table "); sb.append(tableName); sb.append(" ( "); for (String s : columnNames) { sb.append(s); sb.append(", "); } sb.append(")"); System.out.println(sb.toString()); } }
-------输出------
create table user_t ( id INT NOT NULL PRIMARY KEY , name VARCHAR(30) NOT NULL , age INT NOT NULL , email VARCHAR(30) NOT NULL , )
-------
end