码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-01-29 20:37:02      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

一、流程分析

技术分享

二、代码

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     }

 

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

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5169555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!