编程那点事编程那点事

专注编程入门及提高
探究程序员职业规划之道!

商品类目选择功能开发

前面,我们完成了商品列表的展示,本篇文章我们完成商品类目选择的功能。可以看到,新增商品的时候,类目选择弹框是空白的。

新增商品类目选择功能

还是像之前说的那有,我们侧重于后台,至于前台功能我们不详述。

1、新增接口ItemCatService,代码如下

package com.codingwhy.service;
import com.codingwhy.pojo.EasyUITreeNode;
import java.util.List;
public interface ItemCatService {
    List<EasyUITreeNode> getCatList(long parentId);
}

代码路径如下图所示

新增接口ItemCatService

2、新增接口ItemCatService的实现类ItemCatServiceImpl,具体代码如下

package com.codingwhy.service.impl;
import com.codingwhy.mapper.TbItemCatMapper;
import com.codingwhy.pojo.EasyUITreeNode;
import com.codingwhy.pojo.TbItemCat;
import com.codingwhy.pojo.TbItemCatExample;
import com.codingwhy.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Override
    public List<EasyUITreeNode> getCatList(long parentId) {
        //创建查询条件
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //根据条件查询
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        List<EasyUITreeNode> resultList = new ArrayList<>();
        //把列表转换成treeNodelist
        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;
    }
}

代码路径如下图所示

新增实现类ItemCatServiceImpl

3、新增ItemCatController控制器类,具体代码如下

package com.codingwhy.controller;
import com.codingwhy.pojo.EasyUITreeNode;
import com.codingwhy.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
@RequestMapping("/item/cat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;
    @RequestMapping("/list")
    @ResponseBody
    private List<EasyUITreeNode> getCatList(@RequestParam(value="id",defaultValue="0")Long parentId) {
        List<EasyUITreeNode> list = itemCatService.getCatList(parentId);
        return list;
    }
}

代码路径如下图所示

新增控制器类ItemCatController

4、最后,重新运行项目,再点“类目选择”按钮,可以看到类目选择里面,数据就出来了。

商品类目选择功能开发完成

未经允许不得转载: 技术文章 » Java编程 » 商品类目选择功能开发