Scene
When an order is placed, an order and multiple order details are generated, that is, an order object and multiple OrderInfo objects.
When multiple OrderInfo objects are in the library, the DAO layer is called to the upper layer, and the interface entry is LIST<ORDERINFO>, then the implementation of the interfaces needs to convert the list<orderinfo> to a full SQL statement.
Such as:
Insert into Values ('18-year-old Zheng bin ' freshman '), ('22-year-old Zheng bin ' A ' Seniors ');
The following is a reflection method that generates an array of data with placeholders for the SQL, and padding placeholders.
Source
Private Static voidInsertObject (list<?> objectList)throwsillegalaccessexception {/*** For example: * SQL statement: INSERT into person (name, age, Info) VALUES (' ... ', ' ... '); * Where values are changed to placeholders: * SQL statement: INSERT into person (name, age, Info) VALUES (?,?,?); * Retrieve all data values of the Object array * [Zhengbin, 21, to graduate]*/ //determine the number of placeholders (that is, the number of fields in the object that are not null) intColumnnum = 0; //column name for inserting dataStringBuilder columns =NewStringBuilder ("("); //the value of the padding placeholder (that is, the value of a field that is not NULL in the object)List<object> valueslist =NewArraylist<object>(); //if it is empty, do not execute if(Collectionutil.isempty (objectList)) {return; } //determines the field of the inserted object through the first object in the listObject object = Objectlist.get (0); Class Clazz=Object.getclass (); Field[] Fields=Clazz.getdeclaredfields (); for(Field field:fields) {field.setaccessible (true); if(Field.get (object)! =NULL) {Columnnum++; Columns.Append (Field.getname ()). Append (", "); Valueslist.add (Field.get (object)); }} columns.replace (Columns.lastindexof (","), Columns.length (), ")"); //get all the values for(inti = 1; I < objectlist.size (); i++) { for(Field field:fields) {field.setaccessible (true); if(Field.get (Objectlist.get (i))! =NULL) {Valueslist.add (Field.get (Objectlist.get (i))); } } } //determine the placeholder for an Object '? 'StringBuilder Zhanweifucolumn =NewStringBuilder ("("); for(inti = 0; i < Columnnum; i++) {zhanweifucolumn.append ("?, "); } zhanweifucolumn.replace (Zhanweifucolumn.lastindexof (","), Zhanweifucolumn.length (), ")"); //to determine all placeholders intObjectnum =objectlist.size (); StringBuilder Zhanweifu=NewStringBuilder (); for(intj = 0; J < Objectnum; J + +) {zhanweifu.append (zhanweifucolumn.tostring ()). Append (", "); } zhanweifu.replace (Zhanweifu.lastindexof (","), Zhanweifu.length (), ""); //Generate final SQLString sql = "INSERT into" + Object.getclass (). Getsimplename (). toLowerCase () + "+ columns +" VALUES "+zhanweifu.tostring (); SYSTEM.OUT.PRINTLN (SQL); System.out.println (valueslist); }Test
Public Static voidMain (string[] args)throwsInvocationTargetException, illegalaccessexception {List<OrderInfo> Orderinfos =NewArraylist<orderinfo>(); OrderInfo OrderInfo1=NewOrderInfo (); ORDERINFO1.SETORDER_ID (10); ORDERINFO1.SETFOOD_ID (1); Orderinfo1.setprice (18d); Orderinfo1.setnum (1); Orderinfo1.settotal_price (18d); Orderinfo1.setfood_name ("Kung Pao Chicken"); Orderinfo1.setremark ("Less pepper."); OrderInfo OrderInfo2=NewOrderInfo (); ORDERINFO2.SETORDER_ID (10); ORDERINFO2.SETFOOD_ID (1); Orderinfo2.setprice (16d); Orderinfo2.setnum (1); Orderinfo2.settotal_price (16d); Orderinfo2.setfood_name ("Fish fragrant Eggplant"); Orderinfo2.setremark ("Drop the Oil less"); Orderinfos.add (ORDERINFO1); Orderinfos.add (OrderInfo2); InsertObject (Orderinfos); }View CodeTest results:
INSERT into VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) [10, 1, 18.0, 1, 18.0, Kung Pao Chicken, less pepper, 10, 1, 16.0, 1, 16.0, fish fragrant eggplant, less oil ]
Reflection generates SQL statements that insert multiple objects