网上图书商城项目学习笔记-015删除和批量删除购物车条目

一、流程分析

二、代码

1.view层

(1)list.jsp

 1 <a href="<c:url value='/CartItemServlet?method=batchDelete&cartItemIds=${item.cartItemId }'/>">删除</a>
 2 
 3 <a href="javascript:batchDelete();">批量删除</a>
 4 /*
 5  * 批量删除
 6  */
 7  function batchDelete() {
 8      // 1. 获取所有被选中条目的复选框
 9     // 2. 创建一数组,把所有被选中的复选框的值添加到数组中
10     // 3. 指定location为CartItemServlet,参数method=batchDelete,参数cartItemIds=数组的toString()
11      var ids = new Array();
12      $("input[type=checkbox][name=checkboxBtn]:checked").each(function(index, domEl) {
13          ids.push($(domEl).val());
14      });
15      location = "/goods/CartItemServlet?method=batchDelete&cartItemIds=" + ids;
16  }

 

2.servlet层

(1)CartItemServlet.java

 

 1     /**
 2      * 批量删除(id以逗号隔开,若只有一个id则删除一条记录)
 3      * @param req
 4      * @param resp
 5      * @return
 6      * @throws ServletException
 7      * @throws IOException
 8      */
 9     public String batchDelete(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         /*
12          * 1. 获取cartItemIds参数
13          * 2. 调用service方法完成工作
14          * 3. 返回到list.jsp
15          */
16         String ids = req.getParameter("cartItemIds");
17         service.batchDelete(ids);
18         return myCart(req, resp);
19     }

 

3.service层

(1)CartItemService.java 

 1     /**
 2      * 批量删除
 3      * @param ids
 4      */
 5     public void batchDelete(String ids) {
 6         try {
 7             dao.batchDelete(ids);
 8         } catch (SQLException e) {
 9             throw new RuntimeException(e);
10         }
11     }

 

4.dao层

(1)CartItemDao.java

 1     /**
 2      * 批量删除
 3      * @param ids
 4      * @throws SQLException 
 5      */
 6     public void batchDelete(String ids) throws SQLException {
 7         String [] idsArray = ids.split(","); 
 8         String sql = "delete from t_cartItem where cartItemId in " + toWhereSql(idsArray.length);
 9         //String sql = "delete from t_cartItem where cartItemId in (" + ids + ")";
10         qr.update(sql, idsArray);//视频说要用Object []数组,这里才能对应参数,但我用String[]也可以
11     }

 

转载于:https://www.cnblogs.com/shamgod/p/5169555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值