SSM + MySQL批量刪除操做

  最近項目中有個購物車功能須要可以選中商品,而後批量刪除的操做,也能夠單個刪除,其實代碼很簡單就能實現。

  這裏須要注意的就是你前端是怎麼傳值的,我這裏採用的數組的形式,用 ‘,’隔開。前端

 

  而後控制層代碼以下:java

 1 public Object DeleteShopping(HttpSession session, @RequestBody Map map) {
 2 
 3         JSONObject json = new JSONObject();
 4 
 5         String id = (String) map.get("ShoppingID");  //首先拿到前端傳遞進來的數組,而後放入字符串對象中
 6 
 7         List<Object> list = new ArrayList<>();
 8 
 9         String[] str = id.split(",");  //這裏以逗號分隔來確認每一個ID是什麼,放入list數組中
10   
11         for (int i = 0; i < str.length; i++) {
12 
13             list.add(str[i]);    //循環將其添加到List集合中
14         }
15 
16         Integer is = applyService.DeleteShopping(list);
17 
18   
19 20 if (is > 0) { 21 22 json.put("msg", "刪除成功!"); 23 json.put("code", "1"); 24 } 25 } else { 26 json.put("code", "0"); 27 json.put("error", "沒法請求!"); 28 } 29 return json; 30 }

  

   咱們使用的MyBatis,因此Mapper.xml中的SQL語句以下:json

 1 <delete id="DeleteShopping" parameterType="java.util.List">
 2         <!-- delete from emp where empno in(7789,7790) -->
 3         <!-- forEach : 用來循環 collection : 用來指定循環的數據的類型 能夠填的值有:array,list,map item
 4             : 循環中爲每一個循環的數據指定一個別名 index : 循環中循環的下標 open : 開始 close : 結束 separator : 數組中元素之間的分隔符 -->
 5         DELETE FROM ShoppingCart WHERE ShoppingID IN
 6         <foreach collection="list" item="arr" open="("
 7                  separator="," close=")">
 8             #{arr}
 9         </foreach>
10     </delete>

 

  至此,批量刪除則已經實現,很簡單的一個列子,就很少講了。數組

相關文章
相關標籤/搜索