从数据库获取到列表数据(层级), 把下一层的数据放到上一层的children中
优化 : 查询速度/时间
实现逻辑 : 查询一次得到所有数据, 把数据封装到map集合中, 通过集合直接获取
为什么使用map ?
> 把父级的子级放到children中, 把子级封装到list集合中赋值给父级,
> 父级 子级的联系是父级的id是子级parentId
> parentId对应的是子级的集合, 就是父级id对应它的子级的list集合
> map中key-parentid value-List
Map<parentId,List<ItemCat>>
key value
0级的id 1级集合 (只有一个key)
1级的id 每一个1级id(2级的parentId)对应一个2级集合list 有多个key
2级的id 每一个2级id(3级的parentId)对应一个3级集合list 有多个key
map逻辑 :
key不能重复, value的list增加, 遇到相同的key直接放到list中
不存在: 存储该key, 创建list集合作为value,并把自己放到里面
存在: 获得相同key的value, 把自己追加进去
(这里的存在/不存在指的是 parentId, 相同父级存一个list)
获取级别逻辑 :
1级 :
map.get(0) 就是1级
2级 :
得到1级, 通过1级的id(2级的parentId), 获取2级的集合, 把2级的集合添加到1级的children中
itemCatOne.setChildren(map.get(itemCatOne.getId()));
最终返回的是 1级中children封装2级的list
3级 :
执行2级的,得到1级中children有2级
遍历结果, 得到的是1级, 获取1级的children, 得到的是2级的list
遍历2级list, 根据2级的id获取3级的list, 把3级list放到2级的children中
最终返回的是 1级中children封装2级的list,2级中children封装3级list
service业务:
public Map<Integer,List<ItemCat>> getItemCatListMap(){
// map用于根据parentId封装数据
HashMap<Integer, List<ItemCat>> map = new HashMap<>();
// 获取数据库中的数据 ItemCat的List
List<ItemCat> itemCatsListAll = itemCatDao.selectList(null);
// 遍历list 封装到map中
for (ItemCat itemCat : itemCatsListAll) {
// 得到parentId
Integer parentId = itemCat.getParentId();
if (map.containsKey(parentId)){
// 不是第一次(已经存在) 1.得到相同key的value 2.把自己放到value中的list中
List<ItemCat> listTwo = map.get(parentId);
listTwo.add(itemCat);
}else{
// 第一次 1.封装List 2.把自己放到list中 3.把list放到map中
List<ItemCat> listFirst = new ArrayList<>();
listFirst.add(itemCat);
map.put(parentId,listFirst);
}
}
// 返回map集合
return map;
}
// getItemCatList
@Overried
public List<ItemCat> getItemCatListTwo(Integer level){
long startTime = System.currentTimeMillis();
List<ItemCat> list = new ArrayList<>();
// 封装方法 getItemCatListMap --获取所有数据, 并把数据按parentId封装到map集合中
Map<Integer, List<ItemCat>> itemCatListMap = getItemCatListMap();
// 判断level 要获取哪一级
// level=1, 1级目录 就是parentId为0, map中的key=0
if (level==1){ list = itemCatListMap.get(0); }
// level=2, 在level=3时, 也要使用,
// 所以放在这个方法 getItemCatListTwo 返回封装了2级的1级list
if (level==2){ list = getItemCatListTwo(itemCatListMap); }
// level=3, 执行2, 得到带有2的1, 遍历list, getChildren得到2级, 遍历2级,
// 把3级放到2级children中
if (level==3){
// 获取 children中封装了2级的1级list
List<ItemCat> itemCatListOne = getItemCatListTwo(itemCatListMap);
for (ItemCat itemCatOne : itemCatListOne) {
// 通过1级的children的到 2级list
List<ItemCat> itemCatListTwo = itemCatOne.getChildren();
// bug 问题: 如果没有2级列表,不执行后面的, 结束本轮循环
if (itemCatListTwo==null||itemCatListTwo.size()==0){
continue;
}
for (ItemCat itemCatTwo : itemCatListTwo) {
// 1.根据2级id, 2.获取3级list, 3.把3级list放到2级children中
itemCatTwo.setChildren(itemCatListMap.get(itemCatTwo.getId()));
}
}
list = itemCatListOne;
}
long endTime = System.currentTimeMillis();
System.out.println("Map封装数据,耗时: "+(endTime-startTime)+" 耗时");
return list;
}
public List<ItemCat> getItemCatListTwo(Map<Integer,List<ItemCat>> map){
List<ItemCat> listOne = map.get(0);
for (ItemCat itemCatOne : listOne) {
// 1.根据1级id, 2.获取2级list, 3.把2级list放到1级children中
itemCatOne.setChildren(map.get(itemCatOne.getId()));
}
return listOne;
}
连接数据库方式:
@Override
public List<ItemCat> getItemCatList(Integer level) {
long startTime = System.currentTimeMillis();
QueryWrapper<ItemCat> qw = new QueryWrapper<>();
qw.eq("parent_id",0);
List<ItemCat> itemCats = itemCatDao.selectList(qw);
if (level!=1){
for (ItemCat itemCat1 : itemCats) {
qw.clear();
qw.eq("parent_id",itemCat1.getId());
List<ItemCat> itemCats2 = itemCatDao.selectList(qw);
itemCat1.setChildren(itemCats2);
if (level!=2){
for (ItemCat itemCat2 : itemCats2) {
qw.clear();
qw.eq("parent_id",itemCat2.getId());
List<ItemCat> itemCats3 = itemCatDao.selectList(qw);
itemCat2.setChildren(itemCats3);
}
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("mp : "+(endTime-startTime));
return itemCats;
}
for循环:
for循环会形成 笛卡尔积 速度慢
@Override
public List<ItemCat> findAllLevelOne() {
long startTime = System.currentTimeMillis();
List<ItemCat> itemCats = itemCatDao.selectList(null);
List<ItemCat> oneLevel = new ArrayList<>();
// 一级
for (ItemCat one : itemCats) {
List<ItemCat> oneChildren = new ArrayList<>();
if (one.getLevel()==1){
Integer idOne = one.getId();
// 二级
for (ItemCat two : itemCats) {
List<ItemCat> twoChildren = new ArrayList<>();
if (two.getLevel()==2){
Integer idTwo = two.getId();
Integer parentIdTwo = two.getParentId();
// 二级的添加到一级children
if (idOne == parentIdTwo){
oneChildren.add(two);
}
// 三级
for (ItemCat three : itemCats) {
if (three.getLevel()==3){
Integer parentIdThree = three.getParentId();
// 三级添加到 二级 children
if (idTwo == parentIdThree){
twoChildren.add(three);
}
}
}
two.setChildren(twoChildren);
}
}
one.setChildren(oneChildren);
oneLevel.add(one);
}
}
long endTime = System.currentTimeMillis();
System.out.println("for : "+(endTime-startTime)); // 1958
return oneLevel;
}