码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-07-02 08:08:27      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:javabean   sql      class   注解   

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章部分代码

标签:javabean   sql      class   注解   

原文地址:http://blog.csdn.net/liangrui1988/article/details/36251509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!