1、添加索引(一般是唯一索引,我的是联合唯一索引):
alter table T_Cart add unique index(goods_id,user_id);
2、SQL
/* * 保存购物车(如果不存在则插入,如果存在则更新) */ @Override public void insetCart(Cart cart) throws Exception { String sql = "INSERT INTO T_Cart(goods_id,order_num,user_id) VALUES(?,?,?) ON DUPLICATE KEY UPDATE order_num=order_num+?"; util.update(sql, cart.getGoods_id(),cart.getOrder_num(), cart.getUser_id(),cart.getOrder_num()); }