Generate SQL statements based on reflection

Source: Internet
Author: User

1 /**2 * Base Query Statement3 * Return type of property string sql4      * @author: Inkyi5 * Modification Time: May 11, 2016-morning 10:06:00<br/>6 * Function Description:<br/>7      * @paramClazz8      * @return"Id,name,sex ..."9      */Ten     PrivateString baseselectsql (Class clazz) { OneStringBuffer SB =NewStringBuffer (); Afield[] Fields =clazz.getdeclaredfields (); -          for(inti = 0; i < fields.length; i++) { -Field field =Fields[i]; theField.setaccessible (true ); - Sb.append (Field.getname ()); -Sb.append (","); -             Try { +Object object =Field.get (clazz); - System.out.println (object.tostring ()); +}Catch(IllegalArgumentException |illegalaccessexception e) { A e.printstacktrace (); at             } -         } -         intLastIndexOf = Sb.lastindexof (","); - Sb.delete (LastIndexOf, Sb.length ()); -         returnsb.tostring (); -}
1 /**2 * Basic Conditional statements3 * Only properties and values that are not empty are returned4      * @author: Inkyi5 * Modification Time: May 11, 2016-morning 10:52:26<br/>6 * Function Description:<br/>7      * @paramobj8      * @returnSELECT & delete-->id= ' 0 ' and name= ' Inkyi '9      *  Ten      */ One     PrivateString basewheresql (Object obj) { AStringBuffer SB =NewStringBuffer (); -class<?extendsObject> Clazz =Obj.getclass (); -field[] Fields =clazz.getdeclaredfields (); the         Try { -              for(inti = 0; i < fields.length; i++) { -Field field =Fields[i]; -Field.setaccessible (true); +String name =field.getname (); -Object value =field.get (obj); +class<?> type =Field.gettype (); A                 if(value!=NULL){ at                     //Processing Time Formats -                     if(Type = = Date.)class){ -Value = Dateutils.datestr ((Date) value, "Yyyy-mm-dd HH:mm:ss"); -                     } -Sb.append (name). Append ("=") -. Append ("'"). Append (Value). Append ("'") in. Append ("and"); -                 } to             } +}Catch(IllegalArgumentException |illegalaccessexception e) { - e.printstacktrace (); the         } *         intLastIndexOf = Sb.lastindexof ("and"); $ Sb.delete (LastIndexOf, Sb.length ());Panax Notoginseng         returnsb.tostring (); -}
1 /**2 * BASIC UPDATE statement3 * Only properties and values that are not empty are returned4      * @author: Inkyi5 * Modification Time: May 11, 2016-morning 11:11:25<br/>6 * Function Description:<br/>7      * @paramobj8      * @return>id= ' 0 ', name= ' Inkyi ', sex= ' 1 '9      */Ten     PrivateString baseupdatesql (Object obj) { OneStringBuffer SB =NewStringBuffer (); Aclass<?extendsObject> Clazz =Obj.getclass (); -field[] Fields =clazz.getdeclaredfields (); -         Try { the              for(inti = 0; i < fields.length; i++) { -Field field =Fields[i]; -Field.setaccessible (true); -String name =field.getname (); +Object value =field.get (obj); -class<?> type =Field.gettype (); +                 if(value!=NULL){ A                     //Processing Time Formats at                     if(Type = = Date.)class){ -Value = Dateutils.datestr ((Date) value, "Yyyy-mm-dd HH:mm:ss"); -                     } -Sb.append (name). Append ("=") -. Append ("'"). Append (Value). Append ("'") -. Append (","); in                 } -             } to}Catch(IllegalArgumentException |illegalaccessexception e) { + e.printstacktrace (); -         } the         intLastIndexOf = Sb.lastindexof (","); * Sb.delete (LastIndexOf, Sb.length ()); $         returnsb.tostring ();Panax Notoginseng}

Generate SQL statements based on reflection

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.