流程:
package com.jt.service;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
/*注释service层*/
@Service
public class ItemCatServiceImpl implements ItemCatService{
@Autowired
private ItemCatMapper itemCatMapper;
/**
* 业务:Map<parentId,List<ItemCat>>
* 思路:
* 判断Map中是否有key
* true: 获取list集合,将自己追加到集合中
* false: new List集合,将自己作为第一个元素保存,之后存储map
*/
/**本方法用于根据ParentId作为key 使用Map存储数据表数据,
* 一把获取数据库所有数据,然后在service层拼接*/
public Map<Integer,List<ItemCat>> getMap(){
/*1.创建一个Map类型对象*/
Map<Integer,List<ItemCat>> map = new HashMap<>();
/*2.查询所有数据表中信息存到集合中*/
List<ItemCat> list = itemCatMapper.findItemCatList();
/*3.遍历所有数据*/
for (ItemCat itemCat : list){
/*4.从数据表中数据获取parentId作为Map的Key值*/
int key = itemCat.getParentId();
/*5.存在parentId作为key值的,则*/
if(map.containsKey(key)){
/*6.则通过key获取map中的value,value是一个List集合,可以用.add存很多数据*/
map.get(key).add(itemCat);
}else{
/*7.不存在则创建一个ArrayList集合,*/
List<ItemCat> tempList = new ArrayList<>();
/*8.集合存入数据表一段数据*/
tempList.add(itemCat);
/*9.然后把key和存好一段数据的集合存到作为value存到map集合进行下次遍历*/
map.put(key,tempList);
}
}
/*10.这个方法返回一个整理好根据parentId分类好的map集合*/
return map;
}
/**
* 思路:
* 1.获取三级商品分类信息
* 2.先应该获取2级商品分类信息
* 口诀: 判断为null 使用||,
* 判断非null 使用&&
* @param map
* @return
*/
/*24.接收整理好根据parentId分类所有数据的map集合*/
private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
/*25.先查询一级和二级的数据,获取的是存好二级数据的一级数据的集合*/
List<ItemCat> oneList = getTwoList(map);
/*26.遍历一级数据集合,获取二级数据集合独享*/
for (ItemCat oneItemCat : oneList){
/*27.获取单个二级菜单对象, 二级数据可能为null*/
List<ItemCat> twoList = oneItemCat.getChildren();
/*28.判断二级数据是否为空*/
if(twoList == null || twoList.size()==0)
/*如果为空则直接跳过执行下一次循环执行下一次循环,不获取这个二级菜单数据*/
continue;
/*29.如果二级数据不为空则遍历出这个二级数据*/
for (ItemCat twoItemCat : twoList){
/*30.获取这个二级菜单的id*/
int twoId = twoItemCat.getId();
/*31.根据二级菜单Id,查询对应的三级数据存到集合*/
List<ItemCat> threeList = map.get(twoId);
/*32.将一个个三级菜单存到二级菜单中*/
twoItemCat.setChildren(threeList);
}
}
/*33.返回存好二级菜单和三级菜单的一级菜单*/
return oneList;
}
/*16.传入整理好根据parentId分类好的map集合为参数*/
private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
/*17.查询一级数据*/
List<ItemCat> oneList = map.get(0);
/*18.遍历一级数据*/
for (ItemCat oneItemCat : oneList){
/*19.获取一级菜单id*/
int id = oneItemCat.getId();
/*20.根据1级菜单Id,查询对应的二级数据存到集合*/
List<ItemCat> twoList = map.get(id);
/*21.将集合对象一个个存到一级菜单中*/
oneItemCat.setChildren(twoList);
}
/*22.返回存好的一级菜单*/
return oneList;
}
/**完成业务方式:使用for循环,第一次读取数据库地拼接一级数据和二级数据,二级数据和三级数据
* 本方法理解简单,但需要后端和数据库来回读取拼接,效率低*/
/*@Override
public List<ItemCat> findItemCatList(Integer level) {
long startTime = System.currentTimeMillis();
//1.查询一级商品分类信息
int parentId = 0;
List<ItemCat> oneList = itemCatMapper.findItemCatByParentId(parentId);
//2.查询二级商品分类信息
for (ItemCat oneItemCat : oneList){//100条
//2.1 根据一级的Id 查询二级的数据
int oneId = oneItemCat.getId();
List<ItemCat> twoList = itemCatMapper.findItemCatByParentId(oneId);
//3.查询三级商品分类信息
for (ItemCat twoItemCat : twoList){//100条
//3.1 根据二级Id,查询三级数据
int twoId = twoItemCat.getId();
List<ItemCat> threeList = itemCatMapper.findItemCatByParentId(twoId);
twoItemCat.setChildren(threeList);
}
oneItemCat.setChildren(twoList);
}
long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime - startTime)+"毫秒!!");
return oneList;
}*/
}
删除三级分类信息的一级分类信息
说明:删除一级信息时,需要删除一级信息对应的二级信息,二级信息对应的三级信息
<delete id="deleteItemCat1">
<!--执行删除sql-->
delete from item_cat
where
<!--通过一级id作为父级id,找到二级菜单所有id作为三级菜单的父级id作为查找条件找到到所有的三级菜单-->
parent_id in
(select id from item_cat where parent_id=#{id})
or
<!--通过一级id作为父级id,找到 二级菜单所有数据-->
parent_id=#{id}
or
<!--通过传进来的id找到一级菜单所有数据-->
id=#{id}
</delete>