Annotations (ii) mapping of simulated entities to database table fields

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.