Skip to content

Commit 024e637

Browse files
committed
自定义注解实战:自定义 orm 工具。生成 sql 语句 ok ~
Signed-off-by: pythonCat <pythoncat@qq.com>
1 parent 137d118 commit 024e637

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.intellij.cat.orm;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* 对表字段的注解
10+
*/
11+
@Target(ElementType.FIELD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface BindField {
14+
15+
// `_uid` int(32) NOT NULL AUTO_INCREMENT,
16+
17+
String columnName() default "";
18+
19+
/**
20+
* 字段类型:varchar , int(25)
21+
*
22+
* @return field type
23+
*/
24+
String type();
25+
26+
boolean notNull() default false;
27+
28+
boolean primaryKey() default false;
29+
30+
boolean unique() default false;
31+
32+
boolean autoIncrement() default false;
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.intellij.cat.orm;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(value = ElementType.TYPE)
9+
@Retention(value = RetentionPolicy.RUNTIME)
10+
public @interface BindTable {
11+
12+
/**
13+
* table name
14+
*
15+
* @return table name
16+
*/
17+
String value();
18+
19+
String charset() default "utf8";
20+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.intellij.cat.orm;
2+
3+
import javax.annotation.Nullable;
4+
import java.lang.reflect.Field;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Locale;
8+
import java.util.Objects;
9+
10+
/**
11+
* 自定义注解解析类
12+
*/
13+
public class TbUserParser {
14+
15+
private TbUserParser() {
16+
}
17+
18+
private static TableInfo parseClass() {
19+
20+
BindTable annotation = (BindTable) ((Class) User.class).getAnnotation(BindTable.class);
21+
// System.out.println(annotation);
22+
String charset = annotation.charset();
23+
String tbName = annotation.value();
24+
return new TableInfo(charset, tbName);
25+
26+
}
27+
28+
29+
private static List<FieldInfo> parserField() {
30+
31+
Field[] fields = User.class.getDeclaredFields();
32+
ArrayList<FieldInfo> infoSet = new ArrayList<>();
33+
34+
for (Field f : fields) {
35+
36+
// 没有添加该注解的字段就会返回 null
37+
@Nullable
38+
BindField bindF = f.getAnnotation(BindField.class);
39+
40+
// System.out.println("bindF==" + bindF);
41+
if (Objects.nonNull(bindF)) {
42+
String columnName = bindF.columnName();
43+
String type = bindF.type();
44+
boolean unique = bindF.unique();
45+
boolean autoIncrement = bindF.autoIncrement();
46+
boolean notNull = bindF.notNull();
47+
boolean primaryKey = bindF.primaryKey();
48+
FieldInfo info = new FieldInfo(columnName, type, notNull,
49+
autoIncrement, unique, primaryKey);
50+
infoSet.add(info);
51+
}
52+
}
53+
return infoSet;
54+
}
55+
56+
57+
public static String createTable() {
58+
TableInfo tableInfo = parseClass();
59+
List<FieldInfo> fieldInfoSet = parserField();
60+
String head = String.format(Locale.getDefault(),
61+
"CREATE TABLE IF NOT EXISTS %s(", tableInfo.tbName);
62+
StringBuilder item = new StringBuilder();
63+
for (FieldInfo fi : fieldInfoSet) {
64+
// `chat_id` INT UNSIGNED AUTO_INCREMENT,
65+
item.append(String.format(Locale.getDefault(), "`%s` ", fi.columnName))
66+
.append(String.format(Locale.getDefault(), "%s ", fi.type))
67+
.append(fi.primaryKey ? "PRIMARY KEY " : "")
68+
.append(fi.notNull ? "NOT NULL " : "")
69+
.append(fi.unique ? "UNIQUE " : "")
70+
.append(fi.autoIncrement ? "AUTO_INCREMENT," : ",");
71+
}
72+
if (item.charAt(item.length() - 1) == ',') {
73+
item.deleteCharAt(item.length() - 1);
74+
}
75+
String tail = String.format(Locale.getDefault(), ") CHARSET=%s;", tableInfo.charset);
76+
return head + item + tail;
77+
}
78+
79+
80+
static class TableInfo {
81+
String charset;
82+
String tbName;
83+
84+
TableInfo(String charset, String tbName) {
85+
this.charset = charset;
86+
this.tbName = tbName;
87+
}
88+
}
89+
90+
static class FieldInfo {
91+
String columnName;
92+
String type;
93+
boolean notNull;
94+
boolean autoIncrement;
95+
boolean unique;
96+
boolean primaryKey;
97+
98+
FieldInfo(String columnName, String type, boolean notNull,
99+
boolean autoIncrement, boolean unique, boolean primaryKey) {
100+
this.columnName = columnName;
101+
this.type = type;
102+
this.notNull = notNull;
103+
this.autoIncrement = autoIncrement;
104+
this.unique = unique;
105+
this.primaryKey = primaryKey;
106+
}
107+
}
108+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.intellij.cat.orm;
2+
3+
/**
4+
* 对应到 数据库中的一条记录
5+
*/
6+
@BindTable(value = "tb_user")
7+
public class User {
8+
9+
public static final int GENDER_SECRET = 0;
10+
public static final int GENDER_MAIL = 1;
11+
public static final int GENDER_FEMAIL = 2;
12+
13+
@BindField(type = "varchar(50)", notNull = true, columnName = "_name")
14+
private String name;
15+
@BindField(type = "int(3)", columnName = "_age")
16+
private int age;
17+
@BindField(type = "int(1)", notNull = true, columnName = "_gender")
18+
private int gender;
19+
20+
@BindField(type = "int(1)", primaryKey = true, columnName = "_uid")
21+
private long uid;
22+
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public int getAge() {
33+
return age;
34+
}
35+
36+
public void setAge(int age) {
37+
this.age = age;
38+
}
39+
40+
public int getGender() {
41+
return gender;
42+
}
43+
44+
public void setGender(int gender) {
45+
this.gender = gender;
46+
}
47+
48+
public long getUid() {
49+
return uid;
50+
}
51+
52+
public void setUid(long uid) {
53+
this.uid = uid;
54+
}
55+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* 注解实践
3+
* 通过自定义注解,使用注解,解析注解三部曲完成一个简单的 orm 功能
4+
*/
5+
package com.intellij.cat.orm;

src/test/java/TestTableParse.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import com.intellij.cat.orm.TbUserParser;
2+
import org.junit.Test;
3+
4+
public class TestTableParse {
5+
6+
7+
@Test
8+
public void testParse() {
9+
10+
String table = TbUserParser.createTable();
11+
12+
System.out.println("table==\n" + table);
13+
}
14+
}

0 commit comments

Comments
 (0)