下面是封装需要更新的集合:
/**
* 批量更新购物车需要更新的数据
* @Description:
* @date 2016年7月28日
*/
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());
}
}
下面是最终sql语句显示的debug:
insert into jbe_cart
(cart_id,price,tariff,weight,name,itemtype)
values('10745','15.0','0.0','1.0','保健食品','0'),
('10746','25.0','0.0','1.0','商品审核验证','0'),
('10747','70.0','0.0','1.0','手机','0'),
('10748','850.0','0.0','1.0','保税仓','0'),
('10749','60.0','0.0','5.0','测试999999','0')
on duplicate key Update price=values(price),tariff=values(tariff),weight=values(weight),name=values(name),itemtype=values(itemtype)