10 — 网上书城(4)— 删除图书

百般思念 提交于 2020-01-02 07:54:06

1 删除图书

1.1 修改 list.jsp

在这里插入图片描述

1.2 DAO

....

public void deleteBookById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "delete from books where id = ?";
        qr.update(sql,id);
    }

1.3 Service

...

 public void deleteBookById(String id) {
        try {
            bookDao.deleteBookById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

1.4 Servlet

@WebServlet("/DeleteBookByIdServlet")
public class DeleteBookByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");

        BookServiceImpl bookService = new BookServiceImpl();

        bookService.deleteBookById(id);

        req.setAttribute("books",bookService.findAllBooks());
        req.getRequestDispatcher("/admin/products/list.jsp").forward(req, resp);
    }
}

2 删除添加提示

  • list.jsp
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3 批量删除

3.1 list.jsp

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


  • 添加批量删除按钮
function deleteAllBook() {
            var checkboxs = document.getElementsByName('ckBookIds');
            var ids = '';
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked == true) {
                    ids += checkboxs[i].value + ",";
                }
            }

            // 删除最后一个逗号
            ids = ids.substring(0, ids.length - 1);

            // 空字符串就不用删除了
            if(ids == '') return;
            location.href = '${pageContext.request.contextPath}/DeleteAllBookServlet?ids=' + ids;

        }

在这里插入图片描述

3.2 DAO

public void deleteAllBook(String[] idsArr) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "delete from books where id = ?";

        // 方式1
        //for (String id : idsArr) {
//            qr.update(sql,id);
//        }

        Object[][] params = new Object[idsArr.length][];
        for (int i = 0; i < idsArr.length; i++) {
            params[i] = new Object[]{idsArr[i]};
        }
        qr.batch(sql, params);

    }

3.3 Service

public void deleteAllBook(String ids) {

        String[] idsArr = ids.split(",");
        try {
            bookDao.deleteAllBook(idsArr);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

3.4 Servlet

@WebServlet("/DeleteAllBookServlet")
public class DeleteAllBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");

        System.out.println(ids);

        BookServiceImpl bookService = new BookServiceImpl();
        bookService.deleteAllBook(ids);


        // 回到展示页面
        req.setAttribute("books",bookService.findAllBooks());
        req.getRequestDispatcher("/admin/products/list.jsp").forward(req, resp);
    }
}

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!