前面,我们完成了商品列表的展示,本篇文章我们完成商品类目选择的功能。可以看到,新增商品的时候,类目选择弹框是空白的。
还是像之前说的那有,我们侧重于后台,至于前台功能我们不详述。
1、新增接口ItemCatService,代码如下package com.codingwhy.service;
import com.codingwhy.pojo.EasyUITreeNode;
import java.util.List;
public interface ItemCatService {
List getCatList(long parentId);
}
代码路径如下图所示
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 getCatList(long parentId) {
//创建查询条件
TbItemCatExample example = new TbItemCatExample();
TbItemCatExample.Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
//根据条件查询
List list = itemCatMapper.selectByExample(example);
List 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;
}
}
代码路径如下图所示
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 getCatList(@RequestParam(value="id",defaultValue="0")Long parentId) {
List list = itemCatService.getCatList(parentId);
return list;
}
}
代码路径如下图所示
4、最后,重新运行项目,再点“类目选择”按钮,可以看到类目选择里面,数据就出来了。