mysql树状查询

it2022-05-05  155

1.如图所示,该树状的父节点和子节点在数据库中用同一个字段表示

String id = request.getParameter("id"); List<GoodsSpec> l = new ArrayList<GoodsSpec>(); //循环商品拿到gid并且type为0 拿到规格父节点 List<GoodsSpec> list =GoodsSpecDao.findBygid(Long.valueOf(id)); for(GoodsSpec c:list) { l.add(c); //根据GoodsSpec的id拿到子节点 List<GoodsSpec> list2 = GoodsSpecDao.findBytid(c.getId()); System.out.print(c.getId()); for(GoodsSpec cc:list2) { l.add(cc); } } request.setAttribute("list", l); request.getRequestDispatcher("/WEB-INF/jsp/sys/goods_standard.jsp").forward(request, response); private static final String SELECT_ALL = "select t1.id,t1.siteId,t1.type,t1.title,t1.logo from wxcms_goods_spec t1"; //拿到父节点 public static List<GoodsSpec> findBygid(long gid){ StringBuilder str=new StringBuilder(SELECT_ALL); str.append(" where gid=? and type=0"); List<GoodsSpec> list=new ArrayList<GoodsSpec>(); list=queryForList(str.toString(),new Object[] {gid},GoodsSpec.class); return list; } //拿到子节点 public static List<GoodsSpec> findBytid(long type){ StringBuilder str=new StringBuilder(SELECT_ALL); str.append(" where type=?"); List<GoodsSpec> list=new ArrayList<GoodsSpec>(); list=queryForList(str.toString(),new Object[] {type},GoodsSpec.class); return list; } <select name=""> <option value="">全部</option> <c:forEach items="${list }" var="item"> <option value="${item.id}"><c:if test="${item.type!=0}">  </c:if>${item.title }</option> </c:forEach> </select>

最新回复(0)