父節點
----葉子節點
----子節點一
---------葉子節點
---------葉子節點
----子節點二
---------子節點三
----------------葉子節點
----------------葉子節點
如何得到父節點
采用遞歸算法,碰到葉子節點就加到列表里,不是葉子節點就對其進行循環再遞歸遍歷
- /**
- * 向旗添加
- * 實現將得到的目錄的所有子目錄和目錄本身變成ID的LIST
- * @param 目錄ID
- * @return 目錄與其所有子目錄組成的ID的LIST
- * @throws ServiceException
- */
- public List<Long> getAllPisTCategoryStr(PisTCategory obj)throws ServiceException{
- List<PisTCategory> listAll = new ArrayList<PisTCategory>();
- List<Long> list = new ArrayList<Long>();
- listAll = getAllPisTCategoryByParentId(obj);
- if(listAll.size()!=0){
- for(int i =0;i<listAll.size();i++){
- list.add(listAll.get(i).getId());
- }
- }
- return list;
- }
- /**
- * 向旗添加
- * 實現根據目錄ID得到其所有子目錄list
- * @param 目錄ID
- * @return 所有子目錄list
- * @throws ServiceException
- */
- public List<PisTCategory> getAllPisTCategoryByParentId(PisTCategory obj)throws ServiceException{
- List<PisTCategory> listAll = new ArrayList<PisTCategory>();
- List<PisTCategory> list = new ArrayList<PisTCategory>();
- List<PisTCategory> listtest = new ArrayList<PisTCategory>();
- try{
- if(obj.getLeaf() != null){
- if(obj.getLeaf()==1){
- listAll.add(obj);
- return listAll;
- }}
- obj.setLeaf(null);
- list = getPisTCategoryByParentId_Leaf(obj);
- if(list.size()!=0){
- for(int i=0;i<list.size();i++){
- PisTCategory instance = new PisTCategory();
- instance = list.get(i);
- instance.setParentId(instance.getId());
- listtest = getAllPisTCategoryByParentId(instance);
- listAll.addAll(listtest);
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- throw new ServiceException(this.getClass().getName()+e.getMessage());
- }
- return listAll;
- }
- /**
- * 向旗添加
- * 實現根據父目錄查詢其子目錄及其的所有商品信息
- * @param categoryId
- * @return
- * @throws ServiceException
- */
- public List<PisTProduct> getListPisTProductByCategoryId(Long categoryId)throws ServiceException{
- PisTCategory obj = new PisTCategory();
- obj = pisTCategoryService.getPisTCategoryById(categoryId);
- obj.setParentId(obj.getId());
- List<Long> list = pisTCategoryService.getAllPisTCategoryStr(obj);
- System.out.println(list.size());
- GetProductBySiteCategoryQueryObject qo = new GetProductBySiteCategoryQueryObject();
- qo.setSiteId(null);
- qo.setCategorys(list);
- try {
- return pisTProductDao.getListPisTProductBySiteId_CategoryIDs(qo);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }