小编最近正在做一个商城的项目,下面让小编给大家分享一下商品类目选择的实现过程。
商品类目选择的原型:
功能分析:
展示商品分类列表,使用EasyUI的tree控件展示。
**异步树控件:**树控件内建异步加载模式的支持,用户先创建一个空的树,然后执行一个服务器端,执行检索后动态返回Json数据来填充树并完成异步请求。
初始化tree请求的url:/item/cat/list
参数:
初始化tree时只需要把第一级节点展示,子节点异步加载。
long id(父节点id)
返回值:json。数据格式
[{
“id”: 1,
“text”: “Node 1”,
“state”: “closed”
},{
“id”: 2,
“text”: “Node 2”,
“state”: “closed”
}]
state:如果节点下有子节点“closed”,如果没有子节点“open”
创建一个pojo来描述tree的节点信息,包含三个属性id、text、state。放到e3-common工程中。
在package cn.e3mall.common.pojo中创建 EasyUITreeNode类。
package cn.e3mall.common.pojo;
import java.io.Serializable;
public class EasyUITreeNode implements Serializable{
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;
}
}
查询表:tb_item_cat
查询列:Id、name、isparent 查询条件parentId
**Dao层:**tb_item_cat可以使用****生成的代码。
Service层:
参数:long parentId
业务逻辑:
1、根据parentId查询节点列表
2、转换成EasyUITreeNode列表。
3、返回。返回值:List
在Service层中的包package cn.e3mall.service.impl中创建ItemCatServiceImpl类,如图所示:
package cn.e3mall.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.mapper.TbItemCatMapper;
import cn.e3mall.pojo.TbItemCat;
import cn.e3mall.pojo.TbItemCatExample;
import cn.e3mall.pojo.TbItemCatExample.Criteria;
import cn.e3mall.service.ItemCatService;
/**
* 商品分类管理
* <p>Title: ItemCatServiceImpl</p>
* <p>Description: </p>
* <p>Company: www.itcast.cn</p>
* @version 1.0
*/
@Service
public class ItemCatServiceImpl implements ItemCatService {
@Autowired
private TbItemCatMapper itemCatMapper;
@Override
public List<EasyUITreeNode> getItemCatlist(long parentId) {
//根据parentId查询子节点列表
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
//设置查询条件
criteria.andParentIdEqualTo(parentId);
//执行查询
List<TbItemCat> list = itemCatMapper.selectByExample(example);
//创建返回结果List
List<EasyUITreeNode> resultList = new ArrayList<>();
//把列表转换成EasyUITreeNode列表
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;
}
}
发布服务:
在Service层中的spring文件夹中的applicationContext-service.xml中,声明服务:<dubbo:service interface=“cn.e3mall.service.ItemCatService” ref=“itemCatServiceImpl” timeout=“600000”/>
在显示层web中的springmvc中引用dubbo服务。<dubbo:reference interface=“cn.e3mall.service.ItemCatService” id=“itemCatService” />
在表现层manager-web中的package cn.e3mall.controller包中创建ItemCatController类。
先初始化tree请求url:/item/cat/list
参数: long id(父节点id)
返回值:json.
代码展示:
package cn.e3mall.controller;
import java.util.List;
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 cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.service.ItemCatService;
/**
* 商品分类管理controller
* <p>Title: ItemCatController</p>
* <p>Description: </p>
* <p>Company: www.itcast.cn</p>
* @version 1.0
*/
@Controller
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;
@RequestMapping("/item/cat/list")
@ResponseBody
public List<EasyUITreeNode> getItemCatList(
@RequestParam(name="id", defaultValue="0")Long parentId) {
//调用服务查询节点列表
List<EasyUITreeNode> list = itemCatService.getItemCatlist(parentId);
return list;
}
}
这样商品类目选择就实现了。