Java generates additions and deletions to SQL statements based on entity objects

Source: Internet
Author: User

<pre name= "code" class= "java" >import java.lang.reflect.field;import Java.lang.reflect.modifier;import Java.util.list;import java.util.vector;import org.apache.commons.lang3.reflect.fieldutils;/** * @author Gary Huang *  Blog Address: http://blog.csdn.net/hfmbook/article/details/41290641 * * */public class Updatesql {private Object target; private String Idname; Private Object idvalue; Private SqlType Currenttype; public enum SqlType {INSERT, UPDATE, DELETE}public updatesql (sqltype sqltype, Object target) {this.target = target; swit  CH (sqltype) {case insert:currenttype = Sqltype.insert; Createinsert (); Case update:currenttype = sqltype.update; Createupdate ();  Break;case delete:currenttype = sqltype.delete; Createdelete (); Break;}} /** * Create with delete * */private void Createdelete () {String tableName = Gettablename (); field[] fields = Target.getclass (). Getdeclaredfields (); StringBuffer sqlbuffer = new StringBuffer (); Sqlbuffer.append ("DELETE from"). Append (TableName). Append ("wherE "); for (Field field:fields) {if (! Modifier.isstatic (Field.getmodifiers ())) {id id = field.getannotation (id.class); if (null! = ID) {sqlbuffer.append ( Field.getname ()). Append ("=?"); Param.add (Readfield (field));}}} This.sqlbuffer = Sqlbuffer.tostring ();} Public Object readfield (field field) {try {return Fieldutils.readfield (field, target, true);} catch (Exception e) {T Hrow New RuntimeException (Currenttype.name (), E);}} /** * Create with new statement * */private void Createupdate () {String tableName = Gettablename (); field[] fields = Target.getclass (). Getdeclaredfields (); StringBuffer sqlbuffer = new StringBuffer ();  Sqlbuffer.append ("UPDATE"). Append (TableName). Append ("SET"); for (Field field:fields) {if (! Modifier.isstatic (Field.getmodifiers ())) {id id = field.getannotation (id.class), if (ID = = null) {Sqlbuffer.append ( Field.getname ()). Append ("=?");  Param.add (Readfield (field)); }else{idname = Field.getname (); idvalue = Readfield (field);}}} if (idname = = null) {throw new RuntimeException ("Not fOund of "+ target.getclass () +" ' s ID ");} Sqlbuffer.append ("WHERE"). Append (Idname). Append ("=?"); Param.add (Idvalue); This.sqlbuffer = Sqlbuffer.tostring ();} /** * Get table name according to annotations * */private string Gettablename () {string tableName = null; Class<?> clazz = Target.getclass (); Table table = Clazz.getannotation (Table.class); if (null! = table) {tableName = Table.name (), if ("". Equalsignorecase (TableName)) {tableName = Clazz.getsimplename ();}} Else{tablename = Clazz.getsimplename ();} return tableName;} /** * Create INSERT statement * */private void Createinsert () {String tableName = Gettablename (); field[] fields = Target.getclass (). Getdeclaredfields (); StringBuffer sqlbuffer = new StringBuffer (); Sqlbuffer.append ("INSERT into"). Append (TableName). Append ("("); for (Field field:fields) {if (! Modifier.isstatic (Field.getmodifiers ())) {id id = field.getannotation (id.class), if (ID = = null) {Sqlbuffer.append ( Field.getname ()). Append (","); Param.add (Readfield (field)); }}}int length = Sqlbuffer.length (); SqlbuffEr.delete (length-1, Length). Append (") values (");  int size = Param.size (); for (int x=0;x<size;x++) {if (x! = 0) {sqlbuffer.append (","); }sqlbuffer.append ("?"); }sqlbuffer.append (")"); This.sqlbuffer = Sqlbuffer.tostring ();} Private list<object> param = new vector<object> ();p rivate String sqlbuffer;public list<object> GetParam () {return param;} Public String Getsqlbuffer () {return sqlbuffer;} Public String Getidname () {return idname;} Public Object Getidvalue () {return idvalue;} @java. Lang.annotation.Target (Value={java.lang.annotation.elementtype.type}) @java. Lang.annotation.Retention ( value=java.lang.annotation.retentionpolicy.runtime) public @interface Table {String name () default "";} @java. Lang.annotation.Target (Value={java.lang.annotation.elementtype.field}) @java. Lang.annotation.Retention ( value=java.lang.annotation.retentionpolicy.runtime) public @interface id{}}


Java generates additions and deletions to SQL statements based on entity objects

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.