zoukankan      html  css  js  c++  java
  • 网上图书商城项目学习笔记-033删除图书

    一、流程分析

    二、代码

    1.view层

    (1).jsp

    (2).js

     

    2.servlet层

    (1)AdminBookServlet.java

     

     1     /**
     2      * 删除图书
     3      * @param req
     4      * @param resp
     5      * @return
     6      * @throws ServletException
     7      * @throws IOException
     8      */
     9     public String delete(HttpServletRequest req, HttpServletResponse resp)
    10             throws ServletException, IOException {
    11         String bid = req.getParameter("bid");
    12         /*
    13          * 删除图片
    14          */
    15         Book book = bookService.load(bid);
    16         String savepath = this.getServletContext().getRealPath("/");
    17         new File(savepath, book.getImage_b()).delete();
    18         new File(savepath, book.getImage_w()).delete();
    19         
    20         bookService.delete(bid);
    21         req.setAttribute("msg", "删除图书成功!");
    22         return "f:/adminjsps/msg.jsp";
    23     }

    3.service层

    (1).java

     

    4.dao层

    (1)BookDao.java

     1     /**
     2      * 删除图书
     3      * @param bid
     4      * @throws SQLException
     5      */
     6     public void delete(String bid) throws SQLException {
     7         String sql = "delete from t_book where bid=?";
     8         qr.update(sql, bid);
     9     }
    10 
    11     /**
    12      * 按bid查询
    13      * @param bid
    14      * @return
    15      * @throws SQLException
    16      */
    17     public Book findById(String bid) throws SQLException {
    18         String sql = "SELECT * FROM t_book b, t_category c WHERE b.cid=c.cid AND b.bid=?";
    19         // 一行记录中,包含了很多的book的属性,还有一个cid属性
    20         Map<String,Object> map = qr.query(sql, new MapHandler(), bid);
    21         // 把Map中除了cid以外的其他属性映射到Book对象中
    22         Book book = CommonUtils.toBean(map, Book.class);
    23         // 把Map中cid属性映射到Category中,即这个Category只有cid
    24         Category category = CommonUtils.toBean(map, Category.class);
    25         // 两者建立关系
    26         book.setCategory(category);
    27         
    28         // 把pid获取出来,创建一个Category parnet,把pid赋给它,然后再把parent赋给category
    29         if(map.get("pid") != null) {
    30             Category parent = new Category();
    31             parent.setCid((String)map.get("pid"));
    32             category.setParent(parent);
    33         }
    34         return book;
    35     }
  • 相关阅读:
    记一次网站迁移的过程
    如何才能搜索微信群和网盘群
    2021最新车载u盘歌曲集合,每轴更新。想要拉你入群
    工具分享:目录生成器
    福利,剪映PC版来了~ 支持windos系统和苹果系统
    微信8.0来了,可以加1w人好友,微商必看!!!
    centOS7安装 redis server
    Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)')
    《More Effective C++》读书笔记(下)
    《More Effective C++》读书笔记(中)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5181932.html
Copyright © 2011-2022 走看看