?
think in java的例子:
?
?
class="java">package com.tch.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
	public String name() default "";
	
}
?
package com.tch.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
	public boolean primaryKey() default false;
	
	public boolean allowNull() default false;
	
	public boolean unique() default false;
	
}
?
package com.tch.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLInteger {
	String name() default "";
	
	Constraints constraints() default @Constraints;
	
}
?
package com.tch.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString {
	int value() default 0;
	
	String name() default "";
	
	Constraints constraints() default @Constraints;
	
}
?
然后使用这几个注解:
?
package com.tch.test.annotation;
@DBTable(name="member")
public class Member {
	@SQLString(30)
	private String firstName;
	
	@SQLString(50)
	private String lastName;
	
	@SQLInteger
	private Integer age;
	
	@SQLString(value=30,constraints=@Constraints(primaryKey=true))
	private String handle;
	
}
?
?
解析注解,生成SQL语句
?
package com.tch.test.annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class Test {
	public static <T> void annotationCheck(Class<T> clazz){
		DBTable c = clazz.getAnnotation(DBTable.class);
		if(c == null){
			System.out.println("没有注解");
		}
		Field[] field = clazz.getDeclaredFields();
		List<String> columnDef = new ArrayList<String>();
		for(Field f : field){
			SQLString ss = f.getAnnotation(SQLString.class);
			SQLInteger si = f.getAnnotation(SQLInteger.class);
			if(ss != null){
				columnDef.add(" "+f.getName()+" varchar("+ss.value()+") "+getConstraints(ss.constraints()));
			}
			if(si != null){
				columnDef.add(" "+f.getName()+" int "+getConstraints(si.constraints()));
			}
		}
		StringBuilder sb = new StringBuilder();
		sb.append("create table "+c.name()+"(");
		for(String s:columnDef){
			sb.append("\n"+s+" ,");
		} 
		String result = sb.substring(0, sb.length()-1);
		result = result+"\n)";
		System.out.println(result);
	}
	public static String getConstraints(Constraints con){
		String result = "";
		if(! con.allowNull()){
			result+=" not null ";
		}
		if(con.primaryKey()){
			result+=" primary key ";
		}
		if(con.unique()){
			result+=" unique ";
		}
		return result;
	}
	
	public static void main(String[] args) {
		
		annotationCheck(Member.class);
		
	}
	
}
?
?结果:
create table member( firstName varchar(30) not null , lastName varchar(50) not null , age int not null , handle varchar(30) not null primary key )
?
?
?