hibernate 里面通过注解,映射等手段,可以自动生成表,现在模拟实现。随便学学注解如何使用。
首先,我们要定义几个注解:
Table 用于定义表名字,类型使用Type
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 Table {
// 表名
String name() default "";
}
Types,定义一下常用的类型,这里我写得比较乱,也可以使用 枚举 等其他方法,方便使用
import java.util.HashMap;
import java.util.Map;
public class Types {
// 自己定义的一些基本类型,和数据对应就行了,就是组成字符串
// 这是临时的办法,需要大家从新设计
public static final String VARCHAR = "VARCHAR";
public static final String INT = "INT";
public static final String BOOLEAN = "BOOLEAN";
// 默认长度,和数据库对应
public static final int STRING_LENGTH =32;
public static final int INT_LENGTH = 10;
// 将类型 已经默认长度,放入集合
public static Map map = new HashMap();
static{
map.put(VARCHAR, STRING_LENGTH);
map.put(INT, INT_LENGTH);
map.put(BOOLEAN, 0);
}
public static String getType(String type,int length){
if(type == null){
throw new RuntimeException("Not recognized the type :"+type);
}
// 防止boolean 这类型
if( length > 0){
return type+"("+length+")";
}
return type;
}
public static String getString(){
return getStirng(VARCHAR, STRING_LENGTH);
}
public static String ge