-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathORMapping.java
More file actions
149 lines (124 loc) · 3.87 KB
/
ORMapping.java
File metadata and controls
149 lines (124 loc) · 3.87 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package annotation;
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
class ORM{
static void execute(MyRecord myRecord){
// 生成sql语句 select
// 完成相应的数据库操作
}
static void save(MyRecord myRecord){
// 生成相应的insert SQL语句
// 执行SQL
}
}
public class ORMapping {
public static void main(String[] args) {
MyRecord record1 = new MyRecord("123", "34"); // _id - 123 username 34
MyRecord record2 = new MyRecord("123", "test");
MyRecord record3 = new MyRecord("", "test1,test2,test3,test4");
ORM.execute(record1);
ORM.save(record1);
String sql1 = assembleSqlFromObj(record1);
String sql2 = assembleSqlFromObj(record2);
String sql3 = assembleSqlFromObj(record3);
System.out.println(sql1);
System.out.println(sql2);
System.out.println(sql3);
}
/**
* 通过注解来组装查询条件,生成查询语句
*/
public static String assembleSqlFromObj(Object obj) {
Table table = obj.getClass().getAnnotation(Table.class);
StringBuffer sql = new StringBuffer();
String tableName = table.value();
sql.append("select * from " + tableName + " where 1=1 ");
Field[] fileds = obj.getClass().getDeclaredFields();
for (Field f : fileds) {
String fieldName = f.getName(); // tt -> getTt
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Column column = f.getAnnotation(Column.class);
if (column != null) {
Method method = obj.getClass().getMethod(methodName);
String value = (String) method.invoke(obj); // id
if (value != null && !value.equals("")) {
if (!isNum(column.value()) && !isNum(value)) {
// 判断参数是不是 in 类型参数 1,2,3
if (value.contains(",")) {
sql.append(" and " + column.value() + " in (" + value + ") ");
} else {
sql.append(" and " + column.value() + " like '%" + value + "%' ");
}
} else {
sql.append(" and " + column.value() + "=" + value + " ");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return sql.toString();
}
/**
* 检查给定的值是不是id类型
*/
public static boolean isNum(String target) {
boolean isNum = false;
if (target.toLowerCase().contains("id")) {
isNum = true;
}
if (target.matches("\\d+")) {
isNum = true;
}
return isNum;
}
}
@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Table {
String value() default "";
}
@Inherited
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Column {
String value() default "";
}
@Table("MyRecord")
class MyRecord {
private String tt;
@Column("_id")
private String id;
@Column("username")
private String name;
public MyRecord(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getTt() {
return tt;
}
public void setTt(String tt) {
this.tt = tt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}