栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

京淘项目——商品分类实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

京淘项目——商品分类实现

  1.数据库item_cat表结构

             

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编辑ItemCatMapper
package 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);
    }

实现效果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322312.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号