java注解演示 映射生成表 示例 来自thinking in java 4 20章部分代码

java注解演示 映射生成表 示例  来自thinking in java 4 20章部分代码

package org.rui.annotation.database;

import java.lang.annotation.*;
/**
 * 生成一个数据库表 
 * @author lenovo
 *
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
  public String name() default "";
	
}

package org.rui.annotation.database;

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

/**
 * sql类型
 * @author lenovo
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString {
	int value() default 0;
	String name() default "";
	Constraints constraints() default @Constraints;
	

}

package org.rui.annotation.database;

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

/**
 *  sql类型
 * @author lenovo
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SqlInteger {
	int value() default 0;
	String name() default "";
	Constraints constraints() default @Constraints;

}

package org.rui.annotation.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * javaBean域准备的注解
 * @author lenovo
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
  boolean primaryKey() default false;
  boolean allowNull() default true;
  boolean unique() default false;
}

package org.rui.annotation.database;
/**
 * 如果要令嵌入的@Constraints注解中的unique()元素为true 并以此作为constraints()元素的默认值
 * 则需要如下定义该元素
 * @author lenovo
 *
 */
public @interface Uniqueness {
	Constraints constraints() default @Constraints(unique=true);
}

package org.rui.annotation.database;

@DBTable(name="MEMBER")
public class Member {
	
	@SQLString(30)	String firstName;
	@SQLString(50) String lastName;
	@SqlInteger Integer age;
	@SQLString(value=30,constraints=@Constraints(primaryKey=true))
	String handle;
	static int memberCount;
	

	//get
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public Integer getAge() {
		return age;
	}
	public String getHandle() {
		return handle;
	}
	

	@Override
	public String toString()
	{
		return handle;
	}

}

package org.rui.annotation.database;

import java.io.ObjectInputStream.GetField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
 * 相前资料请参考 thinking in  java 第4版 20章
 * @author lenovo
 *
 */
public class TableCreator {
	
	//解释注解 获得相应的值
	public static String getConstraints(Constraints con)
	{
		String constraints="";
		if(con.allowNull())
			constraints+="NOT NULL";
		if(con.primaryKey())
			constraints+="PRIMARY KEY";
		if(con.unique())
			constraints+="UNIQUE";
		return constraints;
	}
	
	
	
	public static void main(String[] args) throws ClassNotFoundException {
		String[] arr=new String[]{"org.rui.annotation.database.Member"};
		if (arr.length<1)
		{
			System.out.println("0000");
			System.exit(0);
		}
		
		
		
		for(String className:arr)
		{
			Class<?> clzz=Class.forName(className);
			DBTable table=clzz.getAnnotation(DBTable.class);
			if(table==null)
			{
				System.out.println("dbtable annotaions in class:"+className);
				continue;
			}
			
		 
		   
			String tableName=table.name();
			if(tableName.length()<1)
				tableName=clzz.getName().toUpperCase();
			
			 //结果集合
			 List<String> columnDefs=new ArrayList<String>();
			   
			//获取所有字段
			for(Field f:clzz.getDeclaredFields())
			{
				String columnName =null;
				//获取字段上面的注解
				Annotation[] annot=f.getAnnotations();
				if(annot.length<1)continue;
				//如果是SqlInteger
				if(annot[0] instanceof SqlInteger)
				{
					SqlInteger sInt=(SqlInteger)annot[0];
					//如果没有指定名称    使用字段名称
					if(sInt.name().length()<1)
					{
						columnName=f.getName().toUpperCase();
					}else
					{
						columnName=sInt.name();
					}
					
					columnDefs.add(columnName+" INT"+getConstraints(sInt.constraints()));
				}
				//如果是string 内型
				if(annot[0] instanceof SQLString)
				{
					SQLString sStr=(SQLString)annot[0];
					if(sStr.name().length()<1)
					{
						columnName=f.getName().toUpperCase();
					}else
					{
						columnName=sStr.name();
					}
					columnDefs.add(columnName+" VARCHAR("+sStr.value()+")"+
					getConstraints(sStr.constraints()));
				}
				
				StringBuilder sql=new StringBuilder("CREATE TABLE "+tableName+"( ");
				for(String c:columnDefs)
				{
					sql.append("\n   "+c+",");//拼接字段
					//remove trailing comma
					String tableCreate=sql.substring(0,sql.length()-1)+");";
					System.out.println("table ceration sql fro "+className+" is :\n "+tableCreate);
				}
			}
		}
		
	}

}
/**output:
 table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL,
   AGE INTNOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL,
   AGE INTNOT NULL);
table ceration sql fro org.rui.annotation.database.Member is :
 CREATE TABLE MEMBER( 
   FIRSTNAME VARCHAR(30)NOT NULL,
   LASTNAME VARCHAR(50)NOT NULL,
   AGE INTNOT NULL,
   HANDLE VARCHAR(30)NOT NULLPRIMARY KEY);

 */


java注解演示 映射生成表 示例 来自thinking in java 4 20章部分代码,布布扣,bubuko.com

java注解演示 映射生成表 示例 来自thinking in java 4 20章部分代码

上一篇:关于消息推送的补充,主要介绍服务端的实现,包含object c 版本 c 版本 java 版本 php 版本 (转)


下一篇:Android学习 第十一周