• 网上图书商城项目学习笔记-030删除二级分类


    一、流程分析

    二、代码

    1.view层

    和一相同

     

    2.servlet层

    (1)AdminCategoryServlet.java

     1     /**
     2      * 删除二级分类
     3      * @param req
     4      * @param resp
     5      * @return
     6      * @throws ServletException
     7      * @throws IOException
     8      */
     9     public String deleteChild(HttpServletRequest req, HttpServletResponse resp)
    10             throws ServletException, IOException {
    11         String cid = req.getParameter("cid");
    12         int count = bookService.findBookCountByCategory(cid);
    13         if(count > 0) {
    14             req.setAttribute("code", "eror");
    15             req.setAttribute("msg", "该分类下还有图书,不能删除!");
    16             return "/adminjsps/msg.jsp";
    17         }
    18         service.delete(cid);
    19         return findAll(req, resp);
    20     }

    3.service层

    (1)AdminCategory.java

     1     /**
     2      * 删除分类
     3      * @param cid
     4      */
     5     public void delete(String cid) {
     6         try {
     7             categoryDao.delete(cid);
     8         } catch (SQLException e) {
     9             throw new RuntimeException(e);
    10         }
    11     }

    (2)BookService.java 

     1     /**
     2      * 查询某分类下的图书数量
     3      * @param cid
     4      * @return
     5      */
     6     public int findBookCountByCategory(String cid) {
     7         try {
     8             return bookDao.findBookCountByCategory(cid);
     9         } catch (SQLException e) {
    10             throw new RuntimeException(e);
    11         }
    12     }

    4.dao层

    (1)AdminCategoryDao.java

    1     /**
    2      * 删除分类
    3      * @param cid
    4      * @throws SQLException
    5      */
    6     public void delete(String cid) throws SQLException {
    7         String sql = "delete from t_category where cid=?";
    8         qr.update(sql, cid);
    9     }

    (2)BookDao

     1     /**
     2      * 查询某分类下的图书数量
     3      * @param cid
     4      * @return
     5      * @throws SQLException 
     6      */
     7     public int findBookCountByCategory(String cid) throws SQLException {
     8         String sql = "select count(*) from t_book where cid=?";
     9         Number count = (Number) qr.query(sql, new ScalarHandler(), cid);
    10         return count == null ? 0 : count.intValue();
    11     }
  • 相关阅读:
    磁盘原理总结
    Algorithm:多维数组和矩阵
    Algorithm:字典序最小问题
    Algorithm:递归思想及实例分析
    Algorithm:贪心策略之区间覆盖问题
    Algorithm:贪心策略之区间选点问题
    Algorithm:位运算的这些小技巧你知道吗?
    面试题
    操作系统
    数据结构:B树和B+树的插入、删除图文详解
  • 原文地址:https://www.cnblogs.com/shamgod/p/5181823.html
Copyright © 2020-2023  润新知