定义几个注解类
package cn.martin.core.service.anno.e2;
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 true;
boolean unique() default false;
}
package cn.martin.core.service.anno.e2
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 cn.martin.core.service.anno.e2
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 cn.martin.core.service.anno.e2
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
}
pojo添加注解
package cn.martin.core.service.anno.e2;
@DBTable(name="TB_MEMBER")
public class Member {
@SQLString(30)
private String firstname;
@SQLInteger
private int age;
@SQLString(value=30,constraints=@Constraints(primaryKey=true))
String id;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Member [id=" + id + "]";
}
}
注解处理类
package cn.martin.core.service.anno.e2
import java.lang.annotation.Annotation
import java.lang.reflect.Field
import java.util.ArrayList
import java.util.List
public class TableCreate {
public static void main(String[] args) throws ClassNotFoundException {
String[] tyleArys = new String [] {"cn.martin.core.service.anno.e2.Member"}
createSql(tyleArys)
}
public static void createSql(String[] args) throws ClassNotFoundException{
for (String className : args) {
Class<?> cls = Class.forName(className)
DBTable dbTable = cls.getAnnotation(DBTable.class)
if(dbTable == null){
continue
}
String tableName = dbTable.name()
if(tableName.length() < 1){
tableName = cls.getName().toUpperCase()
}
List<String> columnsDef = new ArrayList<String>()
for (Field field : cls.getDeclaredFields()) {
String columnName = null
Annotation[] anns = field.getDeclaredAnnotations()
if(anns.length < 0){
continue
}
if(anns[0] instanceof SQLInteger){
SQLInteger sQLInteger = (SQLInteger)anns[0]
if(sQLInteger.name().length() < 1){
columnName = field.getName().toUpperCase()
}else{
columnName = sQLInteger.name()
}
columnsDef.add( columnName +" INT "+ getConstraints(sQLInteger.constraints()))
}
if(anns[0] instanceof SQLString){
SQLString sQLString = (SQLString)anns[0]
if(sQLString.name().length() < 1){
columnName = field.getName().toUpperCase()
}else {
columnName = sQLString.name()
}
columnsDef.add( columnName + " varchar ("+sQLString.value()+ ")" + getConstraints(sQLString.constraints()))
}
}
StringBuilder sb = new StringBuilder("create table "+tableName + " ( ")
for (String column : columnsDef) {
sb.append("\n" + column + " ,")
}
String createTable = sb.substring(0,sb.length()-1)+" );"
System.out.println(createTable)
}
}
public static String getConstraints(Constraints con){
String constaints = ""
if(!con.allowNull()){
constaints += " not null "
}
if(con.primaryKey()){
constaints += " primary key "
}
if(con.unique()){
constaints += " unique "
}
return constaints
}
}
测试结果
create table TB_MEMBER (
FIRSTNAME varchar (30) ,
AGE INT ,
ID varchar (30) primary key );