Skip to content

Commit c1fd88d

Browse files
committed
注解,反射
0 parents  commit c1fd88d

File tree

18 files changed

+535
-0
lines changed

18 files changed

+535
-0
lines changed

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java-annotation/java-annotation.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.zyj.annotation;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Field;
5+
6+
/**
7+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
8+
*
9+
* @author : 张勇杰
10+
* @date : 2018/11/10 17:38
11+
* @Version : v1.0
12+
* @description 使用反射读取注解的信息,模拟处理注解信息的流程
13+
**/
14+
public class AynasAnnotation {
15+
public static void main(String[] args) {
16+
try {
17+
Class clazz = Class.forName("com.zyj.annotation.Student");
18+
//获得类所有有效注解
19+
Annotation[] annotations = clazz.getAnnotations();
20+
for(Annotation a : annotations){
21+
/*ZyjTable zyjTable = (ZyjTable)a;
22+
System.out.println(zyjTable.value());*/
23+
System.out.println(a);
24+
}
25+
//获得类制定的注解
26+
ZyjTable zyjTable = (ZyjTable) clazz.getAnnotation(ZyjTable.class);
27+
System.out.println(zyjTable.value());
28+
Field[] fields = clazz.getDeclaredFields();
29+
for(Field f:fields){
30+
ZyjField zyjField = f.getAnnotation(ZyjField.class);
31+
System.out.println(zyjField.columnName()+"--"+zyjField.type()+"--"+zyjField.length());
32+
}
33+
//根据获得的表明,字段信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库种生成相关的表
34+
} catch (ClassNotFoundException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.zyj.annotation;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/11/10 17:26
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
@ZyjTable("tb_Student")
12+
@ZyjTable1("tb_Student1111")
13+
public class Student {
14+
@ZyjField(columnName = "id",type="int",length=10)
15+
private int id;
16+
@ZyjField(columnName = "age",type="int",length = 3)
17+
private int age;
18+
@ZyjField(columnName = "sname",type="String",length = 10)
19+
private String name;
20+
21+
22+
public int getId() {
23+
return id;
24+
}
25+
26+
public void setId(int id) {
27+
this.id = id;
28+
}
29+
30+
public int getAge() {
31+
return age;
32+
}
33+
34+
public void setAge(int age) {
35+
this.age = age;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.zyj.annotation;
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({ElementType.FIELD})
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface ZyjField {
11+
String columnName();
12+
String type();
13+
int length();
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.zyj.annotation;
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(RetentionPolicy.RUNTIME)
10+
public @interface ZyjTable {
11+
String value();
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.zyj.annotation;
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(RetentionPolicy.RUNTIME)
10+
public @interface ZyjTable1 {
11+
String value();
12+
}

java-base.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

java-reflect/java-reflect.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.zyj.bean;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/11/10 18:02
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class User {
12+
private int id;
13+
private int age;
14+
private String name;
15+
16+
public User() {
17+
}
18+
19+
public User(int id, int age, String name) {
20+
this.id = id;
21+
this.age = age;
22+
this.name = name;
23+
}
24+
25+
public int getId() {
26+
return id;
27+
}
28+
29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
33+
public int getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(int age) {
38+
this.age = age;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
}

0 commit comments

Comments
 (0)