网上图书商城项目学习笔记-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     }
原文地址:https://www.cnblogs.com/shamgod/p/5181823.html