图上的这种分类一般可以无限递归下去,一般分类用三张表来做,现在用一张表试试:
1.商品分类表:
1.1 分类类型 type: 1:一级大分类 ; 2:二级分类 ; 3:三级小分类 【可以一直分类下去,我这里需求不复杂,所以只分了三级】。
1.2 父id :每一个类别都有上级分类【一级分类没有】:
看下图:id 为11 的蛋糕 是二级分类,他的上级分类 是 id 为 1 的甜点/蛋糕
1.3 图标:是个logo图片:能清楚地让客户知道 该 分类是什么类型的,提高用户体验感
1.4 口号:不要也行
1.5: 分类图: 这个就是分类图
看一个京东的:
==============================================================================================
2. 对于商品分类,我们不是一次性就将所有数据查询出来(服务器压力过大)
首页分类展示需求:
1.第一次刷新主页查询大分类,渲染到首页
2.如果鼠标上移到大分类,则加载其子分类的内容,如果已经存在子分类,则不需要加载(懒加载)
3.所以先查出所有的一级分类:
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<Category> queryAllRootLevelCat() {
Example example=new Example(Category.class);
Example.Criteria criteria=example.createCriteria();
criteria.andEqualTo("type",1);
List<Category> result=categoryMapper.selectByExample(example);
return result;
}
4.然后鼠标点到哪里,就查那个一级分类对应的子类(前端的工作,后端负责查出):
注意:这是将某个一级分类下的二级和三级分类都查了出来。
<select id="getSubCatList" parameterType="int" resultMap="myCategoryVO">
select
f.id as id,
f.name as name ,
f.type as type ,
f.father_id as fatherId,
c.id as subId,
c.name as subName,
c.father_id as subFatherId
from
category f
left join
category c
on
f.id=c.father_id
where
f.father_id=#{rootCatId}
</select>
这是当 rootCatId 等于1的时候,查出的结果: 二级分类都重复了,所以在 二级分类下设置一个List<三级分类>
5.
/**
* 二级分类VO
*/
public class CategoryVO {
private Integer id;
private String name;
private String type;
private Integer fatherId;
// 三级分类vo list
private List<SubCategoryVO> subCatList;
}
===========================================================
子分类:
public class SubCategoryVO {
private Integer subId;
private String subName;
private String subType;
private Integer subFatherId;
}
=================================================
这样就实现了懒加载