美文网首页
taotao-添加商品-类目选择

taotao-添加商品-类目选择

作者: 小小蒜头 | 来源:发表于2018-07-26 19:32 被阅读105次

商品类目使用的表:tb_item_cat

选择类目 选择类目小窗口

请求的url:/item/cat/list
请求的参数:id(当前节点的id)
响应的结果:json数据

[{    
    "id": 1,    
    "text": "Node 1",    
    "state": "closed"
 }
{    
    "id": 2,    
    "text": "Node 2",    
    "state": "closed"
 }
]

如果当前节点为父节点,state应为“closed”、如果是叶子节点“open”

数据表

1. DAO层

查询tb_item_cat表,根据id查询商品分类列表。可以使用逆向工程。

2. Service层

接收参数parentId,根据parentId查询分类列表。返回pojo列表。
Pojo应该包含三个属性:id、text、state
应该放到taotao-common工程中

EasyUITreeNode

package com.taotao.common.pojo;

/**
 * Created by yvettee on 2018/7/27.
 */
public class EasyUITreeNode {
    private long id;
    private String text;
    private String state;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

ItemCatService

package com.taotao.service;

import com.taotao.common.pojo.EasyUITreeNode;

import java.util.List;

/**
 * Created by yvettee on 2018/7/27.
 */
public interface ItemCatService {
    List<EasyUITreeNode> getItemCatList(long parentId);
}

ItemCatServiceImpl

package com.taotao.service.impl;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类管理
 * Created by yvettee on 2018/7/27.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {
        //根据parentId查询分类列表
        TbItemCatExample example = new TbItemCatExample();

        //设置查询条件
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(parentId);

        List<TbItemCat> list = itemCatMapper.selectByExample(example);

        //转换成EasyUITreeNode列表
        List<EasyUITreeNode> resultList = new ArrayList<>();
        for (TbItemCat tbItemCat : list) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
            node.setState(tbItemCat.getIsParent() ? "closed" : "open");
            resultList.add(node);
        }

        return resultList;
    }
}

2. Controller层

接收参数,parentId。调用Service查询分类类别,返回列表(json数据),需要使用@ResponseBody。

请求的url:/item/cat/list

ItemCatController

package com.taotao.controller;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品分类管理Controller
 * Created by yvettee on 2018/7/27.
 */
@Controller
@RequestMapping("/item/cat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping("/list")
    @ResponseBody
    public List<EasyUITreeNode> getItemCatList(@RequestParam(value = "id",defaultValue = "0") Long parentId) {
        List<EasyUITreeNode> list = itemCatService.getItemCatList(parentId);

        return list;
    }
}

结果展示

上篇:taotao-Mybatis分页插件(商品列表展示)
下篇:taotao-图片服务器
源代码:https://github.com/yvettee36/taotao

相关文章

网友评论

      本文标题:taotao-添加商品-类目选择

      本文链接:https://www.haomeiwen.com/subject/oxqimftx.html