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

一、流程分析

二、代码

1.view层

(1)msg.jsp

1   <body>
2 <h2>${msg }</h2>
3 <input type="button" value="返回" onclick="history.go(-1)"/>
4   </body>
5 </html>

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 deleteParent(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         String cid = req.getParameter("cid");
12         int count = service.findChildCountByParent(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)AdminCategoryService.java 

 1     /**
 2      * 查询子类个数
 3      * @param cid
 4      * @return
 5      */
 6     public int findChildCountByParent(String cid) {
 7         try {
 8             return categoryDao.findChildCountByparent(cid);
 9         } catch (SQLException e) {
10             throw new RuntimeException(e);
11         }
12     }
13     
14     /**
15      * 删除分类
16      * @param cid
17      */
18     public void delete(String cid) {
19         try {
20             categoryDao.delete(cid);
21         } catch (SQLException e) {
22             throw new RuntimeException(e);
23         }
24     }

4.dao层

(1)AdminCategoryDao.java

 1     /**
 2      * 查询子类个数
 3      * @param cid
 4      * @return
 5      * @throws SQLException
 6      */
 7     public int findChildCountByparent(String cid) throws SQLException {
 8         String sql = "select count(1) from t_category where pid=?";
 9         Number count = (Number) qr.query(sql, new ScalarHandler(), cid);
10         return count == null ? 0 : count.intValue();
11     }
12     
13     /**
14      * 删除分类
15      * @param cid
16      * @throws SQLException
17      */
18     public void delete(String cid) throws SQLException {
19         String sql = "delete from t_category where cid=?";
20         qr.update(sql, cid);
21     }
原文地址:https://www.cnblogs.com/shamgod/p/5181579.html