原文出处:http://computerdragon.blog.51cto.com/6235984/1210969
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
whut.annotation;
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
//定义一个注解
@Target
(ElementType.METHOD)
//定义该注解将应用于什么地方,方法或者域
@Retention
(RetentionPolicy.RUNTIME)
//定义该注解在哪一个级别可用
public
@interface
UseCase {
//注解元素,可以指定默认值,在使用注解的时候,可以直接给元素赋值如id=5
public
int
id();
public
String description()
default
"no description"
;
//利用枚举来设置参数类型
public
enum
ParameterType { STRING, SHORT, INT, BOOL, LONG, OBJECT };
// 默认值,在使用注解的时候,只需要为元素赋值
public
ParameterType type()
default
ParameterType.STRING;
}
|
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
|
package
whut.annotationDB;
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
whut.annotationDB;
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
@Target
(ElementType.TYPE)
//类,接口或enum
@Retention
(RetentionPolicy.RUNTIME)
//定义表名的注解
public
@interface
DBTable {
public
String name()
default
""
;
}
///
package
whut.annotationDB;
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
@Target
(ElementType.FIELD)
//类,接口或enum
@Retention
(RetentionPolicy.RUNTIME)
public
@interface
SQLInteger {
String name()
default
""
;
//嵌套注解的功能,将column类型的数据库约束信息嵌入其中
Constraints constraints()
default
@Constraints
;
}
///
package
whut.annotationDB;
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
@Target
(ElementType.FIELD)
//类,接口或enum
@Retention
(RetentionPolicy.RUNTIME)
public
@interface
SQLString {
int
value()
default
0
;
String name()
default
""
;
//注解元素中引用别的注解,
Constraints constraints()
default
@Constraints
;
}
|
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
|
package
whut.annotationDB;
@DBTable
(name=
"MEMBER"
)
public
class
Member {
//在使用注解过程中,如果有元素是value,并且只有value需要赋值,
//则只需要在()中将值写入
@SQLString
(
30
)
private
String firstName;
@SQLString
(
50
)
private
String lastName;
@SQLInteger
private
Integer age;
@SQLString
(value=
30
,constraints=
@Constraints
(primaryKey=
true
))
private
String handle;
public
String getFirstName() {
return
firstName;
}
public
void
setFirstName(String firstName) {
this
.firstName = firstName;
}
public
String getLastName() {
return
lastName;
}
public
void
setLastName(String lastName) {
this
.lastName = lastName;
}
public
Integer getAge() {
return
age;
}
public
void
setAge(Integer age) {
this
.age = age;
}
public
String getHandle() {
return
handle;
}
public
void
setHandle(String handle) {
this
.handle = handle;
}
}
|
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package
whut.annotationDB;
import
java.lang.annotation.Annotation;
import
java.lang.reflect.Field;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.List;
public
class
TableCreator {
public
Connection getConnection() {
String user =
"root"
;
String password =
""
;
String serverUrl =
"jdbc:mysql://localhost:3306/carrent?user=root&password="
;
try
{
Class.forName(
"com.mysql.jdbc.Driver"
);
Connection con = DriverManager.getConnection(serverUrl, user,
password);
return
con;
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
//实现创建表
public
static
void
main(String[] args) {
TableCreator tc =
new
TableCreator();
tc.executeCreateDB(Member.
class
);
}
public
void
executeCreateDB(Class<?> entity) {
String sqlStr = explainAnnotation(entity);
Connection con = getConnection();
PreparedStatement psql =
null
;
if
(con !=
null
&& !sqlStr.equals(
"error"
)) {
try
{
psql = con.prepareStatement(sqlStr);
psql.execute();
}
catch
(SQLException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(psql !=
null
)
psql.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
try
{
if
(con !=
null
)
psql.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
else
System.out.println(
"failure to..."
);
}
// 真正的处理器,Class<?>必须用这个表明
public
String explainAnnotation(Class<?> entity) {
// 获取指定类型的注解
DBTable dbtable = entity.getAnnotation(DBTable.
class
);
if
(dbtable ==
null
) {
System.out.println(
"No DBTable annotation in class"
+ entity.getName());
return
"error"
;
}
else
{
String tableName = dbtable.name();
// 获取注解name值,即表名称
// 当没有设置name值,直接利用类的名作为表名
if
(tableName.length() <
1
)
tableName = entity.getName().toUpperCase();
// 转换大写
// 准备处理字段注解
List<String> columnsDefs =
new
ArrayList<String>();
// 获取该类的所有字段
for
(Field field : entity.getDeclaredFields()) {
String columnName =
null
;
// 获取该字段所有的注解
Annotation[] anns = field.getDeclaredAnnotations();
// Annotation[] anns=field.getAnnotations();
// 当有注解的时候
if
(anns.length >=
1
) {
// 判断注解的类型
if
(anns[
0
]
instanceof
SQLInteger) {
SQLInteger sInt = (SQLInteger) anns[
0
];
// 当没有name时候,将字段大写为列名
if
(sInt.name().length() <
1
)
columnName = field.getName().toUpperCase();
else
columnName = sInt.name();
columnsDefs.add(columnName +
" INT"
+ getConstraints(sInt.constraints()));
}
if
(anns[
0
]
instanceof
SQLString) {
SQLString sString = (SQLString) anns[
0
];
// 当没有name时候,将字段大写为列名
if
(sString.name().length() <
1
)
columnName = field.getName().toUpperCase();
else
columnName = sString.name();
columnsDefs.add(columnName +
" VARCHAR("
+ sString.value() +
")"
+ getConstraints(sString.constraints()));
}
}
}
StringBuilder createDB =
new
StringBuilder(
"CREATE TABLE "
+ tableName +
"("
);
for
(String cols : columnsDefs)
createDB.append(
" "
+ cols +
","
);
// 移除最后一个,号
String tableSQL = createDB.substring(
0
, createDB.length() -
1
)
+
");"
;
// 输出创建表的过程
System.out.println(
"Table Creation SQL is:\n"
+ tableSQL);
return
tableSQL;
}
}
// 返回指定的约束
public
String getConstraints(Constraints con) {
String constras =
""
;
if
(!con.allowNull())
constras +=
" NOT NULL"
;
if
(con.primaryKey())
constras +=
" PRIMARY KEY"
;
if
(con.unique())
constras +=
" UNIQUE"
;
return
constras;
}
}
|