原文:
http://blog.csdn.net/jstxzhangrui/article/details/52976953
前面两篇博客已经详细讲解了Java注解,这次我们做个小案例,实现基于自定义注解的类与表之间的映射,完成表的反向生成
首先是表名的注解
package com.shinerio.annotation
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 TableName {
public String name()
}
然后创建一个枚举类型,用来表示数据库字段类型
package com.shinerio.annotation;
public enum ColumnType {
Int,String,Double;
}
然后创建表的字段的注解
package com.shinerio.annotation;
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 TableColumn {
public String ColumnName();
public int value() default 0;
public ColumnType ColumnType();
}
字段约束注解
package com.shinerio.annotation;
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)
@interface Constraints {
boolean primarykey() default false;
boolean allowNull() default false;
boolean unique() default false;
}
最后我们定义一个Users做测试,添加注解并通过反射生成SQL语句
package com.shinerio.annotation;
import java.lang.reflect.Field;
@TableName(name = "USERS")
public class Users {
@TableColumn(ColumnName = "id", ColumnType = ColumnType.Int)
@Constraints(primarykey = true)
public int id;
@TableColumn(ColumnName = "name", ColumnType = ColumnType.String, value = 50)
public String name;
@TableColumn(ColumnName = "password", ColumnType = ColumnType.String, value = 50)
public String password;
@TableColumn(ColumnName = "height", ColumnType = ColumnType.Double)
public double height;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
TableName tableName = Users.class.getAnnotation(TableName.class);
if (tableName != null && !"".equals(tableName.name())) {
sb.append("CREATE TABLE " + tableName.name() + "(\n");
} else {
String name[] = Users.class.getName().split("\\.");
sb.append("CREATE TABLE "
+ (name.length == 0 ? "" : name[name.length - 1]) + "(\n");
}
for (Field field : Users.class.getDeclaredFields()) {
TableColumn tc = field.getAnnotation(TableColumn.class);
if (tc != null) {
sb.append(tc.ColumnName());
switch (tc.ColumnType()) {
case String:
sb.append(" VARCHAR2(" + tc.value() + ")");
break;
case Double:
sb.append(" NUMBER");
break;
case Int:
sb.append(" INT");
}
}
Constraints con = field.getAnnotation(Constraints.class);
if (con != null) {
if (con.primarykey()) {
sb.append(" PRIMARY KEY");
}else if (!con.allowNull()) {
sb.append(" NOT NULL");
}
if (con.unique()) {
sb.append(" UNIQUE");
}
}
sb.append(",\n");
}
sb.delete(sb.lastIndexOf(",\n"), sb.lastIndexOf(",\n")+",\n".length());
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append(")\n");
System.out.println(new String(sb));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
输出结果:
CREATE TABLE USERS(
id INT PRIMARY KEY,
name VARCHAR2(50),
password VARCHAR2(50),
height NUMBER
)