java商品类_商品类目选择功能开发

本文主要介绍如何在Java中开发商品类目选择功能。通过新增接口ItemCatService、实现类ItemCatServiceImpl以及控制器类ItemCatController,实现了从数据库获取商品类目列表并转换为EasyUITreeNode格式,以便在前端展示。最终,成功展示了商品类目选择的数据。
摘要由CSDN通过智能技术生成

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

bdd98c9721ef5c50358204a0720b2c45.png

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

1、新增接口ItemCatService,代码如下package com.codingwhy.service;

import com.codingwhy.pojo.EasyUITreeNode;

import java.util.List;

public interface ItemCatService {

List getCatList(long parentId);

}

代码路径如下图所示

53b059d9f77fef8a08e8435a34062d25.png

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;

}

}

代码路径如下图所示

7b9d4e6099d28a0a2ce0050541a37f1f.png

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;

}

}

代码路径如下图所示

f26f177c416bac3c6c7fb830524517a6.png

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

7e5b29f9b0e9cff0f65d1091d507308c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值