• 网上图书商城项目学习笔记-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     }
  • 相关阅读:
    Qt判断文件夹是否存在并新建文件夹
    QFileDialog的使用
    C++11 std::chrono库详解
    disconnected no supported authentication methods available(server sent: publickey)
    connect函数的第5参数Qt::ConnectionType
    在C++ 中检查一个文件是否存在的几种方法
    win10打开便签
    1024. Palindromic Number (25)
    1023. Have Fun with Numbers (20)
    1021. Deepest Root (25)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5181932.html
Copyright © 2020-2023  润新知