Java中内置了三种标准注解:
@Override
@Deprecated
@SuppressWarnings
提供了四种源注解:
@Target
@Retention
@Documented
@Inherited
下边的例子通过注解实现了对象到数据表的映射,利用反射机制生成创建表的SQL语句,实现ORM
自定义注解:
package com.gbx.anotation;
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 {
boolean primaryKey() default false;
boolean allowNull() default false;
boolean unique() default false;
}
package com.gbx.anotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* 定义关于数据库表的注解
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBtable {
String name() default "";
}
package com.gbx.anotation;
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.gbx.anotation;
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;
}
对象:
@DBtable(name = "Memeber")
public class Member {
@SQLString(30) //这里默认直接给value赋值,
public String firstName;
@SQLString(50)
public String lastName;
@SQLInteger
public Integer age;
@SQLString(value = 30, constraints = @Constraints(primaryKey = true))
public String handle;
public int memberCount;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public int getMemberCount() {
return memberCount;
}
public void setMemberCount(int memberCount) {
this.memberCount = memberCount;
}
}
反射处理:
package com.gbx.bz;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.jws.WebParam;
import com.gbx.anotation.Constraints;
import com.gbx.anotation.DBtable;
import com.gbx.anotation.SQLInteger;
import com.gbx.anotation.SQLString;
import com.gbx.po.Member;
public class Test{
public static String getConstraints(Constraints constraints) {
StringBuilder sb = new StringBuilder();
if (constraints.primaryKey()) {
sb.append(" PRIMARY KEY ");
}
if (constraints.unique()) {
sb.append(" UNIQUE ");
}
if (!constraints.allowNull()) {
sb.append(" NOT NULL ");
}
return sb.toString();
}
/*
* 利用反射机制实现ORM...
*/
public static void main(String[] args) throws ClassNotFoundException {
Class<?> cl = Class.forName(Member.class.getName());
DBtable dbtable = cl.getAnnotation(DBtable.class);
StringBuilder createSQL = new StringBuilder();
if (dbtable == null) {
System.out.println("没有DBTable的注解");
} else {
String tableName = dbtable.name();
if (tableName == "") {
tableName = cl.getSimpleName().toUpperCase();
}
createSQL.append("CREATE TABLE " + tableName + "(");
for (Field field : cl.getFields()) {
StringBuilder columInf = new StringBuilder();
Annotation[] annotations = field.getAnnotations();
if (annotations.length < 1) continue;
String columName = null;
if (annotations[0] instanceof SQLInteger) {
SQLInteger sInt = (SQLInteger) annotations[0];
columName = sInt.name();
if (columName.length() < 1) {
columName = field.getName().toUpperCase();
}
columInf.append(columName + " INT " + getConstraints(sInt.constraints()));
} else if (annotations[0] instanceof SQLString) {
SQLString sStr = (SQLString)annotations[0];
columName = sStr.name();
if (columName.length() < 1) {
columName = field.getName().toUpperCase();
}
columInf.append(columName + " VARCHAR " + "(" + sStr.value() + ")" + " " + getConstraints(sStr.constraints()));
}
createSQL.append("\n" + columInf.toString() + ",");
}
String createCommond = createSQL.substring(0, createSQL.length() - 1) + ");";
System.out.println(createCommond);
}
}
}
输出:
CREATE TABLE Memeber(
FIRSTNAME VARCHAR (30) NOT NULL ,
LASTNAME VARCHAR (50) NOT NULL ,
AGE INT NOT NULL ,
HANDLE VARCHAR (30) PRIMARY KEY NOT NULL );