前言
前幾天學(xué)習(xí)了反射和自定義注解,剛好工作中遇到一個(gè)小問題:前臺(tái)傳遞到后臺(tái)的必填字段為空,導(dǎo)致不能插入數(shù)據(jù)庫(kù)。就是這樣一個(gè)小問題,讓我考慮到是否可以做一個(gè)通用的方法,讓前臺(tái)傳遞過來(lái)的必填字段在后臺(tái)也校驗(yàn)一遍,如果傳遞為空,則把響應(yīng)字段返回提示。因此,我考慮的是用注解的方式,在必填字段上面定義,利用反射得到必填字段的字段名,判斷是否為空,并返回響應(yīng)的信息。
需求模擬
假設(shè)客戶有:姓名,年齡,地址,手機(jī)號(hào)碼,身份證號(hào)等信息,而我們是做金融業(yè)務(wù),所以關(guān)鍵是看客戶的三要素:姓名,身份證號(hào),手機(jī)號(hào)碼。我們要保證前臺(tái)傳遞過來(lái)的這三個(gè)值不為空。
廢話不多說(shuō),直接上代碼。只看紅框里面的即可。
目錄結(jié)構(gòu)
客戶信息類:customer
這個(gè)是個(gè)實(shí)體類,我們?cè)冢盒彰矸葑C號(hào)碼,手機(jī)號(hào)碼上都用了我們的自定義注解。
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
|
package com.dao.chu.po; /** * * <p>title: customer</p> * <p>description:客戶信息實(shí)體 </p> */ public class customer { private int id; @isrequired private string name; // 姓名 @isrequired private string idnum; // 身份證號(hào)碼 @isrequired private string phone; // 手機(jī)號(hào) private string sex; // 性別 private int age; // 年齡 private string address; // 地址 @override public string tostring() { return "customer [id=" + id + ", name=" + name + ", idnum=" + idnum + ", phone=" + phone + ", sex=" + sex + ", age=" + age + ", address=" + address + "]" ; } public int getid() { return id; } public void setid( int id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getidnum() { return idnum; } public void setidnum(string idnum) { this .idnum = idnum; } public string getphone() { return phone; } public void setphone(string phone) { this .phone = phone; } public string getsex() { return sex; } public void setsex(string sex) { this .sex = sex; } public int getage() { return age; } public void setage( int age) { this .age = age; } public string getaddress() { return address; } public void setaddress(string address) { this .address = address; } } |
自定義注解類:isrequired
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.dao.chu.po; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; /** * * <p>title: isrequired</p> * <p>description: 字段是否必填 </p> */ @retention (value = retentionpolicy.runtime) @target (value = {elementtype.field}) public @interface isrequired { /** * * <p>title: isrequired</p> * <p>description:true:必填 false:非必填 </p> * @return */ boolean isrequired() default true ; } |
關(guān)鍵工具類:poutils
我們?cè)谶@個(gè)類里面主要用了反射的知識(shí),得到帶有自定義注解的字段,并取得這個(gè)對(duì)象的值進(jìn)行判斷
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
|
package com.dao.chu.po; import java.beans.introspectionexception; import java.beans.propertydescriptor; import java.lang.reflect.field; import java.lang.reflect.invocationtargetexception; import javax.jws.webresult; import com.sun.xml.internal.ws.util.stringutils; /** * * <p>title: poutils</p> * <p>description:po操作工具類 </p> */ @suppresswarnings ( "unused" ) public class poutils { /** * <p>title: getproperties</p> * <p>description: 獲取javabean屬性通用方法 </p> * @param t * @param beanname * @return * @throws illegalaccessexception * @throws illegalargumentexception * @throws invocationtargetexception * @throws introspectionexception */ private static <t> object getproperties(t t, string beanname) throws illegalaccessexception, illegalargumentexception, invocationtargetexception, introspectionexception { object namevalue = new propertydescriptor(beanname, t.getclass()).getreadmethod().invoke(t); return namevalue; } /** * <p>title: isfieldblank</p> * <p>description:判斷前臺(tái)傳過來(lái)的必填字段是否為空 ,不正確則將相應(yīng)字段返回 </p> * @param t * @return * @throws illegalaccessexception * @throws illegalargumentexception * @throws invocationtargetexception * @throws introspectionexception */ public static <t> respbody isfieldblank(t t) throws illegalaccessexception, illegalargumentexception, invocationtargetexception, introspectionexception { respbody respbody = new respbody(); stringbuffer sb = new stringbuffer(); field[] declaredfields = t.getclass().getdeclaredfields(); for (field field : declaredfields) { field.setaccessible( true ); string name = field.getname(); boolean fieldhasanno = field.isannotationpresent(isrequired. class ); if (fieldhasanno) { isrequired annotation = field.getannotation(isrequired. class ); boolean required = annotation.isrequired(); if (required) { object value = getproperties(t, name); if ( null == value) { sb.append(name + "," ); } } } } if ( null ==sb.tostring()|| "" .equals(sb.tostring())) { respbody.issuccess(); } respbody.setsuccess( false ); respbody.setmsg(sb.tostring().substring( 0 ,sb.tostring().lastindexof( "," )) + " is required" ); return respbody; } } |
respbody:響應(yīng)實(shí)體類
封裝了響應(yīng)的成功失敗以及一些信息
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
|
package com.dao.chu.po; /** * * <p>title: respbody</p> * <p>description: 響應(yīng)實(shí)體類</p> */ public class respbody { private boolean issuccess = true ; private string msg; private object data; public boolean issuccess() { return issuccess; } public void setsuccess( boolean issuccess) { this .issuccess = issuccess; } public string getmsg() { return msg; } public void setmsg(string msg) { this .msg = msg; } public object getdata() { return data; } public void setdata(object data) { this .data = data; } public respbody( boolean issuccess, string msg, object data) { super (); this .issuccess = issuccess; this .msg = msg; this .data = data; } public respbody( boolean issuccess, string msg) { super (); this .issuccess = issuccess; this .msg = msg; } public respbody() { } @override public string tostring() { return "returnbody [issuccess=" + issuccess + ", msg=" + msg + ", data=" + data + "]" ; } } |
測(cè)試類:isrequiredtest
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
|
package com.dao.chu.po; /** * * <p>title: isrequiredtest</p> * <p>description: 必填成員變量測(cè)試類</p> */ public class isrequiredtest { public static void main(string[] args) { customer customer = new customer(); try { //=========第一次不賦值========== respbody respbody = poutils.isfieldblank(customer); //不通過則返回提示信息 if (!respbody.issuccess()) { system.out.println( "1." +respbody.getmsg()); } //=========第二次給姓名賦值========== customer.setname( "張三" ); respbody = poutils.isfieldblank(customer); //不通過則返回提示信息 if (!respbody.issuccess()) { system.out.println( "2." +respbody.getmsg()); } } catch (exception e) { e.printstacktrace(); } } } |
輸出結(jié)果
第一次三個(gè)值都為空,提示三個(gè)都是必填的,第二次因?yàn)樾彰x值了,所以提示另外兩個(gè)是必填的,本次實(shí)驗(yàn)宣告結(jié)束,本人知識(shí)有限,若有更好的方法歡迎指正
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/daochuwenziyao/article/details/77755314?utm_source=tuicool&utm_medium=referral