网上图书商城项目学习笔记-025分类管理模块分析及查询所有分类实现

一、流程分析

1.分类管理介绍

网上图书商城项目学习笔记-025分类管理模块分析及查询所有分类实现_第1张图片

 

2.查询所有分类

网上图书商城项目学习笔记-025分类管理模块分析及查询所有分类实现_第2张图片

二、代码

1.view层

(1)top.jsp

 1   <body style="background: rgb(78,78,78);color: #fff;">
 2 <h1 style="text-align: center; line-height: 30px;">网上书城系统后台管理</h1>
 3 <div style="line-height: 10px;">
 4     <span>管理员:${sessionScope.admin.adminname }</span>
 5     <a target="_top" href="<c:url value='/adminjsps/login.jsp'/>">退出</a>
 6     <span style="padding-left:50px;">
 7         <a href="<c:url value='/admin/AdminCategoryServlet?method=findAll'/>">分类管理</a>
 8         <a href="<c:url value='/adminjsps/admin/book/main.jsp'/>">图书管理</a>
 9         <a href="<c:url value='/admin/AdminOrderServlet?method=findAll'/>">订单管理</a>
10     </span>
11 </div>
12   </body>

 

(2)list.jsp 

 1   <body>
 2     <h2 style="text-align: center;">分类列表</h2>
 3     <table align="center" border="1" cellpadding="0" cellspacing="0">
 4         <caption class="captionAddOneLevel">
 5           <a href="<c:url value='/adminjsps/admin/category/add.jsp'/>">添加一级分类</a>
 6         </caption>
 7         <tr class="trTitle">
 8             <th>分类名称</th>
 9             <th>描述</th>
10             <th>操作</th>
11         </tr>
12         
13 <c:forEach items="${parents }" var="parent">
14         <tr class="trOneLevel">
15             <td width="200px;">${parent.cname }</td>
16             <td>${parent.desc }</td>
17             <td width="200px;">
18               <a href="<c:url value='/admin/AdminCategoryServlet?method=addChildPre&pid=${parent.cid }'/>">添加二级分类</a>
19               <a href="<c:url value='/admin/AdminCategoryServlet?method=editParentPre&cid=${parent.cid }'/>">修改</a>
20               <a onclick="return confirm('您是否真要删除该一级分类?')" href="<c:url value='/admin/AdminCategoryServlet?method=deleteParent&cid=${parent.cid }'/>">删除</a>
21             </td>
22         </tr>
23     <c:forEach items="${parent.children }" var="child">
24         <tr class="trTwoLevel">
25             <td>${child.cname }</td>
26             <td>J${child.desc }</td>
27             <td width="200px;" align="right">
28               <a href="<c:url value='/admin/AdminCategoryServlet?method=editChildPre&cid=${child.cid }'/>">修改</a>
29               <a onclick="return confirm('您是否真要删除该二级分类?')" href="<c:url value='/admin/AdminCategoryServlet?method=deleteChild&cid=${child.cid }'/>">删除</a>
30             </td>
31         </tr>
32     </c:forEach>
33 </c:forEach>
34 
35     </table>
36   </body>

 

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 findAll(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         List<Category> parents = service.findAll();
12         req.setAttribute("parents", parents);
13         return "/adminjsps/admin/category/list.jsp";
14     }

 

3.service层

(1)CategoryService.java 

 1     /**
 2      * 查询所有分类
 3      * @return
 4      */
 5     public List<Category> findAll() {
 6         try {
 7             return categoryDao.findAll();
 8         } catch (SQLException e) {
 9             throw new RuntimeException(e);
10         }
11     }

 

4.dao层

(1)CategoryDao.java

 1     /**
 2      * 返回所有分类
 3      * @return
 4      * @throws SQLException
 5      */
 6     public List<Category> findAll() throws SQLException {
 7         // 1. 查询出所有一级分类
 8         String sql = "SELECT * FROM t_category WHERE pid IS NULL ORDER BY orderBy";
 9         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
10         
11         List<Category> parents = toCategoryList(mapList);
12         
13         // 2. 循环遍历所有的一级分类,为每个一级分类加载它的二级分类 
14         for(Category parent : parents) {
15             // 查询出当前父分类的所有子分类
16             List<Category> children = findByparent(parent.getCid());
17             // 设置给父分类
18             parent.setChildren(children);
19         }
20         return parents;
21     }
22 
23     /**
24      * 可以把多个Map(List<Map>)映射成多个Category(List<Category>)
25      * @param mapList
26      * @return
27      */
28     private List<Category> toCategoryList(List<Map<String, Object>> mapList) {
29         List<Category> categoryList = new ArrayList<Category>();
30         for(Map<String,Object> map : mapList) {
31             Category c = toCategory(map);
32             categoryList.add(c);
33         }
34         return categoryList;
35     }
36 
37 
38     /**
39      * 把一个Map中的数据映射到Category中
40      * @param map
41      * @return
42      */
43     private Category toCategory(Map<String, Object> map) {
44         /*
45          * map {cid:xx, cname:xx, pid:xx, desc:xx, orderBy:xx}
46          * Category{cid:xx, cname:xx, parent:(cid=pid), desc:xx}
47          */
48         Category category = CommonUtils.toBean(map, Category.class);
49         String pid = (String) map.get("pid");
50         if(pid != null) {
51             Category parent = new Category();
52             parent.setCid(pid);
53             category.setParent(parent);
54         }
55         return category;
56     }
57 
58     /**
59      * 通过父分类查询子分类
60      * @param cid
61      * @return
62      * @throws SQLException
63      */
64     public List<Category> findByparent(String cid) throws SQLException {
65         String  sql = "select * from t_category where pid=?";
66          List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), cid);
67         return toCategoryList(mapList);
68     }

 

你可能感兴趣的:(网上图书商城项目学习笔记-025分类管理模块分析及查询所有分类实现)