京淘(三阶段)

本文详细介绍了如何实现商品分类模块,包括前后端交互、Mapper接口的SQL编写、事务控制和数据优化策略。通过ItemCatServiceImpl服务层,ItemCatController控制层以及ItemCatMapper接口的代码实现,展示了分类数据的查询与保存过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

商品分类模块实现

2.1完成页面的跳转
前端页面路由定义Home.vue为父 ItemCat.vue为子

 添加新的操作层实现层,mapper层

根据接口文档实现分类数据显示

 

 

优化一二三级数据分类(优化策略)

 

 

 一级

 二级查询

 三级查询

全整体代码块

实现类(ItemCatServiceImpl)

package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.Item;
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
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper ;

    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        //1.查询所有的数据库信息
        List<ItemCat> list= itemCatMapper.findItemCatList();
        //2.遍历数据封装Map
        for (ItemCat itemCat:list){
           int key = itemCat.getParentId();
           if (map.containsKey(key)){
                map.get(key).add(itemCat);
           }else {//key不存在,第一个数据
               List<ItemCat> tempList = new ArrayList<>();
               tempList.add(itemCat);
               map.put(key,tempList);
           }
        }
        return map;
    }



    @Override
    public List<ItemCat> findItemCatList(Integer level) {
    Map<Integer,List<ItemCat>> map = getMap();
    if (level ==1){
        return map.get(0);
    }
    if (level==2)
        return getTwoList(map);
    //level=2 查询一级和二级数据


    List<ItemCat> threeList = getThreeList(map);
    return threeList;
       /* return null;*/
    }

    @Override
    @Transactional //事务控制
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true)
                .setUpdated(date)
                .setCreated(date);
        itemCatMapper.saveItemCat(itemCat);
    }

    private List<ItemCat> getTwoList (Map<Integer,List<ItemCat>> map){
        //1.查询一级数据
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneitemCat : oneList){
            //1.获取一级Id
            int id =oneitemCat.getId();
            //根据1级Id,查询二级数据
            List<ItemCat> twoList = map.get(id);
            oneitemCat.setChildren(twoList);
        }
        return  oneList;
}
private List<ItemCat> getThreeList(Map<Integer,List<ItemCat>> map){
        //1.先查询一级和二级的数据
        List<ItemCat> oneList=getTwoList(map);
        //2.遍历一级集合,获取二级数据
        for (ItemCat oneItemCat : oneList){
                //2.1获取二级数据信息,二级数据可能为null
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size()==0)
                continue;
            for (ItemCat twoItemCat : twoList){
                int twoId = twoItemCat.getId();
                //2.2获取三级集合信息
                List<ItemCat> threeList = map.get(twoId);
                //2.3将三级数据封装到二级对象中
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
}
}
   /* //1.查询一级商品分类信息
    int parentId = 0;
    List<ItemCat> oneList = itemCatMapper.findItemCatByParentId(parentId);
//2.查询二级商品分类信息
        for (ItemCat oneItemCat : oneList){
                //2.1 根据一级的Id 查询二级的数据
                int oneId = oneItemCat.getId();
                List<ItemCat> twoList = itemCatMapper.findItemCatByParentId(oneId);
        oneItemCat.setChildren(twoList);
        for (ItemCat twoItemCat : twoList){
        int twoId = twoItemCat.getId();
        List<ItemCat> threeList = itemCatMapper.findItemCatByParentId(twoId);
        twoItemCat.setChildren(threeList);
        }
        oneItemCat.setChildren(twoList);
        }*/

操控层(ItemCatController)

package com.jt.controller;

import com.jt.pojo.ItemCat;
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.util.List;

@RestController
@CrossOrigin
@RequestMapping("/itemCat")
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    /**
     * url:  /itemCat/findItemCatList/{level}
     * 参数:  {level}
     * 返回值: SysResult对象
     */
    @GetMapping("/findItemCatList/{level}")
    public SysResult findItemCatList(@PathVariable Integer level){//1-2-3

        List<ItemCat> list = itemCatService.findItemCatList(level);
        return SysResult.success(list);
    }

    /**
     * 业务需求:实现商品分类入库操作
     * URL地址:/itemCat/saveItemCat
     * 参数:表单数据
     * 返回值:SYSResult对象
     * */
    @PostMapping("/saveItemCat")
    public SysResult saveItemCat(@RequestBody ItemCat itemCat){
        itemCatService.saveItemCat(itemCat);
        return SysResult.success();

    }
}

ItemCatMapper层(用注解编写Sql语句执行的地方)

package com.jt.mapper;

import com.jt.pojo.ItemCat;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ItemCatMapper {

    @Select("select * from item_cat where parent_id=#{parentId}")
    List<ItemCat> findItemCatByParentId(int parentId);

    @Select("select * from item_cat")
    List<ItemCat> findItemCatList();

    void saveItemCat(ItemCat itemCat);
}

映射文件(ItemCatMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.jt.mapper.ItemCatMapper">
    <insert id="saveItemCat">
       insert into item_cat value (null,#{parentId},#{name},#{status},#{level},#{created},#{updated})
   </insert>
</mapper>

报错集

报错信息的配置,有些报错信息是存在报错但是可以运行的现象

报错的信息可以根据所需进行更改,操作需谨慎

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值