我自己用
GoodsCateVO
package com.zjdfwl.mall.restfulapi.modular.goods.controller.vo; import java.io.Serializable; import java.util.List; /** * 产品类别 展示类 * @author Administrator * */ public class GoodsCateVO implements Serializable { /** * */ private static final long serialVersionUID = 5010898519808673759L; private Integer id; /** * 商品分类编号 */ private String cateSn; /** * 产品类别名字 */ private String name; /** * 产品类别图片 */ private String goodsGallery; /** * 产品类别图标 */ private String logo; /** * 产品类别简介 */ private String goodsCateDesc; /** * 产品类别id */ private Integer pid; /** * 子列表 */ List<GoodsCateVO> child; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCateSn() { return cateSn; } public void setCateSn(String cateSn) { this.cateSn = cateSn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGoodsGallery() { return goodsGallery; } public void setGoodsGallery(String goodsGallery) { this.goodsGallery = goodsGallery; } public String getLogo() { return logo; } public void setLogo(String logo) { this.logo = logo; } public String getGoodsCateDesc() { return goodsCateDesc; } public void setGoodsCateDesc(String goodsCateDesc) { this.goodsCateDesc = goodsCateDesc; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public List<GoodsCateVO> getChild() { return child; } public void setChild(List<GoodsCateVO> child) { this.child = child; } }
controller
@RequestMapping("catelist") public ResultEntity cateList() { try { List<GoodsCateVO> resultList = (List<GoodsCateVO>) JedisUtils.getObject(CacheConsts.CACHE_GOODS_CATE); if (resultList == null || resultList.size() == 0) { Page<GoodsCate> page = new Page<GoodsCate>(1, -1); page.setSearchCount(false); Wrapper<GoodsCate> wrapper = new EntityWrapper<GoodsCate>().eq("is_enable", 1).eq("is_deleted", 0) .orderBy("level,sort_num", true); page = goodsCateService.selectPage(page, wrapper); resultList = new ArrayList<GoodsCateVO>(); if (page != null && page.getRecords().size() > 0) { for (GoodsCate goodsCate : page.getRecords()) { if (goodsCate.getPid() == 0) { GoodsCateVO goodsCateVO = new GoodsCateVO(); BeanUtil.copyProperties(goodsCate, goodsCateVO); List<GoodsCateVO> childList = new ArrayList<GoodsCateVO>(); forChildList(childList, page.getRecords(), goodsCate.getId()); goodsCateVO.setChild(childList); resultList.add(goodsCateVO); } } } if (resultList.size() > 0) { JedisUtils.setObject(CacheConsts.CACHE_GOODS_CATE, resultList, CacheConsts.CACHE_GOODS_CATE_EXPIRE_TIME); } } return success(resultList); } catch (Exception e) { logger.error("goods.catelist() --> ", e); } return error(ResultCode.SYS_ERROR); }
/**
* 分类列表:递归循环子节点
*
* @param childList
* @param dataList
* @param pId
*/
public void forChildList(List<GoodsCateVO> childList, List<GoodsCate> dataList, Integer pId) {
for (GoodsCate goodsCate : dataList) {
if (pId.equals(goodsCate.getPid())) {
GoodsCateVO goodsCateVO = new GoodsCateVO();
BeanUtil.copyProperties(goodsCate, goodsCateVO);
childList.add(goodsCateVO);
}
}
}
mapper.xml
<select SELECT1> SELECT t1.id FROM m_shop_info t1 INNER JOIN m_goods_area_info t2 ON t1.id = t2.shop_id WHERE t1.is_enable = 1 AND t1.is_deleted = 0 AND t2.is_enable = 1 AND t2.is_deleted = 0 AND t2.area = #{area} </select> <select SELECT2> SELECT t3.goods_id FROM m_shop_goods t3 INNER JOIN ( SELECT1 ) t4 ON t3.shop_id = t4.id WHERE t3.is_enable = 1 AND t3.is_deleted = 0 </select> <select SELECT3> SELECT t5.goods_cate_id, count( t5.goods_cate_id ) AS num FROM m_goods_base_info t5 INNER JOIN ( SELECT2 ) t6 ON t5.id = t6.goods_id WHERE t5.is_enable = 1 AND t5.is_deleted = 0 GROUP BY t5.goods_cate_id </select> <select id="selectIndexCateList" resultType="map" parameterType="com.baomidou.mybatisplus.plugins.Page"> SELECT t7.id, t7.name, t7.logo FROM m_goods_cate t7 LEFT JOIN (SELECT3) t8 ON t7.id = t8.goods_cate_id WHERE t7.is_enable = 1 AND t7.is_deleted = 0 ORDER BY t8.num DESC, t7.id ASC </select>