Directly on the code
Package Com.guoxinet.o2o.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.inherited;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @ Retention (retentionpolicy.runtime) @Inherited @documentedpublic @interface Column {String value ();}
Package Com.guoxinet.o2o.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.inherited;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.type) @ Retention (retentionpolicy.runtime) @Inherited @documentedpublic @interface Table {String value ();}
Package com.guoxinet.o2o.annotation; @Table ("Tb_user") Public classUser {@Column ("user_id") Private intuserId; @Column ("user_name") PrivateString UserName; @Column ("Address") PrivateString address; @Column ("Email") PrivateString Email; Public intgetUserId () {returnuserId; } Public voidSetuserid (intuserId) { This. UserId =userId; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; }}
Package com.guoxinet.o2o.annotation; @Table ("tb_department") Public classDepartMent {@Column ("department_id") Private intDepartmentID; @Column ("name") PrivateString name; Public intGetdepartmentid () {returnDepartmentID; } Public voidSetdepartmentid (intDepartmentID) { This. DepartmentID =DepartmentID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } }
Package com.guoxinet.o2o.annotation;import Java.lang.reflect.field;import Java.lang.reflect.Method; Public classClient { Public Static voidMain (string[] args) {User User=NewUser (); User.setuserid (1); User.setemail ("[email protected],[email protected],[email protected]"); GetSQL (user); DepartMent DM=NewDepartMent (); Dm.setdepartmentid ( -); Dm.setname ("Technical Department"); GetSQL (DM); } Public StaticString getsql (Object ob) {StringBuilder sb=NewStringBuilder (); //Get class@SuppressWarnings ("Rawtypes") Class C=Ob.getclass (); //get the name of the table@SuppressWarnings ("unchecked") Boolean isexist= C.isannotationpresent (Table.class); if(!isexist) { return ""; } Table Table= (table) c.getannotation (table).class); String TableName=Table.value (); Sb.append ("SELECT * from"). Append (TableName). Append ("where 1=1"); //Traverse Propertiesfield[] Farray =C.getdeclaredfields (); for(Field Field:farray) {Boolean fisexist= Field.isannotationpresent (Column.class); if(!fisexist) { Continue; } Column Column= (column) field.getannotation (column).class); //Column NameString ColumnName =Column.value (); //get the value of the propertyString MethodName ="Get"+field.getname (). SUBSTRING (0,1). toUpperCase () + field.getname (). SUBSTRING (1); Object Fieldvalue=NULL; Try{Method Method=C.getmethod (methodName); Fieldvalue=Method.invoke (OB); if(NULL= = Fieldvalue | | (Fieldvalue instanceof Integer &&0==(Integer) fieldvalue)) {System. out. println (Fieldvalue); Continue; } sb.append (" and"). Append (ColumnName); if(fieldvalue instanceof String) {if((String) fieldvalue). Contains (",") ) {string[] Sarray= (String) fieldvalue. Split (","); Sb.append ("inch"). Append ("("); for(String s:sarray) {sb.append ("'"). Append (s). Append ("',"); } Sb.deletecharat (Sb.length ()-1); Sb.append (")"); }Else{sb.append (" = "); Sb.append ("'"). Append (Fieldvalue). Append ("'"); } }Else{sb.append (" = "). Append (Fieldvalue); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} sb.append (";"); System. out. println (Sb.tostring ()); return NULL; }}
Annotated learning examples (simulating hibernate,table,column annotations, assembling SQL)