2.SpringBoot整合MybatisPlus 2.1导入MPjar包 2.2编辑pojo
package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
@Data
@Accessors(chain = true)
@TableName("item_cat")
public class ItemCat extends basePojo{
@TableId(type = IdType.AUTO)
private Integer id; //定义主键
private Integer parentId; //定义父级菜单
private String name; //分类名称
private Boolean status; //分类状态 0 停用 1 正常
private Integer level; //商品分类等级 1 2 3
@TableField(exist = false)
private List children;//业务属性
}
注意children属性不存在 使用@TableField(exist = false)标注该属性在数据库内并不存在
2.3编辑ItemCatMapperpackage com.jt.mapper; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.jt.pojo.ItemCat; import java.util.List; public interface ItemCatMapper extends baseMapper{ //CURD操作如果没有特殊需求可以省略 }
若没有写sql的需求,mapper映射文件可以省略
2.4修改yml文件 2.5 编辑Service层、Controller 3.实现商品分类页面跳转 3.1编辑路由 3.2业务接口文档业务接口文档
3.3编辑Controller层package com.jt.controller;
import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import com.jt.service.ItemCatService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.ParameterizedType;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/itemCat")
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;
@GetMapping("/findItemCatList/{level}")
public SysResult findItemCatByLevel(@PathVariable Integer level){
List list=itemCatService.findItemCatByLevel(level);
return SysResult.success(list);
}
}
3.4编辑Service层
package com.jt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ItemCatServiceImpl implements ItemCatService{
@Autowired
private ItemCatMapper itemCatMapper;
public Map> getMap(){
Map> map = new HashMap<>();
//查询所有的数据库记录
List list = itemCatMapper.selectList(null);
//1.遍历数据
for(ItemCat itemCat:list){
int parentId = itemCat.getParentId();
if(map.containsKey(parentId)){
//表示数据存在,将自己追加
map.get(parentId).add(itemCat);
}else{
//key不存在, 定义list集合,将自己作为第一个元素追加
List childrenList = new ArrayList<>();
childrenList.add(itemCat);
//将数据保存到map集合中
map.put(parentId,childrenList);
}
}
return map;
}
public List getTwoList(Map> map){
List oneList=map.get(0);
for(ItemCat oneItemCat:oneList){
Integer parentId=oneItemCat.getId();
List twoList=map.get(parentId);
oneItemCat.setChildren(twoList);
}
return oneList;
}
@Override
public List findItemCatByLevel(Integer level) {
//获取所有集合数据
Map> map = getMap();
if(level==1){
return map.get(0);
}
if(level ==3){
return getTwoList(map);
}
return null;
}
}
页面效果
实现三级分类页面
实现代码:
package com.jt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.annotation.ElementType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ItemCatServiceImpl implements ItemCatService{
@Autowired
private ItemCatMapper itemCatMapper;
public Map> getMap(){
Map> map = new HashMap<>();
//查询所有的数据库记录
List list = itemCatMapper.selectList(null);
//1.遍历数据
for(ItemCat itemCat:list){
int parentId = itemCat.getParentId();
if(map.containsKey(parentId)){
//表示数据存在,将自己追加
map.get(parentId).add(itemCat);
}else{
//key不存在, 定义list集合,将自己作为第一个元素追加
List childrenList = new ArrayList<>();
childrenList.add(itemCat);
//将数据保存到map集合中
map.put(parentId,childrenList);
}
}
return map;
}
public List getTwoList(Map> map){
List oneList=map.get(0);
for(ItemCat oneItemCat:oneList){
Integer parentId=oneItemCat.getId();
List twoList=map.get(parentId);
oneItemCat.setChildren(twoList);
}
return oneList;
}
public List getThreeList(Map> map){
List oneList=getTwoList(map);
for(ItemCat oneItemCat:oneList){
List twoList=oneItemCat.getChildren();
//判断twoList是否为空
if(twoList==null||twoList.size()==0){
continue;//跳过本轮循环进入下一轮循环
}
for(ItemCat twoItemCat:twoList){
Integer parentId=twoItemCat.getId();
ListthreeList=map.get(parentId);
twoItemCat.setChildren(threeList);
}
}
return oneList;
}
@Override
public List findItemCatByLevel(Integer level) {
//获取所有集合数据
Map> map = getMap();
if(level==1){
return map.get(0);
}
if(level ==2){
return getTwoList(map);
}
return getThreeList(map);
}
}
判断是否为空位置不可颠倒 !!!否则是空的话报空指针异常
4.商品分类新增 4.1业务接口业务接口文档
4.2编辑Controller层
@PostMapping("/saveItemCat")
public SysResult saveItemCat(@RequestBody ItemCat itemCat){
itemCatService.saveItemCat(itemCat);
return SysResult.success();
}
4.3编辑Service层
@Override
@Transactional
public void saveItemCat(ItemCat itemCat) {
Date date=new Date();
itemCat.setStatus(true).setCreated(date).setUpdated(date);
itemCatMapper.insert(itemCat);
}
实现效果
5.删除商品分类 5.1编辑Controller层
@DeleteMapping("/deleteItemCat")
public SysResult deleteItemCat(ItemCat itemCat){
itemCatService.deleteItemCat(itemCat);
return SysResult.success();
}
5.2编辑Service层
@Override
@Transactional
public void deleteItemCat(ItemCat itemCat) {
Integer level=itemCat.getLevel();
if(level==3){
itemCatMapper.deleteById(itemCat.getId());
return;
}
if(level==2){
//先删除三级
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("parent_id", itemCat.getId());
itemCatMapper.delete(queryWrapper);
//再删除二级(自己)
itemCatMapper.deleteById(itemCat.getId());
return;
}
//删除一级数据
//先查询二级 使用sql parent_id=id;
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("parent_id", itemCat.getId());
List idList=itemCatMapper.selectObjs(queryWrapper);
if(idList.size()>0){
queryWrapper.clear();
queryWrapper.in("parent_id", idList);
itemCatMapper.delete(queryWrapper);
idList.add(itemCat.getId());
itemCatMapper.deleteBatchIds(idList);
}else{
itemCatMapper.deleteById(itemCat.getId());
}
}
6.修改商品分类状态
6.1编辑Controller层
@PutMapping("/status/{id}/{status}")
public SysResult updateStatus( ItemCat itemCat){
itemCatService.updateStatus(itemCat);
return SysResult.success();
}
6.2编辑Service层
@Override
@Transactional
public void updateStatus(ItemCat itemCat) {
Date date=new Date();
itemCat.setUpdated(date);
itemCatMapper.updateById(itemCat);
}
实现效果



