Batch update sql statement with optimal performance

Here's a collection of packages that need to be updated:

/**

* Batch update the data that needs to be updated in the shopping cart

* @Description:

*@date July 28, 2016

*/

public void updateCartDate2(List carUpdateList){

if(carUpdateList != null && carUpdateList.size() > 0){

StringBuffer sql = new StringBuffer("insert into jbe_cart (cart_id,price,tariff,weight,name,itemtype)");

       sql.append("values");

       

       for (int i = 0; i < carUpdateList.size(); i++) {

        CartUpdate carUpdate = (CartUpdate) carUpdateList.get(i);

        sql.append("(");

        sql.append("'"+carUpdate.getCart_id()+"',");

        sql.append("'"+carUpdate.getPrice()+"',");

        sql.append("'"+carUpdate.getTariff()+"',");

        sql.append("'"+carUpdate.getWeight()+"',");

        sql.append("'"+carUpdate.getName()+"',");

        sql.append("'"+carUpdate.getItemtype()+"'");

        sql.append(")");

        if(i< carUpdateList.size() - 1){

        sql.append(",");

        }

}

       

   sql.append("on duplicate key Update price=values(price),tariff=values(tariff),weight=values(weight),name=values(name),itemtype=values(itemtype)");

   this.cartManager.updateCartBySql(sql.toString());

}

}

 

The following is the debug displayed by the final sql statement:

insert into jbe_cart 

(cart_id,price,tariff,weight,name,itemtype)

values('10745','15.0','0.0','1.0','Health food','0'),

('10746','25.0','0.0','1.0','Commodity review verification','0'),

('10747','70.0','0.0','1.0','Mobile','0'),

('10748','850.0','0.0','1.0','Bonded warehouse','0'),

('10749','60.0','0.0','5.0','Test 999999','0')

on duplicate key Update price=values(price),tariff=values(tariff),weight=values(weight),name=values(name),itemtype=values(itemtype)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692945&siteId=291194637