File tree Expand file tree Collapse file tree 13 files changed +211
-0
lines changed Expand file tree Collapse file tree 13 files changed +211
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .anotition ;
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 Col {
11
+ String name ();
12
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .anotition ;
2
+
3
+ import java .lang .reflect .Field ;
4
+
5
+
6
+ public class Main {
7
+ public static void main (String [] args ) throws ClassNotFoundException {
8
+ String key = "MAJIAXUEYUAN" ;
9
+
10
+ StringBuffer sb = new StringBuffer ();
11
+ sb .append ("select " );
12
+ // 反射拿到字节码文件
13
+ Class <?> forName = Class .forName ("com.blankj.custom.anotition.User" );
14
+
15
+ // 拿到成员属性
16
+ Field [] declaredFields = forName .getDeclaredFields ();
17
+
18
+ // 遍历属性
19
+ for (int i = 0 ; i < declaredFields .length ; i ++) {
20
+ // 拿到注解获取name值
21
+ Col col = declaredFields [i ].getDeclaredAnnotation (Col .class );
22
+ String name = col .name ();
23
+ sb .append (name );
24
+ if (i == declaredFields .length - 1 ) {
25
+ sb .append (" from " );
26
+ } else {
27
+ sb .append (" , " );
28
+ }
29
+ }
30
+ Table table = forName .getAnnotation (Table .class );
31
+ String name = table .name ();
32
+ sb .append (name + " " );
33
+ sb .append (" where user_name = '" + key + "'" );
34
+ System .out .println (sb .toString ());
35
+ // select user_name,user_age from tb_user;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .anotition ;
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 .TYPE )
9
+ @ Retention (RetentionPolicy .RUNTIME )
10
+ public @interface Table {
11
+ // @table(name="")
12
+ String name ();
13
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .anotition ;
2
+
3
+ @ Table (name = "tb_user" )
4
+ public class User {
5
+
6
+ @ Col (name = "user_name" )
7
+ private String userName ;
8
+ @ Col (name = "user_sex" )
9
+ private String userSex ;
10
+
11
+ public String getUserName () {
12
+ return userName ;
13
+ }
14
+
15
+ public void setUserName (String userName ) {
16
+ this .userName = userName ;
17
+ }
18
+
19
+ public String getUserSex () {
20
+ return userSex ;
21
+ }
22
+
23
+ public void setUserSex (String userSex ) {
24
+ this .userSex = userSex ;
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public class Benz implements Car {
4
+
5
+ @ Override
6
+ public void create () {
7
+ System .out .println ("码家学院的Benz生产成功。" );
8
+ }
9
+
10
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public interface BenzFactory {
4
+ static Car createCar () {
5
+ return new Benz ();
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public class Bmw implements Car {
4
+
5
+ @ Override
6
+ public void create () {
7
+ System .out .println ("码家学院的BMW生产成功。" );
8
+ }
9
+
10
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public interface BmwFactory {
4
+ static Car createCar () {
5
+ return new Bmw ();
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public interface Car {
4
+ void create ();
5
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .factory ;
2
+
3
+ public class Main {
4
+ public static void main (String [] args ) {
5
+ Car bmw = BmwFactory .createCar ();
6
+ Car benz = BenzFactory .createCar ();
7
+
8
+ bmw .create ();
9
+ benz .create ();
10
+ }
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .single ;
2
+
3
+ // 懒汉式(线程安全问题)
4
+ public class Single1 {
5
+ private static Single1 instance ;
6
+
7
+ private Single1 () {
8
+ System .out .println ("构造函数运行-----" );
9
+ }
10
+
11
+ public static Single1 getInstance () {
12
+ if (instance == null ) {
13
+ instance = new Single1 ();
14
+ }
15
+ return instance ;
16
+ }
17
+
18
+ public static void main (String [] args ) {
19
+ Single1 instance = Single1 .getInstance ();
20
+ Single1 instance2 = Single1 .getInstance ();
21
+ System .out .println (instance == instance2 );
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .single ;
2
+
3
+ // 懒汉式()
4
+ public class Single2 {
5
+ private static Single2 instance ;
6
+
7
+ private Single2 () {
8
+ System .out .println ("Single2构造函数运行-----" );
9
+ }
10
+
11
+ public static synchronized Single2 getInstance () {
12
+ if (instance == null ) {
13
+ instance = new Single2 ();
14
+ }
15
+ return instance ;
16
+ }
17
+
18
+ public static void main (String [] args ) {
19
+ Single2 instance = Single2 .getInstance ();
20
+ Single2 instance2 = Single2 .getInstance ();
21
+ System .out .println ("222:" + (instance == instance2 ));
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .custom .single ;
2
+
3
+ /**
4
+ * 码家学院
5
+ *
6
+ * @author Liao 单例模式,饿汉式
7
+ */
8
+ public class Single3 {
9
+
10
+ private static Single3 instance = new Single3 ();
11
+
12
+ private Single3 () {
13
+ System .out .println ("构造函数--" );
14
+ }
15
+
16
+ public static Single3 getInstance () {
17
+ return instance ;
18
+ }
19
+
20
+ public static void main (String [] args ) {
21
+ Single3 instance1 = Single3 .getInstance ();
22
+ Single3 instance2 = Single3 .getInstance ();
23
+ System .out .println ("333:" + (instance1 == instance2 ));
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments