Packagecom.ann.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;/*** Field Custom annotations*/@Target ({Elementtype.field}) @Retention (retentionpolicy.runtime) @Inherited @documented Public@InterfaceColumn {String value ();}
Packagecom.ann.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;/*** Table Custom annotations*/@Target ({elementtype.type}) @Retention (retentionpolicy.runtime) @Inherited @documented Public@InterfaceTable {String value ();}
Packagecom.ann.test;/*** User entity usage Annotations*/@Table ("User") Public classUser {@Column ("id") Private intID; @Column ("UserName") PrivateString UserName; @Column ("Sex") Private intsex; @Column ("Mobile") PrivateString Mobile; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } Public intGetsex () {returnsex; } Public voidSetsex (intsex) { This. Sex =sex; } PublicString Getmobile () {returnMobile; } Public voidSetmobile (String mobile) { This. Mobile =Mobile; } }
Packagecom.ann.test;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;/*** test of simulating Entity to database table field*/ Public classTest { Public Static voidMain (string[] args) {User u=NewUser (); U.setusername ("Zhang San"); U.setsex (1); System.out.println (Parseuser (U)); } Public StaticString Parseuser (User u) {stringbuffer sb=NewStringBuffer (); Sb.append ("SELECT * from"); Try { //1.1 Load class with class loader, get table nameClass C = class.forname ("Com.ann.test.User"); //1.2 Find the annotations on the class and get the annotation instance if(C.isannotationpresent (Table.class) ) {Table T= (table) c.getannotation (table).class); String TableName=T.value (); Sb.append (TableName); } sb.append ("Where 1=1"); //2. Get field names and valuesfield[] fs =C.getdeclaredfields (); for(Field f:fs) {//2.1 Field namesString column = ""; if(F.isannotationpresent (Column).class) {Column fld= (column) f.getannotation (column).class); Column=Fld.value (); } //2.2 field ValueString FieldName =F.getname (); String GetMethod= "Get" +fieldname.substring (0, 1). toUpperCase () +fieldname.substring (1); Method Method=C.getmethod (GetMethod); Object Fieldvalue=method.invoke (U); if(FieldvalueinstanceofInteger && (integer) Fieldvalue = = 0){ Continue; } if(Fieldvalue! =NULL) {sb.append ("and"). Append (column). Append ("="); if(FieldvalueinstanceofString) {Sb.append ("'"). Append (Fieldvalue). Append ("'"); }Else{sb.append (fieldvalue); } } } } Catch(Exception e) {e.printstacktrace (); } returnsb.tostring (); }}
Output Result:
SELECT * from user where 1=1 and Username= ' Zhang San ' and Sex=1
Annotations (ii) mapping of simulated entities to database table fields