The code is written according to the course Web tutorial.
Annotations for custom classes:
Packagecom.immoc.test;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Inherited;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target; @Target ({elementtype.type}) @Retention (retentionpolicy.runtime) @ Inherited@documented Public@InterfaceTable {String value ();}
Annotations for custom methods:
1 Packagecom.immoc.test;2 3 Importjava.lang.annotation.Documented;4 ImportJava.lang.annotation.ElementType;5 Importjava.lang.annotation.Inherited;6 Importjava.lang.annotation.Retention;7 ImportJava.lang.annotation.RetentionPolicy;8 ImportJava.lang.annotation.Target;9 Ten @Target ({Elementtype.field}) One @Retention (retentionpolicy.runtime) A @Inherited - @Documented - Public@InterfaceColumn { the - String value (); -}
Java class
Packagecom.immoc.test; @Table ("User") Public classFilter {@Column ("id") Private intID; @Column ("User_name") PrivateString UserName; @Column ("Nick_name") PrivateString Nickname; @Column ("Age") Private intAge ; @Column ("City") PrivateString City; @Column ("Mail") PrivateString Mail; @Column ("Mobile") PrivateString Mobile; PublicString Getmobile () {returnMobile; } Public voidSetmobile (String mobile) { This. Mobile =Mobile; } PublicString Getmail () {returnMail; } Public voidSetmail (String mail) { This. Mail =Mail; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } }
Test class
Packagecom.immoc.test;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method; Public classTest { Public Static voidMain (string[] args) {Filter F1=NewFilter (); F1.setid (10); Filter F2=NewFilter (); F2.setusername ("Tzzt01"); F2.setage (18); Filter f3=NewFilter (); F3.setmail ("[Email protected],[email protected]"); String SQL1=query (F1); String SQL2=query (F2); String Sql3=query (F3); System.out.println (SQL1); System.out.println (SQL2); System.out.println (SQL3); } Private StaticString query (Filter f) {StringBuilder SB=NewStringBuilder (); //1. Get the classClass C =F.getclass (); //2. Get the name of the table Booleanexists = C.isannotationpresent (Table.class); if(!exists) { return NULL; } Table T= (table) c.getannotation (table).class); String TableName=T.value (); Sb.append ("SELECT * from"). Append (TableName). Append ("Where 1=1"); //3. Iterate through all the fieldsfield[] Farray =C.getdeclaredfields (); for(Field field:farray) {//4 working with each field's corresponding SQL//4.1 Get field name BooleanFexists = Field.isannotationpresent (Column.class); if(!fexists) { Continue; } Column Column= Field.getannotation (Column.class);//get Column.class's notesString columnName = Column.value ();//Get field name//4.2 Get the field valueString FieldName =Field.getname (); String Getmethodname= "Get" + fieldname.substring (0,1). toUpperCase () +fieldname.substring (1);//Get GetXXXObject Fieldvalue =NULL; Try{Method GetMethod=C.getmethod (getmethodname); Fieldvalue=Getmethod.invoke (f); } Catch(Exception e) {e.printstacktrace (); } //4.3 assembling SQL if(Fieldvalue = =NULL|| (FieldvalueinstanceofInteger && (integer) Fieldvalue = = 0)) { Continue; } sb.append ("and"). Append (FieldName). Append ("="); if(FieldvalueinstanceofString) { if((String) fieldvalue). Contains (",") {string[] values= (String) fieldvalue. Split (","); Sb.append ("In ("); for(String v:values) {sb.append ("'"). Append (v). Append ("'"). Append (","); } Sb.deletecharat (Sb.length ()-1); Sb.append (")"); } Else{sb.append ("'"). Append (Fieldvalue). Append ("'"); } } Else if(FieldvalueinstanceofInteger) {sb.append (fieldvalue); } } returnsb.tostring (); }}
A simple example of Java parsing annotations